Skip to contentSkip to conversation

Improve VM CPUfreq and task placement behavior

PATCH Superseded David Dai 7 participants 20 messages Nov 11, 2023 – Jan 16, 2024

Subsystems (4)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

David Dai

author
#0 lore raw

v4 cover Improve VM CPUfreq and task placement behavior

Hi,

This patch series is a continuation of the talk Saravana gave at LPC 2022 titled "CPUfreq/sched and VM guest workload problems" [1][2][3]. The gist of the talk is that workloads running in a guest VM get terrible task placement and CPUfreq behavior when compared to running the same workload in the host. Effectively, no EAS(Energy Aware Scheduling) for threads inside VMs. This would make power and performance terrible just by running the workload in a VM even if we assume there is zero virtualization overhead.

With this series, a workload running in a VM gets the same task placement and CPUfreq behavior as it would when running in the host.

The idea is to improve VM CPUfreq/sched behavior by:
- Having guest kernel do accurate load tracking by taking host CPU arch/type and frequency into account.
- Sharing vCPU frequency requirements with the host so that the host can do proper frequency scaling and task placement on the host side.

Based on feedback from RFC v1 proposal[4], we've revised our implementation to using MMIO reads and writes to pass information from/to host instead of using hypercalls. In our example, the VMM(Virtual Machine Manager) translates the frequency requests into Uclamp_min and applies it to the vCPU thread as a hint to the host kernel.

To achieve the results below, configure the host to:
- Affine vCPUs to specific clusters.
- Set vCPU capacity to match the host CPU they are running on.

To make it easy for folks to try this out with CrosVM, we have put up userspace patches[5][6]. With those patches, you can configure CrosVM correctly by adding the options "--host-cpu-topology" and "--virt-cpufreq".

Results:
========

Here are some side-by-side comparisons of RFC v1 proposal vs the current patch series and are labelled as follows.

- (RFC v1) UtilHyp = hypercall + util_guest
- (current) UClampMMIO = MMIO + UClamp_min

Use cases running a minimal system inside a VM on a Pixel 6:
============================================================

FIO
Higher is better

+-------------------+----------+---------+--------+------------+--------+
| Usecase(avg MB/s) | Baseline | UtilHyp | %delta | UClampMMIO | %delta |
+-------------------+----------+---------+--------+------------+--------+
| Seq Write         |     13.3 |    16.4 |   +23% |       13.4 |    +1% |
+-------------------+----------+---------+--------+------------+--------+
| Rand Write        |     11.2 |    12.9 |   +15% |       11.2 |     0% |
+-------------------+----------+---------+--------+------------+--------+
| Seq Read          |      100 |     168 |   +68% |        136 |   +36% |
+-------------------+----------+---------+--------+------------+--------+
| Rand Read         |     20.5 |    35.6 |   +74% |       29.5 |   +44% |
+-------------------+----------+---------+--------+------------+--------+

CPU-based ML Inference Benchmark
Lower is better

+----------------+----------+------------+--------+------------+--------+
| Test Case (ms) | Baseline | UtilHyp    | %delta | UClampMMIO | %delta |
+----------------+----------+------------+--------+------------+--------+
| Cached Sample  |          |            |        |            |        |
| Inference      |     3.40 |       2.37 |   -30% |       2.97 |   -13% |
+----------------+----------+------------+--------+------------+--------+
| Small Sample   |          |            |        |            |        |
| Inference      |     9.87 |       6.78 |   -31% |       7.92 |   -20% |
+----------------+----------+------------+--------+------------+--------+
| Large Sample   |          |            |        |            |        |
| Inference      |    33.35 |      26.74 |   -20% |      31.48 |    -6% |
+----------------+----------+------------+--------+------------+--------+

Use cases running Android inside a VM on a Chromebook:
======================================================

PCMark (Emulates real world usecases)
Higher is better

+-------------------+----------+---------+--------+------------+--------+
| Test Case (score) | Baseline | UtilHyp | %delta | UClampMMIO | %delta |
+-------------------+----------+---------+--------+------------+--------+
| Weighted Total    |     5970 |    7162 |   +20% |       6782 |   +14% |
+-------------------+----------+---------+--------+------------+--------+
| Web Browsing      |     5558 |    5877 |    +6% |       5729 |    +3% |
+-------------------+----------+---------+--------+------------+--------+
| Video Editing     |     4921 |    5140 |    +4% |       5079 |    +3% |
+-------------------+----------+---------+--------+------------+--------+
| Writing           |     6864 |    9111 |   +33% |       8171 |   +10% |
+-------------------+----------+---------+--------+------------+--------+
| Photo Editing     |     7983 |   11349 |   +42% |      10313 |   +29% |
+-------------------+----------+---------+--------+------------+--------+
| Data Manipulation |     5814 |    6051 |    +4% |       6051 |    +1% |
+-------------------+----------+---------+--------+------------+--------+

PCMark Performance/mAh
Higher is better

+-------------------+----------+---------+--------+------------+--------+
|                   | Baseline | UtilHyp | %delta | UClampMMIO | %delta |
+-------------------+----------+---------+--------+------------+--------+
| Score/mAh         |       85 |     102 |   +20% |         94 |    10% |
+-------------------+----------+---------+--------+------------+--------+

Roblox
Higher is better

+-------------------+----------+---------+--------+------------+--------+
|                   | Baseline | UtilHyp | %delta | UClampMMIO | %delta |
+-------------------+----------+---------+--------+------------+--------+
| FPS               |    20.88 |   25.64 |   +23% |      24.05 |   +15% |
+-------------------+----------+---------+--------+------------+--------+

Roblox Frames/mAh
Higher is better

+-------------------+----------+---------+--------+------------+--------+
|                   | Baseline | UtilHyp | %delta | UClampMMIO | %delta |
+-------------------+----------+---------+--------+------------+--------+
| Frames/mAh        |    85.29 |  102.31 |   +20% |     94.20  |    10% |
+-------------------+----------+---------+--------+------------+--------+

We've simplified our implementation based on community feedback to make it less intrusive and to use a more generic MMIO interface for communication with the host. The results show that the current design still has tangible improvements over baseline. We'll continue looking into ways to reduce the overhead of the MMIO read/writes and submit separate and generic patches for that if we find any good optimizations.

Thanks,
David & Saravana

Cc Saravana Kannan <[email protected]>
Cc Quentin Perret <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Will Deacon <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Oliver Upton <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Pavan Kondeti <[email protected]>
Cc Gupta Pankaj <[email protected]>
Cc Mel Gorman <[email protected]>

v3 -> v4:
-Fixed dt-binding formatting issues
-Added additional dt-binding descriptions for “HW interfaces” -Changed dt-binding to “qemu,virtual-cpufreq”
-Fixed Kconfig formatting issues
-Removed frequency downscaling when requesting frequency updates -Removed ops and cpufreq driver data
-Added check to limit freq_scale to 1024
-Added KHZ in the register offset naming
-Added comments to explain FIE and not allowing dvfs_possible_from_any_cpu

v2 -> v3:
- Dropped patches adding new hypercalls
- Dropped patch adding util_guest in sched/fair
- Cpufreq driver now populates frequency using opp bindings
- Removed transition_delay_us=1 cpufreq setting as it was configured too agressively and resulted in poor I/O performance
- Modified guest cpufreq driver to read/write MMIO regions instead of using hypercalls to communicate with the host
- Modified guest cpufreq driver to pass frequency info instead of utilization of the current vCPU's runqueue which now takes iowait_boost into account from the schedutil governor
- Updated DT bindings for a virtual CPU frequency device Userspace changes:
- Updated CrosVM patches to emulate a virtual cpufreq device
- Updated to newer userspace binaries when collecting more recent benchmark data

v1 -> v2:
- No functional changes.
- Added description for EAS and removed DVFS in coverletter.
- Added a v2 tag to the subject.
- Fixed up the inconsistent "units" between tables.
- Made sure everyone is To/Cc-ed for all the patches in the series.

[1] - https://lpc.events/event/16/contributions/1195/
[2] - https://lpc.events/event/16/contributions/1195/attachments/970/1893/LPC%202022%20-%20VM%20DVFS.pdf
[3] - https://www.youtube.com/watch?v=hIg_5bg6opU
[4] - https://lore.kernel.org/all/[email protected]/
[5] - https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4208668
[6] - https://chromium-review.googlesource.com/q/topic:%22virtcpufreq-v4%22

David Dai (2):
dt-bindings: cpufreq: add virtual cpufreq device
cpufreq: add virtual-cpufreq driver

 .../cpufreq/qemu,cpufreq-virtual.yaml         |  99 +++++++++
 drivers/cpufreq/Kconfig                       |  15 ++
 drivers/cpufreq/Makefile                      |   1 +
 drivers/cpufreq/virtual-cpufreq.c             | 201 ++++++++++++++++++
 include/linux/arch_topology.h                 |   1 +
 5 files changed, 317 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml
 create mode 100644 drivers/cpufreq/virtual-cpufreq.c
Signature · 1 line
2.42.0.869.gea05f2083d-goog
author
#1 lore raw

PATCH v4 1/2 dt-bindings: cpufreq: add virtual cpufreq device

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

---
 .../cpufreq/qemu,cpufreq-virtual.yaml         | 99 +++++++++++++++++++
 1 file changed, 99 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +99 -0
new file mode 100644
index 000000000000..16606cf1fd1a
@@ -0,0 +1,99 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/cpufreq/qemu,cpufreq-virtual.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Virtual CPUFreq
+
+maintainers:
+  - David Dai <[email protected]>
+  - Saravana Kannan <[email protected]>
+
+description:
+  Virtual CPUFreq is a virtualized driver in guest kernels that sends frequency
+  selection of its vCPUs as a hint to the host through MMIO regions. Each vCPU
+  is associated with a frequency domain which can be shared with other vCPUs.
+  Each frequency domain has its own set of registers for frequency controls.
+
+properties:
+  compatible:
+    const: qemu,virtual-cpufreq
+
+  reg:
+    maxItems: 1
+    description:
+      Address and size of region containing frequency controls for each of the
+      frequency domains. Regions for each frequency domain is placed
+      contiugously and contain registers for controlling DVFS(Dynamic Frequency
+      and Voltage) characteristics. The size of the region is proportional to
+      total number of frequency domains.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    // This example shows a two CPU configuration with a frequency domain
+    // for each CPU.
+    cpus {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      cpu@0 {
+        compatible = "arm,armv8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table0>;
+      };
+
+      cpu@1 {
+        compatible = "arm,armv8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table1>;
+      };
+    };
+
+    opp_table0: opp-table-0 {
+      compatible = "operating-points-v2";
+      opp-shared;
+
+      opp1098000000 {
+        opp-hz = /bits/ 64 <1098000000>;
+        opp-level = <1>;
+      };
+
+      opp1197000000 {
+        opp-hz = /bits/ 64 <1197000000>;
+        opp-level = <2>;
+      };
+    };
+
+    opp_table1: opp-table-1 {
+      compatible = "operating-points-v2";
+      opp-shared;
+
+      opp1106000000 {
+        opp-hz = /bits/ 64 <1106000000>;
+        opp-level = <1>;
+      };
+
+      opp1277000000 {
+        opp-hz = /bits/ 64 <1277000000>;
+        opp-level = <2>;
+      };
+    };
+
+    soc {
+      #address-cells = <1>;
+      #size-cells = <1>;
+
+      cpufreq@1040000 {
+        compatible = "qemu,virtual-cpufreq";
+        reg = <0x1040000 0x10>;
+      };
+    };
Signature · 1 line
2.42.0.869.gea05f2083d-goog
maintainer
#2 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml line 21

Quoted 5 lines · David Dai
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml +4 -0
+$id: http://devicetree.org/schemas/cpufreq/qemu,cpufreq-virtual.yaml#
+properties:
+  compatible:
+    const: qemu,virtual-cpufreq

Not sure why we need to mention QEMU here.. Why limit this to just QEMU ?

Signature · 1 line
viresh
maintainer
#3 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

Quoted 8 lines · Viresh Kumar

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml line 21

Quoted 5 lines · David Dai
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml +4 -0
+$id: http://devicetree.org/schemas/cpufreq/qemu,cpufreq-virtual.yaml#
+properties:
+  compatible:
+    const: qemu,virtual-cpufreq

Not sure why we need to mention QEMU here.. Why limit this to just QEMU ?

Because that is the implementation. And also to discourage anyone from using this on their hardware or thinking it is generic and the way to extend it to another implementation is adding properties.

Rob

#4 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

In reply to David Dai
Quoted 7 lines · David Dai

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

I would really refrain form having absolute frequencies here. A virtual machine can be migrated, and there are *zero* guarantees that the target system has the same clock range as the source.

This really should be a relative number, much like the capacity. That, at least, can be migrated across systems.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#5 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

Quoted 11 lines · Marc Zyngier
Quoted 7 lines · David Dai

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

Quoted 6 lines

I would really refrain form having absolute frequencies here. A virtual machine can be migrated, and there are *zero* guarantees that the target system has the same clock range as the source.

This really should be a relative number, much like the capacity. That, at least, can be migrated across systems.

There's nothing in this patch that mandates absolute frequency. In true KVM philosophy, we leave it to the VMM to decide.

-Saravana

#6 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

Quoted 17 lines · Saravana Kannan
Quoted 15 lines · Marc Zyngier
Quoted 7 lines · David Dai

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

I would really refrain form having absolute frequencies here. A virtual machine can be migrated, and there are *zero* guarantees that the target system has the same clock range as the source.

Quoted 5 lines
Quoted 2 lines

This really should be a relative number, much like the capacity. That, at least, can be migrated across systems.

There's nothing in this patch that mandates absolute frequency. In true KVM philosophy, we leave it to the VMM to decide.

This has nothing to do with KVM. It would apply to any execution environment, including QEMU in TCG mode.

To quote the original patch:

+ description:

+      Address and size of region containing frequency controls for each of the
+      frequency domains. Regions for each frequency domain is placed
+      contiugously and contain registers for controlling DVFS(Dynamic Frequency
+      and Voltage) characteristics. The size of the region is proportional to
+      total number of frequency domains.

What part of that indicates that *relative* frequencies are acceptable? The example explicitly uses the opp-v2 binding, which clearly is about absolute frequency.

To reiterate: absolute frequencies are not the right tool for the job, and they should explicitly be described as relative in the spec. Not left as a "whatev'" option for the execution environment to interpret.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#7 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

Sorry for the delay in response. Was very busy for a while and then holidays started.

Quoted 38 lines · Marc Zyngier
Quoted 17 lines · Saravana Kannan
Quoted 15 lines · Marc Zyngier
Quoted 7 lines · David Dai

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

I would really refrain form having absolute frequencies here. A virtual machine can be migrated, and there are *zero* guarantees that the target system has the same clock range as the source.

Quoted 5 lines
Quoted 2 lines

This really should be a relative number, much like the capacity. That, at least, can be migrated across systems.

There's nothing in this patch that mandates absolute frequency. In true KVM philosophy, we leave it to the VMM to decide.

This has nothing to do with KVM. It would apply to any execution environment, including QEMU in TCG mode.

To quote the original patch:

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +6 -0
+    description:
+      Address and size of region containing frequency controls for each of the
+      frequency domains. Regions for each frequency domain is placed
+      contiugously and contain registers for controlling DVFS(Dynamic Frequency
+      and Voltage) characteristics. The size of the region is proportional to
+      total number of frequency domains.
Quoted 3 lines

What part of that indicates that *relative* frequencies are acceptable? The example explicitly uses the opp-v2 binding, which clearly is about absolute frequency.

We can update the doc to make that clearer and update the example too.

Quoted 3 lines

To reiterate: absolute frequencies are not the right tool for the job, and they should explicitly be described as relative in the spec. Not left as a "whatev'" option for the execution environment to interpret.

I think it depends on the use case. If there's no plan to migrate the VM across different devices, there's no need to do the unnecessary normalization back and forth.

And if we can translate between pCPU frequency and a normalized frequency, we can do the same for whatever made up frequencies too. In fact, we plan to do exactly that in our internal use cases for this. There's nothing here that prevents the VMM from doing that.

Also, if there are hardware virtualized performance counters (AMU, CPPC, etc) that are used for frequency normalization, then we have to use the real frequencies in those devices otherwise the "current frequency" can be 2 GHz while the max normalized frequency is 1024 KHz. That'll mess up load tracking.

Thanks,
Saravana

#8 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

Quoted 49 lines · Saravana Kannan

Sorry for the delay in response. Was very busy for a while and then holidays started.

Quoted 38 lines · Marc Zyngier
Quoted 17 lines · Saravana Kannan
Quoted 15 lines · Marc Zyngier
Quoted 7 lines · David Dai

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

I would really refrain form having absolute frequencies here. A virtual machine can be migrated, and there are *zero* guarantees that the target system has the same clock range as the source.

Quoted 5 lines
Quoted 2 lines

This really should be a relative number, much like the capacity. That, at least, can be migrated across systems.

There's nothing in this patch that mandates absolute frequency. In true KVM philosophy, we leave it to the VMM to decide.

This has nothing to do with KVM. It would apply to any execution environment, including QEMU in TCG mode.

To quote the original patch:

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +6 -0
+    description:
+      Address and size of region containing frequency controls for each of the
+      frequency domains. Regions for each frequency domain is placed
+      contiugously and contain registers for controlling DVFS(Dynamic Frequency
+      and Voltage) characteristics. The size of the region is proportional to
+      total number of frequency domains.
Quoted 3 lines

What part of that indicates that *relative* frequencies are acceptable? The example explicitly uses the opp-v2 binding, which clearly is about absolute frequency.

We can update the doc to make that clearer and update the example too.

Quoted 7 lines
Quoted 3 lines

To reiterate: absolute frequencies are not the right tool for the job, and they should explicitly be described as relative in the spec. Not left as a "whatev'" option for the execution environment to interpret.

I think it depends on the use case. If there's no plan to migrate the VM across different devices, there's no need to do the unnecessary normalization back and forth.

VM migration is a given, specially when QEMU is involved. Designing something that doesn't support it is a bug, plain and simple.

Quoted 10 lines

And if we can translate between pCPU frequency and a normalized frequency, we can do the same for whatever made up frequencies too. In fact, we plan to do exactly that in our internal use cases for this. There's nothing here that prevents the VMM from doing that.

Also, if there are hardware virtualized performance counters (AMU, CPPC, etc) that are used for frequency normalization, then we have to use the real frequencies in those devices otherwise the "current frequency" can be 2 GHz while the max normalized frequency is 1024 KHz. That'll mess up load tracking.

And that's exactly why this shouldn't be a *frequency*, but a performance scale or some other unit-less coefficient. Just like the big-little capacity.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#9 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

Quoted 56 lines · Marc Zyngier
Quoted 53 lines · Saravana Kannan

Sorry for the delay in response. Was very busy for a while and then holidays started.

Quoted 38 lines · Marc Zyngier
Quoted 17 lines · Saravana Kannan
Quoted 15 lines · Marc Zyngier
Quoted 7 lines · David Dai

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

I would really refrain form having absolute frequencies here. A virtual machine can be migrated, and there are *zero* guarantees that the target system has the same clock range as the source.

Quoted 5 lines
Quoted 2 lines

This really should be a relative number, much like the capacity. That, at least, can be migrated across systems.

There's nothing in this patch that mandates absolute frequency. In true KVM philosophy, we leave it to the VMM to decide.

This has nothing to do with KVM. It would apply to any execution environment, including QEMU in TCG mode.

To quote the original patch:

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +6 -0
+    description:
+      Address and size of region containing frequency controls for each of the
+      frequency domains. Regions for each frequency domain is placed
+      contiugously and contain registers for controlling DVFS(Dynamic Frequency
+      and Voltage) characteristics. The size of the region is proportional to
+      total number of frequency domains.
Quoted 3 lines

What part of that indicates that *relative* frequencies are acceptable? The example explicitly uses the opp-v2 binding, which clearly is about absolute frequency.

We can update the doc to make that clearer and update the example too.

Quoted 3 lines

To reiterate: absolute frequencies are not the right tool for the job, and they should explicitly be described as relative in the spec. Not left as a "whatev'" option for the execution environment to interpret.

Quoted 6 lines
Quoted 3 lines

I think it depends on the use case. If there's no plan to migrate the VM across different devices, there's no need to do the unnecessary normalization back and forth.

VM migration is a given, specially when QEMU is involved. Designing something that doesn't support it is a bug, plain and simple.

I'm not saying this patch series doesn't allow migrating. I'm just pointing out that normalization might not always be worth it for a VMM to do.

We'll update the example and documentation to used normalize values. CPUfreq itself used KHz for the tables and we can't really change that when we are emulating CPU freq scaling. Same goes for OPP table DT property names.

But the values we use can be 0 to 1024 Hz and be normalized.

Quoted 11 lines
Quoted 10 lines

And if we can translate between pCPU frequency and a normalized frequency, we can do the same for whatever made up frequencies too. In fact, we plan to do exactly that in our internal use cases for this. There's nothing here that prevents the VMM from doing that.

Also, if there are hardware virtualized performance counters (AMU, CPPC, etc) that are used for frequency normalization, then we have to use the real frequencies in those devices otherwise the "current frequency" can be 2 GHz while the max normalized frequency is 1024 KHz. That'll mess up load tracking.

Quoted 3 lines

And that's exactly why this shouldn't be a *frequency*, but a performance scale or some other unit-less coefficient. Just like the big-little capacity.

Sorry I wasn't cleared in my explanation. Let me explain it better. When performance counters are available, the scheduler uses them to compute the current average CPU frequency over a scheduler tick. It looks at the performance counters to figure out how many CPU cycles have gone by and how much time has gone by and does (delta cycles / delta seconds) to compute current CPU freq in Hz. So, when a HW virtualized performance counter is available, and the scheduler inside the VM uses it to compute the average CPU frequency, the resulting frequency is going to be the real physical CPU frequency. And the CPU frequency value the scheduler used to compute the PELT load will be completely different from the normalized values and the load tracking will be completely wrong.

Using their performance counters inside the VM reduces a lot of MMIO access exits to the host or memory accesses. So it makes sense a VM might want to use that. I was just trying to say that in that situation, a VMM might have a good reason to just use the real frequencies inside the VM too.

In any case, we can update the doc to use normalized values/examples and call out this caveat.

Thanks,
Saravana

#10 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

In reply to David Dai
Quoted 29 lines · David Dai

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

---
 .../cpufreq/qemu,cpufreq-virtual.yaml         | 99 +++++++++++++++++++
 1 file changed, 99 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +8 -0
new file mode 100644
index 000000000000..16606cf1fd1a
@@ -0,0 +1,99 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/cpufreq/qemu,cpufreq-virtual.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Virtual CPUFreq
+

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml line 18

Quoted 10 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +10 -0
+maintainers:
+  - David Dai <[email protected]>
+  - Saravana Kannan <[email protected]>
+
+description:
+  Virtual CPUFreq is a virtualized driver in guest kernels that sends frequency
+  selection of its vCPUs as a hint to the host through MMIO regions. Each vCPU
+  is associated with a frequency domain which can be shared with other vCPUs.
+  Each frequency domain has its own set of registers for frequency controls.
+

Are these register controls described somewhere ? The reason I ask is we should be able to have single implementation of this virtual cpufreq irrespective of the firmware used(DT vs ACPI) IMO.

Quoted 62 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +61 -0
+properties:
+  compatible:
+    const: qemu,virtual-cpufreq
+
+  reg:
+    maxItems: 1
+    description:
+      Address and size of region containing frequency controls for each of the
+      frequency domains. Regions for each frequency domain is placed
+      contiugously and contain registers for controlling DVFS(Dynamic Frequency
+      and Voltage) characteristics. The size of the region is proportional to
+      total number of frequency domains.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    // This example shows a two CPU configuration with a frequency domain
+    // for each CPU.
+    cpus {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      cpu@0 {
+        compatible = "arm,armv8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table0>;
+      };
+
+      cpu@1 {
+        compatible = "arm,armv8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table1>;
+      };
+    };
+
+    opp_table0: opp-table-0 {
+      compatible = "operating-points-v2";
+      opp-shared;
+
+      opp1098000000 {
+        opp-hz = /bits/ 64 <1098000000>;
+        opp-level = <1>;
+      };
+
+      opp1197000000 {
+        opp-hz = /bits/ 64 <1197000000>;
+        opp-level = <2>;
+      };
+    };
+
+    opp_table1: opp-table-1 {
+      compatible = "operating-points-v2";
+      opp-shared;
+

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml line 89

Quoted 10 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +10 -0
+      opp1106000000 {
+        opp-hz = /bits/ 64 <1106000000>;
+        opp-level = <1>;
+      };
+
+      opp1277000000 {
+        opp-hz = /bits/ 64 <1277000000>;
+        opp-level = <2>;
+      };
+    };

I think using OPP with absolute frequencies seems not appropriate here. Why can't these fetched from the registers and have some abstract values instead ?

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml line 97

Quoted 7 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +7 -0
+    soc {
+      #address-cells = <1>;
+      #size-cells = <1>;
+
+      cpufreq@1040000 {
+        compatible = "qemu,virtual-cpufreq";
+        reg = <0x1040000 0x10>;

So just 16bytes for 2 CPU system ? How does the register layout look like ? I assume just 4 x 32bit registers: 2 for reading and 2 for setting the frequencies ?

--
Regards,
Sudeep

#11 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

Quoted 37 lines · Sudeep Holla
Quoted 35 lines · David Dai

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

---
 .../cpufreq/qemu,cpufreq-virtual.yaml         | 99 +++++++++++++++++++
 1 file changed, 99 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +14 -0
new file mode 100644
index 000000000000..16606cf1fd1a
@@ -0,0 +1,99 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/cpufreq/qemu,cpufreq-virtual.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Virtual CPUFreq
+
+maintainers:
+  - David Dai <[email protected]>
+  - Saravana Kannan <[email protected]>
+
+description:
+  Virtual CPUFreq is a virtualized driver in guest kernels that sends frequency
Quoted 8 lines

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml line 18

Quoted 4 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +4 -0
+  selection of its vCPUs as a hint to the host through MMIO regions. Each vCPU
+  is associated with a frequency domain which can be shared with other vCPUs.
+  Each frequency domain has its own set of registers for frequency controls.
+

Are these register controls described somewhere ? The reason I ask is we should be able to have single implementation of this virtual cpufreq irrespective of the firmware used(DT vs ACPI) IMO.

Agree that we want the same driver to work for DT and ACPI. This doc was written to be similar to other DT binding docs that don't describe the registers in the DT binding. The registers are pretty straight forward (can tell from the code too). One register to "set" the frequency and another to "get" the current frequency. We don't have any ACPI expertise/hardware to test this on or care about it right now. But David looked at some ACPI drivers and we think it should be trivial to add ACPI support to this. Just a different set of probe functions to register and populate the CPUfreq table.

Quoted 69 lines
Quoted 69 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +68 -0
+properties:
+  compatible:
+    const: qemu,virtual-cpufreq
+
+  reg:
+    maxItems: 1
+    description:
+      Address and size of region containing frequency controls for each of the
+      frequency domains. Regions for each frequency domain is placed
+      contiugously and contain registers for controlling DVFS(Dynamic Frequency
+      and Voltage) characteristics. The size of the region is proportional to
+      total number of frequency domains.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    // This example shows a two CPU configuration with a frequency domain
+    // for each CPU.
+    cpus {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      cpu@0 {
+        compatible = "arm,armv8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table0>;
+      };
+
+      cpu@1 {
+        compatible = "arm,armv8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table1>;
+      };
+    };
+
+    opp_table0: opp-table-0 {
+      compatible = "operating-points-v2";
+      opp-shared;
+
+      opp1098000000 {
+        opp-hz = /bits/ 64 <1098000000>;
+        opp-level = <1>;
+      };
+
+      opp1197000000 {
+        opp-hz = /bits/ 64 <1197000000>;
+        opp-level = <2>;
+      };
+    };
+
+    opp_table1: opp-table-1 {
+      compatible = "operating-points-v2";
+      opp-shared;
+
+      opp1106000000 {
+        opp-hz = /bits/ 64 <1106000000>;
+        opp-level = <1>;
+      };
+
+      opp1277000000 {
+        opp-hz = /bits/ 64 <1277000000>;
Quoted 8 lines

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml line 74

Quoted 4 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +3 -0
+        opp-level = <2>;
+      };
+    };

I think using OPP with absolute frequencies seems not appropriate here. Why can't these fetched from the registers and have some abstract values instead ?

Whether the frequencies are real or you want to cap it to 1024 and normalize it, you still need to populate the CPUfreq table. And we didn't want to reinvent the wheel and want to use existing means of representing the table in as cross-architecture as possible -- so, DT and ACPI should cover them all. For example, if we want to say CPU0 and 1 for a single CPUfreq policy, that's all already doable in DT. We don't want to reinvent new schemes/register interfaces for that.

Quoted 9 lines

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml line 97

Quoted 8 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +7 -0
+    soc {
+      #address-cells = <1>;
+      #size-cells = <1>;
+
+      cpufreq@1040000 {
+        compatible = "qemu,virtual-cpufreq";
+        reg = <0x1040000 0x10>;
Quoted 3 lines

So just 16bytes for 2 CPU system ? How does the register layout look like ? I assume just 4 x 32bit registers: 2 for reading and 2 for setting the frequencies ?

Yup. 2 registers per CPU frequency domain or policy.

Thanks,
Saravana

#12 lore raw

Re: [PATCH v4 1/2] dt-bindings: cpufreq: add virtual cpufreq device

In reply to David Dai
Quoted 100 lines · David Dai

Adding bindings to represent a virtual cpufreq device.

Virtual machines may expose MMIO regions for a virtual cpufreq device for guests to read frequency information or to request frequency selection. The virtual cpufreq device has an individual controller for each frequency domain.

---
  .../cpufreq/qemu,cpufreq-virtual.yaml         | 99 +++++++++++++++++++
  1 file changed, 99 insertions(+)

create mode 100644 Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +79 -0
new file mode 100644
index 000000000000..16606cf1fd1a
@@ -0,0 +1,99 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/cpufreq/qemu,cpufreq-virtual.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Virtual CPUFreq
+
+maintainers:
+  - David Dai <[email protected]>
+  - Saravana Kannan <[email protected]>
+
+description:
+  Virtual CPUFreq is a virtualized driver in guest kernels that sends frequency
+  selection of its vCPUs as a hint to the host through MMIO regions. Each vCPU
+  is associated with a frequency domain which can be shared with other vCPUs.
+  Each frequency domain has its own set of registers for frequency controls.
+
+properties:
+  compatible:
+    const: qemu,virtual-cpufreq
+
+  reg:
+    maxItems: 1
+    description:
+      Address and size of region containing frequency controls for each of the
+      frequency domains. Regions for each frequency domain is placed
+      contiugously and contain registers for controlling DVFS(Dynamic Frequency
+      and Voltage) characteristics. The size of the region is proportional to
+      total number of frequency domains.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    // This example shows a two CPU configuration with a frequency domain
+    // for each CPU.
+    cpus {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      cpu@0 {
+        compatible = "arm,armv8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table0>;
+      };
+
+      cpu@1 {
+        compatible = "arm,armv8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table1>;
+      };
+    };
+
+    opp_table0: opp-table-0 {
+      compatible = "operating-points-v2";
+      opp-shared;
+
+      opp1098000000 {
+        opp-hz = /bits/ 64 <1098000000>;
+        opp-level = <1>;
+      };
+
+      opp1197000000 {
+        opp-hz = /bits/ 64 <1197000000>;
+        opp-level = <2>;
+      };
+    };
+
+    opp_table1: opp-table-1 {
+      compatible = "operating-points-v2";
+      opp-shared;
+

Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml line 89

Quoted 10 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +10 -0
+      opp1106000000 {
+        opp-hz = /bits/ 64 <1106000000>;
+        opp-level = <1>;
+      };
+
+      opp1277000000 {
+        opp-hz = /bits/ 64 <1277000000>;
+        opp-level = <2>;
+      };
+    };

NIT: If my understanding is correct, it might be worth re-iterating that these OPPs should mirror the host frequency domain this vCPU is pinned to.

Also, since VM migration has been mentioned elsewhere in this thread, am I right in saying that you can't change these OPPs after registration? So, even if one wants to migrate, one has to migrate to an SoC with the same frequency domains anyway, otherwise the OPPs in the VM are entirely bogus?

Quoted 10 lines
Documentation/devicetree/bindings/cpufreq/qemu,cpufreq-virtual.yaml new file +10 -0
+
+    soc {
+      #address-cells = <1>;
+      #size-cells = <1>;
+
+      cpufreq@1040000 {
+        compatible = "qemu,virtual-cpufreq";
+        reg = <0x1040000 0x10>;
+      };
+    };
author
#13 lore raw

PATCH v4 2/2 cpufreq: add virtual-cpufreq driver

In reply to David Dai

Introduce a virtualized cpufreq driver for guest kernels to improve performance and power of workloads within VMs.

This driver does two main things:

1. Sends the frequency of vCPUs as a hint to the host. The host uses the hint to schedule the vCPU threads and decide physical CPU frequency.

2. If a VM does not support a virtualized FIE(like AMUs), it queries the host CPU frequency by reading a MMIO region of a virtual cpufreq device to update the guest's frequency scaling factor periodically. This enables accurate Per-Entity Load Tracking for tasks running in the guest.

---
 drivers/cpufreq/Kconfig           |  15 +++
 drivers/cpufreq/Makefile          |   1 +
 drivers/cpufreq/virtual-cpufreq.c | 201 ++++++++++++++++++++++++++++++
 include/linux/arch_topology.h     |   1 +
 4 files changed, 218 insertions(+)
 create mode 100644 drivers/cpufreq/virtual-cpufreq.c
drivers/cpufreq/Kconfig +15 -0
index 35efb53d5492..f2d37075aa10 100644
@@ -217,6 +217,21 @@ config CPUFREQ_DT
 
 	  If in doubt, say N.
 
+config CPUFREQ_VIRT
+	tristate "Virtual cpufreq driver"
+	depends on OF
+	select PM_OPP
+	help
+	  This adds a virtualized cpufreq driver for guest kernels that
+	  read/writes to a MMIO region for a virtualized cpufreq device to
+	  communicate with the host. It sends frequency updates to the host
+	  which gets used as a hint to schedule vCPU threads and select CPU
+	  frequency. If a VM does not support a virtualized FIE such as AMUs,
+	  it updates the frequency scaling factor by polling host CPU frequency
+	  to enable accurate Per-Entity Load Tracking for tasks running in the guest.
+
+	  If in doubt, say N.
+
 config CPUFREQ_DT_PLATDEV
 	tristate "Generic DT based cpufreq platdev driver"
 	depends on OF
drivers/cpufreq/Makefile +1 -0
index 8d141c71b016..eb72ecdc24db 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET)	+= cpufreq_governor_attr_set.o
 
 obj-$(CONFIG_CPUFREQ_DT)		+= cpufreq-dt.o
 obj-$(CONFIG_CPUFREQ_DT_PLATDEV)	+= cpufreq-dt-platdev.o
+obj-$(CONFIG_CPUFREQ_VIRT)		+= virtual-cpufreq.o
 
 # Traces
 CFLAGS_amd-pstate-trace.o               := -I$(src)
drivers/cpufreq/virtual-cpufreq.c new file +201 -0

201 lines not on this page · raw message

include/linux/arch_topology.h +1 -0
index a07b510e7dc5..888282dce2ba 100644
@@ -42,6 +42,7 @@ enum scale_freq_source {
 	SCALE_FREQ_SOURCE_CPUFREQ = 0,
 	SCALE_FREQ_SOURCE_ARCH,
 	SCALE_FREQ_SOURCE_CPPC,
+	SCALE_FREQ_SOURCE_VIRT,
 };
 
 struct scale_freq_data {
Signature · 1 line
2.42.0.869.gea05f2083d-goog
maintainer
#14 lore raw

Re: [PATCH v4 2/2] cpufreq: add virtual-cpufreq driver

drivers/cpufreq/virtual-cpufreq.c line 47

Quoted 4 lines · David Dai
drivers/cpufreq/virtual-cpufreq.c +3 -0
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+	writel_relaxed(policy->cached_target_freq,

Drivers shouldn't be using the cached_target_freq directly. Use the target freq or index passed from cpufreq core.

drivers/cpufreq/virtual-cpufreq.c line 121

Quoted 5 lines
drivers/cpufreq/virtual-cpufreq.c +5 -0
+static int virt_cpufreq_cpu_exit(struct cpufreq_policy *policy)
+{
+	topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_VIRT, policy->related_cpus);
+	kfree(policy->freq_table);
+	policy->freq_table = NULL;

No need of doing this. Also the order of above two calls is wrong anyway.

Signature · 1 line
viresh
author
#15 lore raw

Re: [PATCH v4 2/2] cpufreq: add virtual-cpufreq driver

Hi Viresh,

Apologies for the late reply,

Quoted 9 lines · Viresh Kumar

drivers/cpufreq/virtual-cpufreq.c line 47

Quoted 4 lines · David Dai
drivers/cpufreq/virtual-cpufreq.c +3 -0
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+     writel_relaxed(policy->cached_target_freq,

Drivers shouldn't be using the cached_target_freq directly. Use the target freq or index passed from cpufreq core.

We were trying to avoid rounding to frequency table entries to provide more accurate frequency requests. However, we didn't find any significant power or performance regressions using the frequencies from the table, so I'll send another patch series using your suggestion.

Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 121

Quoted 6 lines
drivers/cpufreq/virtual-cpufreq.c +5 -0
+static int virt_cpufreq_cpu_exit(struct cpufreq_policy *policy)
+{
+     topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_VIRT, policy->related_cpus);
+     kfree(policy->freq_table);
+     policy->freq_table = NULL;

No need of doing this. Also the order of above two calls is wrong anyway.

Can you clarify this point a bit more? Are you suggesting to just remove setting policy->freq_table to NULL and swap the ordering freeing the freq_table vs clearing the topology source? I can alternatively use dev_pm_opp_free_cpufreq_table to mirror the init.

Thanks,
David

Quoted 3 lines

--
viresh

maintainer
#16 lore raw

Re: [PATCH v4 2/2] cpufreq: add virtual-cpufreq driver

Quoted 22 lines · David Dai

Hi Viresh,

Apologies for the late reply,

Quoted 9 lines · Viresh Kumar

drivers/cpufreq/virtual-cpufreq.c line 47

Quoted 4 lines · David Dai
drivers/cpufreq/virtual-cpufreq.c +3 -0
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+     writel_relaxed(policy->cached_target_freq,

Drivers shouldn't be using the cached_target_freq directly. Use the target freq or index passed from cpufreq core.

We were trying to avoid rounding to frequency table entries to provide more accurate frequency requests. However, we didn't find any significant power or performance regressions using the frequencies from the table, so I'll send another patch series using your suggestion.

Quoted 1 line
Quoted 4 lines
Quoted 4 lines

drivers/cpufreq/virtual-cpufreq.c line 120

Quoted 4 lines
drivers/cpufreq/virtual-cpufreq.c +4 -0
+static int virt_cpufreq_cpu_exit(struct cpufreq_policy *policy)
+{
+     topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_VIRT, policy->related_cpus);
+     kfree(policy->freq_table);

This becomes a dangling pointer for a very short amount of time. There may or may not be a actual race here and so I said the ordering must be just the opposite anyway.

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ policy->freq_table = NULL;

And I thought this isn't required too since the core is going the free the policy structure right after returning from here. But maybe it is not a guarantee that the core provides (the code can change in future) and so be better to unset it anyway.

Quoted 6 lines
Quoted 1 line

No need of doing this. Also the order of above two calls is wrong anyway.

Can you clarify this point a bit more? Are you suggesting to just remove setting policy->freq_table to NULL and swap the ordering freeing the freq_table vs clearing the topology source? I can alternatively use dev_pm_opp_free_cpufreq_table to mirror the init.

That would be better actually, to let a single piece of code manage this :)

Signature · 1 line
viresh
#17 lore raw

Re: [PATCH v4 2/2] cpufreq: add virtual-cpufreq driver

In reply to David Dai
Quoted 47 lines · David Dai

[...]

drivers/cpufreq/Makefile +1 -0
index 8d141c71b016..eb72ecdc24db 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET)	+= cpufreq_governor_attr_set.o
 
  obj-$(CONFIG_CPUFREQ_DT)		+= cpufreq-dt.o
  obj-$(CONFIG_CPUFREQ_DT_PLATDEV)	+= cpufreq-dt-platdev.o
+obj-$(CONFIG_CPUFREQ_VIRT)		+= virtual-cpufreq.o
 
  # Traces
  CFLAGS_amd-pstate-trace.o               := -I$(src)
drivers/cpufreq/virtual-cpufreq.c new file +28 -0
new file mode 100644
index 000000000000..f828d3345a68
@@ -0,0 +1,201 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2023 Google LLC
+ */
+
+#include <linux/arch_topology.h>
+#include <linux/cpufreq.h>
+#include <linux/init.h>
+#include <linux/sched.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/pm_opp.h>
+#include <linux/slab.h>
+
+#define REG_CUR_FREQ_KHZ_OFFSET 0x0
+#define REG_SET_FREQ_KHZ_OFFSET 0x4
+#define PER_CPU_OFFSET 0x8
+
+static void __iomem *base;
+
+static void virt_scale_freq_tick(void)
+{
+	int cpu = smp_processor_id();
+	u32 max_freq = (u32)cpufreq_get_hw_max_freq(cpu);
+	u64 cur_freq;
+	unsigned long scale;

drivers/cpufreq/virtual-cpufreq.c line 38

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+
+	cur_freq = (u64)readl_relaxed(base + cpu * PER_CPU_OFFSET
+			+ REG_CUR_FREQ_KHZ_OFFSET);
+
+	cur_freq <<= SCHED_CAPACITY_SHIFT;
+	scale = (unsigned long)div_u64(cur_freq, max_freq);
+	scale = min(scale, SCHED_CAPACITY_SCALE);
+
+	this_cpu_write(arch_freq_scale, scale);
+}

Here we update the scaling factor in the guest, but is there any way to let the guest know when the host dequeues the vCPU so that the guest PELT signal doesn't appear larger than it actually is? Is this a known limitation and is there a way to mitigate it?

Quoted 1 line

[...]

#18 lore raw
In reply to David Dai

Hi David,

Quoted 20 lines · David Dai

Hi,

This patch series is a continuation of the talk Saravana gave at LPC 2022 titled "CPUfreq/sched and VM guest workload problems" [1][2][3]. The gist of the talk is that workloads running in a guest VM get terrible task placement and CPUfreq behavior when compared to running the same workload in the host. Effectively, no EAS(Energy Aware Scheduling) for threads inside VMs. This would make power and performance terrible just by running the workload in a VM even if we assume there is zero virtualization overhead.

With this series, a workload running in a VM gets the same task placement and CPUfreq behavior as it would when running in the host.

The idea is to improve VM CPUfreq/sched behavior by:
- Having guest kernel do accurate load tracking by taking host CPU arch/type and frequency into account.
- Sharing vCPU frequency requirements with the host so that the host can do proper frequency scaling and task placement on the host side.

Quoted 6 lines

Based on feedback from RFC v1 proposal[4], we've revised our implementation to using MMIO reads and writes to pass information from/to host instead of using hypercalls. In our example, the VMM(Virtual Machine Manager) translates the frequency requests into Uclamp_min and applies it to the vCPU thread as a hint to the host kernel.

Sorry for not noticing this series until now.

The problem you are having with uclamp is actually the same as what I'm tackling right now. Basically my conclusion so far is that uclamp max aggregation faces quite many problems, which can be easily solved by sum aggregation (summing up the clamped utilization values instead of applying the max uclamp value to the whole rq):

https://lore.kernel.org/all/[email protected]/

What you described as util_guest sounds to me as exactly what uclamp_min under sum aggregation does. I'm really tempted to ask you to apply my series and see if the new uclamp_min does what you want, instead of introducing a new util_guest signal. If you have no time for this I can try to replicate your setup and do the experiments myself.

Also, my knowledge with KVM is limited. May I know where the vCPU fork happens? Can't you just set the p->sched_reset_on_fork flag on fork to not carry forward the uclamp values?

Quoted 2 lines

[...]

Hongyan

#19 lore raw
Quoted 4 lines · Hongyan Xia

Also, my knowledge with KVM is limited. May I know where the vCPU fork happens? Can't you just set the p->sched_reset_on_fork flag on fork to not carry forward the uclamp values?

There is no "vCPU fork". The vcpu is a VMM (qemu, crosvm, kvmtool) thread, nothing else.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.