Skip to contentSkip to conversation

Improve VM CPUfreq and task placement behavior

PATCH Superseded David Dai 9 participants 23 messages Jul 31, 2023 – Aug 24, 2023

Subsystems (4)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

David Dai

author
#0 lore raw

v3 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 RFC V3 proposal and are labelled as follows. Some of the numbers have changed due to using newer userspace binaries compared to RFC V1:

- (RFC V1) UtilHyp = hypercall + util_guest
- (V3) 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]>

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/c/crosvm/crosvm/+/4504738

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

 .../bindings/cpufreq/cpufreq-virtual.yaml     |  89 +++++++
 drivers/cpufreq/Kconfig                       |  15 ++
 drivers/cpufreq/Makefile                      |   1 +
 drivers/cpufreq/virtual-cpufreq.c             | 237 ++++++++++++++++++
 include/linux/arch_topology.h                 |   1 +
 5 files changed, 343 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml
 create mode 100644 drivers/cpufreq/virtual-cpufreq.c
Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
author
#1 lore raw

PATCH v3 1/2 dt-bindings: cpufreq: add bindings for virtual cpufreq

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 CPU.

---
 .../bindings/cpufreq/cpufreq-virtual.yaml     | 89 +++++++++++++++++++
 1 file changed, 89 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +89 -0
new file mode 100644
index 000000000000..f377cfc972ca
@@ -0,0 +1,89 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/cpufreq/cpufreq-virtual.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yamll#
+
+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. The host
+  uses the hint to schedule vCPU threads and select physical CPU frequency. It
+  enables accurate Per-Entity Load Tracking for tasks running in the guest by
+  querying host CPU frequency unless a virtualized FIE (ex. AMU) exists.
+
+properties:
+  compatible:
+    const: virtual,cpufreq
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    cpus {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      cpu@0 {
+        compatible = "arm,arm-v8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table0>;
+      };
+
+      cpu@1 {
+        compatible = "arm,arm-v8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table1>;
+      };
+    };
+
+    opp_table0: opp-table-0 {
+      compatible = "operating-points-v2";
+
+      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";
+
+      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 {
+        reg = <0x1040000 0x10>;
+        compatible = "virtual,cpufreq";
+      };
+    };
Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
author
#6 lore raw

PATCH v3 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 | 237 ++++++++++++++++++++++++++++++
 include/linux/arch_topology.h     |   1 +
 4 files changed, 254 insertions(+)
 create mode 100644 drivers/cpufreq/virtual-cpufreq.c
drivers/cpufreq/Kconfig +15 -0
index f429b9b37b76..3977ca796747 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 ef8510774913..bbc9f9bdfa4b 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 +237 -0

237 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.41.0.585.gd2178a4bd4-goog
maintainer
#2 lore raw

Re: [PATCH v3 1/2] dt-bindings: cpufreq: add bindings for virtual cpufreq

In reply to David Dai
Quoted 6 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 CPU.

Quoted 8 lines
---
 .../bindings/cpufreq/cpufreq-virtual.yaml     | 89 +++++++++++++++++++
 1 file changed, 89 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/jsonschema/validators.py", line 909, in resolve_from_url
document = self.store[url]
File "/usr/local/lib/python3.10/dist-packages/jsonschema/_utils.py", line 28, in __getitem__
return self.store[self.normalize(uri)]
KeyError: 'http://devicetree.org/meta-schemas/core.yamll'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/jsonschema/validators.py", line 912, in resolve_from_url
document = self.resolve_remote(url)
File "/usr/local/lib/python3.10/dist-packages/jsonschema/validators.py", line 1011, in resolve_remote
result = self.handlers[scheme](uri)
File "/usr/local/lib/python3.10/dist-packages/dtschema/schema.py", line 91, in http_handler
raise RefResolutionError('Error in referenced schema matching $id: ' + uri)
jsonschema.exceptions.RefResolutionError: Error in referenced schema matching $id: http://devicetree.org/meta-schemas/core.yamll

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/bin/dt-doc-validate", line 64, in <module>

    ret |= check_doc(f)

File "/usr/local/bin/dt-doc-validate", line 32, in check_doc
for error in sorted(dtsch.iter_errors(), key=lambda e: e.linecol):
File "/usr/local/lib/python3.10/dist-packages/dtschema/schema.py", line 130, in iter_errors
meta_schema = self.resolver.resolve_from_url(self['$schema'])
File "/usr/local/lib/python3.10/dist-packages/jsonschema/validators.py", line 914, in resolve_from_url
raise exceptions.RefResolutionError(exc)
jsonschema.exceptions.RefResolutionError: Error in referenced schema matching $id: http://devicetree.org/meta-schemas/core.yamll
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.example.dts:69.19-72.13: Warning (unit_address_vs_reg): /example-0/soc/cpufreq: node has a reg or ranges property, but no unit name Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.example.dtb: /example-0/cpus/cpu@0: failed to match any schema with compatible: ['arm,arm-v8'] Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.example.dtb: /example-0/cpus/cpu@1: failed to match any schema with compatible: ['arm,arm-v8']

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

#7 lore raw

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

In reply to David Dai

drivers/cpufreq/Kconfig config CPUFREQ_DT · line 224

Quoted 13 lines · David Dai
drivers/cpufreq/Kconfig +5 -0
index f429b9b37b76..3977ca796747 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

The 4 lines above should be indented with one tab (not 8 spaces).

drivers/cpufreq/Kconfig config CPUFREQ_DT · line 226

Quoted 2 lines
drivers/cpufreq/Kconfig +2 -0
+	  This adds a virtualized cpufreq driver for guest kernels that
+	  read/writes to a MMIO region for a virtualized cpufreq device to

reads/writes to an MMIO region

Quoted 7 lines
drivers/cpufreq/Kconfig +7 -0
+	  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.
Signature · 1 line
~Randy
author
#8 lore raw

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

Hi Randy,

Thanks for reviewing,

Quoted 12 lines · Randy Dunlap
Quoted 8 lines · David Dai
drivers/cpufreq/Kconfig +0 -0
index f429b9b37b76..3977ca796747 100644
@@ -217,6 +217,21 @@ config CPUFREQ_DT
 
        If in doubt, say N.
 
Quoted 7 lines

drivers/cpufreq/Kconfig config CPUFREQ_DT · line 224

Quoted 5 lines
drivers/cpufreq/Kconfig +5 -0
+config CPUFREQ_VIRT
+        tristate "Virtual cpufreq driver"
+     depends on OF
+     select PM_OPP
+        help

The 4 lines above should be indented with one tab (not 8 spaces).

Ok.

Quoted 5 lines

drivers/cpufreq/Kconfig config CPUFREQ_DT · line 226

Quoted 3 lines
drivers/cpufreq/Kconfig +2 -0
+       This adds a virtualized cpufreq driver for guest kernels that
+       read/writes to a MMIO region for a virtualized cpufreq device to

reads/writes to an MMIO region

Will fix these, thanks!
David

Quoted 11 lines

drivers/cpufreq/Kconfig config CPUFREQ_DT · line 233

Quoted 8 lines
drivers/cpufreq/Kconfig +7 -0
+       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.

--
~Randy

maintainer
#9 lore raw

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

In reply to David Dai
Quoted 20 lines · David Dai
drivers/cpufreq/virtual-cpufreq.c new file +14 -0
new file mode 100644
index 000000000000..66b0fd9b821c
@@ -0,0 +1,237 @@
+// 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>

drivers/cpufreq/virtual-cpufreq.c line 24

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+#include <linux/slab.h>
+
+#define REG_CUR_FREQ_OFFSET 0x0
+#define REG_SET_FREQ_OFFSET 0x4
+#define PER_CPU_OFFSET 0x8
+
+struct virt_cpufreq_ops {
+	void (*set_freq)(struct cpufreq_policy *policy, u32 freq);
+	u32 (*get_freq)(struct cpufreq_policy *policy);
+};

Since you only have one implementation currently, this isn't really required. Keep the data as NULL in virt_cpufreq_match and use writel/readl directly.

This can be updated if we need more implementations later.

Quoted 9 lines
drivers/cpufreq/virtual-cpufreq.c new file +9 -0
+struct virt_cpufreq_drv_data {
+	void __iomem *base;
+	const struct virt_cpufreq_ops *ops;
+};
+
+static void virt_cpufreq_set_freq(struct cpufreq_policy *policy, u32 freq)
+{
+	struct virt_cpufreq_drv_data *data = policy->driver_data;
+

drivers/cpufreq/virtual-cpufreq.c line 44

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+	writel_relaxed(freq, data->base + policy->cpu * PER_CPU_OFFSET
+			+ REG_SET_FREQ_OFFSET);
+}
+
+static u32 virt_cpufreq_get_freq(struct cpufreq_policy *policy)
+{
+	struct virt_cpufreq_drv_data *data = policy->driver_data;
+
+	return readl_relaxed(data->base + policy->cpu * PER_CPU_OFFSET
+			+ REG_CUR_FREQ_OFFSET);

This doesn't look properly aligned. Please run checkpatch (--strict (optional)).

Quoted 74 lines
drivers/cpufreq/virtual-cpufreq.c new file +74 -0
+}
+
+static const struct virt_cpufreq_ops virt_freq_ops = {
+	.set_freq = virt_cpufreq_set_freq,
+	.get_freq = virt_cpufreq_get_freq,
+};
+
+static void virt_scale_freq_tick(void)
+{
+	struct cpufreq_policy *policy = cpufreq_cpu_get(smp_processor_id());
+	struct virt_cpufreq_drv_data *data = policy->driver_data;
+	u32 max_freq = (u32)policy->cpuinfo.max_freq;
+	u64 cur_freq;
+	u64 scale;
+
+	cpufreq_cpu_put(policy);
+
+	cur_freq = (u64)data->ops->get_freq(policy);
+	cur_freq <<= SCHED_CAPACITY_SHIFT;
+	scale = div_u64(cur_freq, max_freq);
+
+	this_cpu_write(arch_freq_scale, (unsigned long)scale);
+}
+
+static struct scale_freq_data virt_sfd = {
+	.source = SCALE_FREQ_SOURCE_VIRT,
+	.set_freq_scale = virt_scale_freq_tick,
+};
+
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+	struct virt_cpufreq_drv_data *data = policy->driver_data;
+	/*
+	 * Use cached frequency to avoid rounding to freq table entries
+	 * and undo 25% frequency boost applied by schedutil.
+	 */
+	u32 freq = mult_frac(policy->cached_target_freq, 80, 100);
+
+	data->ops->set_freq(policy, freq);
+	return 0;
+}
+
+static unsigned int virt_cpufreq_fast_switch(struct cpufreq_policy *policy,
+		unsigned int target_freq)
+{
+	virt_cpufreq_set_perf(policy);
+	return target_freq;
+}
+
+static int virt_cpufreq_target_index(struct cpufreq_policy *policy,
+		unsigned int index)
+{
+	return virt_cpufreq_set_perf(policy);
+}
+
+static int virt_cpufreq_cpu_init(struct cpufreq_policy *policy)
+{
+	struct virt_cpufreq_drv_data *drv_data = cpufreq_get_driver_data();
+	struct cpufreq_frequency_table *table;
+	struct device *cpu_dev;
+	int ret;
+
+	cpu_dev = get_cpu_device(policy->cpu);
+	if (!cpu_dev)
+		return -ENODEV;
+
+	ret = dev_pm_opp_of_add_table(cpu_dev);
+	if (ret)
+		return ret;
+
+	ret = dev_pm_opp_get_opp_count(cpu_dev);
+	if (ret <= 0) {
+		dev_err(cpu_dev, "OPP table can't be empty\n");
+		return -ENODEV;

drivers/cpufreq/virtual-cpufreq.c line 128

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+	}
+
+	ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &table);
+	if (ret) {
+		dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
+		return ret;
+	}
+
+	policy->freq_table = table;
+	policy->dvfs_possible_from_any_cpu = false;

Why can't we call virt_cpufreq_target_index() from any CPU ?

Quoted 90 lines
drivers/cpufreq/virtual-cpufreq.c new file +90 -0
+	policy->fast_switch_possible = true;
+	policy->driver_data = drv_data;
+
+	/*
+	 * Only takes effect if another FIE source such as AMUs
+	 * have not been registered.
+	 */
+	topology_set_scale_freq_source(&virt_sfd, policy->cpus);
+
+	return 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;
+	return 0;
+}
+
+static int virt_cpufreq_online(struct cpufreq_policy *policy)
+{
+	/* Nothing to restore. */
+	return 0;
+}
+
+static int virt_cpufreq_offline(struct cpufreq_policy *policy)
+{
+	/* Dummy offline() to avoid exit() being called and freeing resources. */
+	return 0;
+}
+
+static struct cpufreq_driver cpufreq_virt_driver = {
+	.name		= "virt-cpufreq",
+	.init		= virt_cpufreq_cpu_init,
+	.exit		= virt_cpufreq_cpu_exit,
+	.online         = virt_cpufreq_online,
+	.offline        = virt_cpufreq_offline,
+	.verify		= cpufreq_generic_frequency_table_verify,
+	.target_index	= virt_cpufreq_target_index,
+	.fast_switch	= virt_cpufreq_fast_switch,
+	.attr		= cpufreq_generic_attr,
+};
+
+static int virt_cpufreq_driver_probe(struct platform_device *pdev)
+{
+	int ret;
+	struct virt_cpufreq_drv_data *drv_data;
+
+	drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
+	if (!drv_data)
+		return -ENOMEM;
+
+	drv_data->ops = of_device_get_match_data(&pdev->dev);
+	if (!drv_data->ops)
+		return -EINVAL;
+
+	drv_data->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(drv_data->base))
+		return PTR_ERR(drv_data->base);
+
+	cpufreq_virt_driver.driver_data = drv_data;
+
+	ret = cpufreq_register_driver(&cpufreq_virt_driver);
+	if (ret) {
+		dev_err(&pdev->dev, "Virtual CPUFreq driver failed to register: %d\n", ret);
+		return ret;
+	}
+
+	dev_dbg(&pdev->dev, "Virtual CPUFreq driver initialized\n");
+	return 0;
+}
+
+static int virt_cpufreq_driver_remove(struct platform_device *pdev)
+{
+	cpufreq_unregister_driver(&cpufreq_virt_driver);
+	return 0;
+}
+
+static const struct of_device_id virt_cpufreq_match[] = {
+	{ .compatible = "virtual,cpufreq", .data = &virt_freq_ops},
+	{}
+};
+MODULE_DEVICE_TABLE(of, virt_cpufreq_match);
+
+static struct platform_driver virt_cpufreq_driver = {
+	.probe = virt_cpufreq_driver_probe,
+	.remove = virt_cpufreq_driver_remove,
+	.driver = {

drivers/cpufreq/virtual-cpufreq.c line 228

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+		.name = "virt-cpufreq",
+		.of_match_table = virt_cpufreq_match,
+	},
+};
+
+static int __init virt_cpufreq_init(void)
+{
+	return platform_driver_register(&virt_cpufreq_driver);
+}
+postcore_initcall(virt_cpufreq_init);

Why do you want to use this and not module_init() ? Then you can simply use module_platform_driver().

Quoted 9 lines
drivers/cpufreq/virtual-cpufreq.c new file +9 -0
+
+static void __exit virt_cpufreq_exit(void)
+{
+	platform_driver_unregister(&virt_cpufreq_driver);
+}
+module_exit(virt_cpufreq_exit);
+
+MODULE_DESCRIPTION("Virtual cpufreq driver");
+MODULE_LICENSE("GPL");
Signature · 1 line
viresh
#15 lore raw

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

In reply to David Dai

Hi David,

drivers/cpufreq/virtual-cpufreq.c line 80

Quoted 7 lines · David Dai
drivers/cpufreq/virtual-cpufreq.c new file +7 -0
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+	struct virt_cpufreq_drv_data *data = policy->driver_data;
+	/*
+	 * Use cached frequency to avoid rounding to freq table entries
+	 * and undo 25% frequency boost applied by schedutil.
+	 */

The VMM would be a better place for this scaling I think, the driver can't/shouldn't make assumptions about the governor it is running with given that this is a guest userspace decision essentially.

IIRC the fast_switch() path is only used by schedutil, so one could probably make a case to scale things there, but it'd be inconsistent with the "slow" switch case, and would create a fragile dependency, so it's probably not worth pursuing.

drivers/cpufreq/virtual-cpufreq.c line 85

Quoted 5 lines
drivers/cpufreq/virtual-cpufreq.c new file +5 -0
+	u32 freq = mult_frac(policy->cached_target_freq, 80, 100);
+
+	data->ops->set_freq(policy, freq);
+	return 0;
+}

Thanks,
Quentin

#16 lore raw

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

Quoted 15 lines · Quentin Perret

Hi David,

drivers/cpufreq/virtual-cpufreq.c line 80

Quoted 7 lines · David Dai
drivers/cpufreq/virtual-cpufreq.c new file +7 -0
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+	struct virt_cpufreq_drv_data *data = policy->driver_data;
+	/*
+	 * Use cached frequency to avoid rounding to freq table entries
+	 * and undo 25% frequency boost applied by schedutil.
+	 */

The VMM would be a better place for this scaling I think, the driver can't/shouldn't make assumptions about the governor it is running with given that this is a guest userspace decision essentially.

Quoted 4 lines

IIRC the fast_switch() path is only used by schedutil, so one could probably make a case to scale things there, but it'd be inconsistent with the "slow" switch case, and would create a fragile dependency, so it's probably not worth pursuing.

Alternatively we could make the schedutil margin configurable via the cmdline or something along those lines, so we can set it to 0 in the guest and avoid the issue entirely.

Some partners have been asking for this IIRC , so I suspect there would be interest from other parties.

Thanks,
Quentin

#10 lore raw

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

In reply to Viresh Kumar
Quoted 140 lines · Viresh Kumar
Quoted 20 lines · David Dai
drivers/cpufreq/virtual-cpufreq.c new file +14 -0
new file mode 100644
index 000000000000..66b0fd9b821c
@@ -0,0 +1,237 @@
+// 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>

drivers/cpufreq/virtual-cpufreq.c line 24

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+#include <linux/slab.h>
+
+#define REG_CUR_FREQ_OFFSET 0x0
+#define REG_SET_FREQ_OFFSET 0x4
+#define PER_CPU_OFFSET 0x8
+
+struct virt_cpufreq_ops {
+     void (*set_freq)(struct cpufreq_policy *policy, u32 freq);
+     u32 (*get_freq)(struct cpufreq_policy *policy);
+};

Since you only have one implementation currently, this isn't really required. Keep the data as NULL in virt_cpufreq_match and use writel/readl directly.

This can be updated if we need more implementations later.

Quoted 9 lines
drivers/cpufreq/virtual-cpufreq.c new file +9 -0
+struct virt_cpufreq_drv_data {
+     void __iomem *base;
+     const struct virt_cpufreq_ops *ops;
+};
+
+static void virt_cpufreq_set_freq(struct cpufreq_policy *policy, u32 freq)
+{
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+

drivers/cpufreq/virtual-cpufreq.c line 44

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+     writel_relaxed(freq, data->base + policy->cpu * PER_CPU_OFFSET
+                     + REG_SET_FREQ_OFFSET);
+}
+
+static u32 virt_cpufreq_get_freq(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+
+     return readl_relaxed(data->base + policy->cpu * PER_CPU_OFFSET
+                     + REG_CUR_FREQ_OFFSET);

This doesn't look properly aligned. Please run checkpatch (--strict (optional)).

Quoted 78 lines
drivers/cpufreq/virtual-cpufreq.c new file +78 -0
+}
+
+static const struct virt_cpufreq_ops virt_freq_ops = {
+     .set_freq = virt_cpufreq_set_freq,
+     .get_freq = virt_cpufreq_get_freq,
+};
+
+static void virt_scale_freq_tick(void)
+{
+     struct cpufreq_policy *policy = cpufreq_cpu_get(smp_processor_id());
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+     u32 max_freq = (u32)policy->cpuinfo.max_freq;
+     u64 cur_freq;
+     u64 scale;
+
+     cpufreq_cpu_put(policy);
+
+     cur_freq = (u64)data->ops->get_freq(policy);
+     cur_freq <<= SCHED_CAPACITY_SHIFT;
+     scale = div_u64(cur_freq, max_freq);
+
+     this_cpu_write(arch_freq_scale, (unsigned long)scale);
+}
+
+static struct scale_freq_data virt_sfd = {
+     .source = SCALE_FREQ_SOURCE_VIRT,
+     .set_freq_scale = virt_scale_freq_tick,
+};
+
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+     /*
+      * Use cached frequency to avoid rounding to freq table entries
+      * and undo 25% frequency boost applied by schedutil.
+      */
+     u32 freq = mult_frac(policy->cached_target_freq, 80, 100);
+
+     data->ops->set_freq(policy, freq);
+     return 0;
+}
+
+static unsigned int virt_cpufreq_fast_switch(struct cpufreq_policy *policy,
+             unsigned int target_freq)
+{
+     virt_cpufreq_set_perf(policy);
+     return target_freq;
+}
+
+static int virt_cpufreq_target_index(struct cpufreq_policy *policy,
+             unsigned int index)
+{
+     return virt_cpufreq_set_perf(policy);
+}
+
+static int virt_cpufreq_cpu_init(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *drv_data = cpufreq_get_driver_data();
+     struct cpufreq_frequency_table *table;
+     struct device *cpu_dev;
+     int ret;
+
+     cpu_dev = get_cpu_device(policy->cpu);
+     if (!cpu_dev)
+             return -ENODEV;
+
+     ret = dev_pm_opp_of_add_table(cpu_dev);
+     if (ret)
+             return ret;
+
+     ret = dev_pm_opp_get_opp_count(cpu_dev);
+     if (ret <= 0) {
+             dev_err(cpu_dev, "OPP table can't be empty\n");
+             return -ENODEV;
+     }
+
+     ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &table);
+     if (ret) {
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 128

Quoted 6 lines
drivers/cpufreq/virtual-cpufreq.c new file +6 -0
+             dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
+             return ret;
+     }
+
+     policy->freq_table = table;
+     policy->dvfs_possible_from_any_cpu = false;

Why can't we call virt_cpufreq_target_index() from any CPU ?

This is mainly an optimization to reduce the latency of the "frequency change" which has a huge impact on the performance (as can be seen from the numbers in the cover letter).

Setting this flag means that the vCPU thread triggering the MMIO handling (on the host side) is the thread on which the host needs to apply any uclamp settings. So this avoids the VMM having to look up the right vCPU thread that corresponds to this CPU, and any permissions issues wrt setting another threads uclamp, etc. This becomes even more important if/when BPF support is added for handling simple MMIO read/writes. Will Deacon has been working on the eBPF part[1] and IIUC, not setting this flag adds a lot of extra overhead on the BPF side.

So, yeah, this flag is very helpful wrt reducing latency/simplifying host side implementation and that's why we want it here.

[1] - https://kvm-forum.qemu.org/2023/talk/AZKC77/

Thanks,
Saravana

Quoted 117 lines
Quoted 91 lines
drivers/cpufreq/virtual-cpufreq.c new file +90 -0
+     policy->fast_switch_possible = true;
+     policy->driver_data = drv_data;
+
+     /*
+      * Only takes effect if another FIE source such as AMUs
+      * have not been registered.
+      */
+     topology_set_scale_freq_source(&virt_sfd, policy->cpus);
+
+     return 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;
+     return 0;
+}
+
+static int virt_cpufreq_online(struct cpufreq_policy *policy)
+{
+     /* Nothing to restore. */
+     return 0;
+}
+
+static int virt_cpufreq_offline(struct cpufreq_policy *policy)
+{
+     /* Dummy offline() to avoid exit() being called and freeing resources. */
+     return 0;
+}
+
+static struct cpufreq_driver cpufreq_virt_driver = {
+     .name           = "virt-cpufreq",
+     .init           = virt_cpufreq_cpu_init,
+     .exit           = virt_cpufreq_cpu_exit,
+     .online         = virt_cpufreq_online,
+     .offline        = virt_cpufreq_offline,
+     .verify         = cpufreq_generic_frequency_table_verify,
+     .target_index   = virt_cpufreq_target_index,
+     .fast_switch    = virt_cpufreq_fast_switch,
+     .attr           = cpufreq_generic_attr,
+};
+
+static int virt_cpufreq_driver_probe(struct platform_device *pdev)
+{
+     int ret;
+     struct virt_cpufreq_drv_data *drv_data;
+
+     drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
+     if (!drv_data)
+             return -ENOMEM;
+
+     drv_data->ops = of_device_get_match_data(&pdev->dev);
+     if (!drv_data->ops)
+             return -EINVAL;
+
+     drv_data->base = devm_platform_ioremap_resource(pdev, 0);
+     if (IS_ERR(drv_data->base))
+             return PTR_ERR(drv_data->base);
+
+     cpufreq_virt_driver.driver_data = drv_data;
+
+     ret = cpufreq_register_driver(&cpufreq_virt_driver);
+     if (ret) {
+             dev_err(&pdev->dev, "Virtual CPUFreq driver failed to register: %d\n", ret);
+             return ret;
+     }
+
+     dev_dbg(&pdev->dev, "Virtual CPUFreq driver initialized\n");
+     return 0;
+}
+
+static int virt_cpufreq_driver_remove(struct platform_device *pdev)
+{
+     cpufreq_unregister_driver(&cpufreq_virt_driver);
+     return 0;
+}
+
+static const struct of_device_id virt_cpufreq_match[] = {
+     { .compatible = "virtual,cpufreq", .data = &virt_freq_ops},
+     {}
+};
+MODULE_DEVICE_TABLE(of, virt_cpufreq_match);
+
+static struct platform_driver virt_cpufreq_driver = {
+     .probe = virt_cpufreq_driver_probe,
+     .remove = virt_cpufreq_driver_remove,
+     .driver = {

drivers/cpufreq/virtual-cpufreq.c line 228

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+             .name = "virt-cpufreq",
+             .of_match_table = virt_cpufreq_match,
+     },
+};
+
+static int __init virt_cpufreq_init(void)
+{
+     return platform_driver_register(&virt_cpufreq_driver);
+}
+postcore_initcall(virt_cpufreq_init);

Why do you want to use this and not module_init() ? Then you can simply use module_platform_driver().

drivers/cpufreq/virtual-cpufreq.c line 237

Quoted 9 lines
drivers/cpufreq/virtual-cpufreq.c new file +9 -0
+
+static void __exit virt_cpufreq_exit(void)
+{
+     platform_driver_unregister(&virt_cpufreq_driver);
+}
+module_exit(virt_cpufreq_exit);
+
+MODULE_DESCRIPTION("Virtual cpufreq driver");
+MODULE_LICENSE("GPL");

--
viresh

#18 lore raw

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

In reply to David Dai
Quoted 16 lines · 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.

[...]

Quoted 7 lines
drivers/cpufreq/virtual-cpufreq.c new file +7 -0
+static void virt_scale_freq_tick(void)
+{
+	struct cpufreq_policy *policy = cpufreq_cpu_get(smp_processor_id());
+	struct virt_cpufreq_drv_data *data = policy->driver_data;
+	u32 max_freq = (u32)policy->cpuinfo.max_freq;
+	u64 cur_freq;
+	u64 scale;

drivers/cpufreq/virtual-cpufreq.c line 68

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+
+	cpufreq_cpu_put(policy);
+
+	cur_freq = (u64)data->ops->get_freq(policy);
+	cur_freq <<= SCHED_CAPACITY_SHIFT;
+	scale = div_u64(cur_freq, max_freq);
+
+	this_cpu_write(arch_freq_scale, (unsigned long)scale);
+}
+

We expect the host to provide the frequency in kHz, can you please add a comment about it. It is not very obvious when you look at the REG_CUR_FREQ_OFFSET register name.

Quoted 7 lines
drivers/cpufreq/virtual-cpufreq.c new file +7 -0
+static struct scale_freq_data virt_sfd = {
+	.source = SCALE_FREQ_SOURCE_VIRT,
+	.set_freq_scale = virt_scale_freq_tick,
+};
+
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{

drivers/cpufreq/virtual-cpufreq.c line 85

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+	struct virt_cpufreq_drv_data *data = policy->driver_data;
+	/*
+	 * Use cached frequency to avoid rounding to freq table entries
+	 * and undo 25% frequency boost applied by schedutil.
+	 */
+	u32 freq = mult_frac(policy->cached_target_freq, 80, 100);
+
+	data->ops->set_freq(policy, freq);
+	return 0;
+}

Why do we undo the frequency boost? A governor may apply other boosts like RT (uclamp), iowait. It is not clear why we need to worry about governor policies here.

Quoted 46 lines
drivers/cpufreq/virtual-cpufreq.c new file +46 -0
+
+static unsigned int virt_cpufreq_fast_switch(struct cpufreq_policy *policy,
+		unsigned int target_freq)
+{
+	virt_cpufreq_set_perf(policy);
+	return target_freq;
+}
+
+static int virt_cpufreq_target_index(struct cpufreq_policy *policy,
+		unsigned int index)
+{
+	return virt_cpufreq_set_perf(policy);
+}
+
+static int virt_cpufreq_cpu_init(struct cpufreq_policy *policy)
+{
+	struct virt_cpufreq_drv_data *drv_data = cpufreq_get_driver_data();
+	struct cpufreq_frequency_table *table;
+	struct device *cpu_dev;
+	int ret;
+
+	cpu_dev = get_cpu_device(policy->cpu);
+	if (!cpu_dev)
+		return -ENODEV;
+
+	ret = dev_pm_opp_of_add_table(cpu_dev);
+	if (ret)
+		return ret;
+
+	ret = dev_pm_opp_get_opp_count(cpu_dev);
+	if (ret <= 0) {
+		dev_err(cpu_dev, "OPP table can't be empty\n");
+		return -ENODEV;
+	}
+
+	ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &table);
+	if (ret) {
+		dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
+		return ret;
+	}
+
+	policy->freq_table = table;
+	policy->dvfs_possible_from_any_cpu = false;
+	policy->fast_switch_possible = true;
+	policy->driver_data = drv_data;
+

drivers/cpufreq/virtual-cpufreq.c line 141

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+	/*
+	 * Only takes effect if another FIE source such as AMUs
+	 * have not been registered.
+	 */
+	topology_set_scale_freq_source(&virt_sfd, policy->cpus);
+
+	return 0;
+
+}
+

Do we need to register as FIE source even with the below commit? By registering as a source, we are not supplying any accurate metric. We still fallback on the same source that cpufreq implements it.

874f63531064 ("cpufreq: report whether cpufreq supports Frequency Invariance (FI)")

Thanks,
Pavan

maintainer
#11 lore raw

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

In reply to Saravana Kannan
Quoted 14 lines · Saravana Kannan

This is mainly an optimization to reduce the latency of the "frequency change" which has a huge impact on the performance (as can be seen from the numbers in the cover letter).

Setting this flag means that the vCPU thread triggering the MMIO handling (on the host side) is the thread on which the host needs to apply any uclamp settings. So this avoids the VMM having to look up the right vCPU thread that corresponds to this CPU, and any permissions issues wrt setting another threads uclamp, etc. This becomes even more important if/when BPF support is added for handling simple MMIO read/writes. Will Deacon has been working on the eBPF part[1] and IIUC, not setting this flag adds a lot of extra overhead on the BPF side.

Quoted 4 lines

So, yeah, this flag is very helpful wrt reducing latency/simplifying host side implementation and that's why we want it here.

[1] - https://kvm-forum.qemu.org/2023/talk/AZKC77/

Would be good to have a (big) comment in the code explaining that as it isn't obvious. Thanks.

Signature · 1 line
viresh
author
#13 lore raw

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

In reply to Viresh Kumar

Hi Viresh,

Thanks for reviewing!

Quoted 28 lines · Viresh Kumar
Quoted 26 lines · David Dai
drivers/cpufreq/virtual-cpufreq.c new file +20 -0
new file mode 100644
index 000000000000..66b0fd9b821c
@@ -0,0 +1,237 @@
+// 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_OFFSET 0x0
+#define REG_SET_FREQ_OFFSET 0x4
+#define PER_CPU_OFFSET 0x8
+
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 24

Quoted 4 lines
drivers/cpufreq/virtual-cpufreq.c new file +4 -0
+struct virt_cpufreq_ops {
+     void (*set_freq)(struct cpufreq_policy *policy, u32 freq);
+     u32 (*get_freq)(struct cpufreq_policy *policy);
+};

Since you only have one implementation currently, this isn't really required. Keep the data as NULL in virt_cpufreq_match and use writel/readl directly.

Okay, I’ll remove the ops for now and bring it back in the future if required.

Quoted 17 lines

This can be updated if we need more implementations later.

Quoted 14 lines
drivers/cpufreq/virtual-cpufreq.c new file +14 -0
+struct virt_cpufreq_drv_data {
+     void __iomem *base;
+     const struct virt_cpufreq_ops *ops;
+};
+
+static void virt_cpufreq_set_freq(struct cpufreq_policy *policy, u32 freq)
+{
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+
+     writel_relaxed(freq, data->base + policy->cpu * PER_CPU_OFFSET
+                     + REG_SET_FREQ_OFFSET);
+}
+
+static u32 virt_cpufreq_get_freq(struct cpufreq_policy *policy)
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 44

Quoted 5 lines
drivers/cpufreq/virtual-cpufreq.c new file +5 -0
+{
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+
+     return readl_relaxed(data->base + policy->cpu * PER_CPU_OFFSET
+                     + REG_CUR_FREQ_OFFSET);

This doesn't look properly aligned. Please run checkpatch (--strict (optional)).

Ok.

Quoted 183 lines
Quoted 75 lines
drivers/cpufreq/virtual-cpufreq.c new file +74 -0
+}
+
+static const struct virt_cpufreq_ops virt_freq_ops = {
+     .set_freq = virt_cpufreq_set_freq,
+     .get_freq = virt_cpufreq_get_freq,
+};
+
+static void virt_scale_freq_tick(void)
+{
+     struct cpufreq_policy *policy = cpufreq_cpu_get(smp_processor_id());
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+     u32 max_freq = (u32)policy->cpuinfo.max_freq;
+     u64 cur_freq;
+     u64 scale;
+
+     cpufreq_cpu_put(policy);
+
+     cur_freq = (u64)data->ops->get_freq(policy);
+     cur_freq <<= SCHED_CAPACITY_SHIFT;
+     scale = div_u64(cur_freq, max_freq);
+
+     this_cpu_write(arch_freq_scale, (unsigned long)scale);
+}
+
+static struct scale_freq_data virt_sfd = {
+     .source = SCALE_FREQ_SOURCE_VIRT,
+     .set_freq_scale = virt_scale_freq_tick,
+};
+
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+     /*
+      * Use cached frequency to avoid rounding to freq table entries
+      * and undo 25% frequency boost applied by schedutil.
+      */
+     u32 freq = mult_frac(policy->cached_target_freq, 80, 100);
+
+     data->ops->set_freq(policy, freq);
+     return 0;
+}
+
+static unsigned int virt_cpufreq_fast_switch(struct cpufreq_policy *policy,
+             unsigned int target_freq)
+{
+     virt_cpufreq_set_perf(policy);
+     return target_freq;
+}
+
+static int virt_cpufreq_target_index(struct cpufreq_policy *policy,
+             unsigned int index)
+{
+     return virt_cpufreq_set_perf(policy);
+}
+
+static int virt_cpufreq_cpu_init(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *drv_data = cpufreq_get_driver_data();
+     struct cpufreq_frequency_table *table;
+     struct device *cpu_dev;
+     int ret;
+
+     cpu_dev = get_cpu_device(policy->cpu);
+     if (!cpu_dev)
+             return -ENODEV;
+
+     ret = dev_pm_opp_of_add_table(cpu_dev);
+     if (ret)
+             return ret;
+
+     ret = dev_pm_opp_get_opp_count(cpu_dev);
+     if (ret <= 0) {
+             dev_err(cpu_dev, "OPP table can't be empty\n");
+             return -ENODEV;

drivers/cpufreq/virtual-cpufreq.c line 128

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+     }
+
+     ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &table);
+     if (ret) {
+             dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
+             return ret;
+     }
+
+     policy->freq_table = table;
+     policy->dvfs_possible_from_any_cpu = false;

Why can't we call virt_cpufreq_target_index() from any CPU ?

Quoted 95 lines
drivers/cpufreq/virtual-cpufreq.c new file +95 -0
+     policy->fast_switch_possible = true;
+     policy->driver_data = drv_data;
+
+     /*
+      * Only takes effect if another FIE source such as AMUs
+      * have not been registered.
+      */
+     topology_set_scale_freq_source(&virt_sfd, policy->cpus);
+
+     return 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;
+     return 0;
+}
+
+static int virt_cpufreq_online(struct cpufreq_policy *policy)
+{
+     /* Nothing to restore. */
+     return 0;
+}
+
+static int virt_cpufreq_offline(struct cpufreq_policy *policy)
+{
+     /* Dummy offline() to avoid exit() being called and freeing resources. */
+     return 0;
+}
+
+static struct cpufreq_driver cpufreq_virt_driver = {
+     .name           = "virt-cpufreq",
+     .init           = virt_cpufreq_cpu_init,
+     .exit           = virt_cpufreq_cpu_exit,
+     .online         = virt_cpufreq_online,
+     .offline        = virt_cpufreq_offline,
+     .verify         = cpufreq_generic_frequency_table_verify,
+     .target_index   = virt_cpufreq_target_index,
+     .fast_switch    = virt_cpufreq_fast_switch,
+     .attr           = cpufreq_generic_attr,
+};
+
+static int virt_cpufreq_driver_probe(struct platform_device *pdev)
+{
+     int ret;
+     struct virt_cpufreq_drv_data *drv_data;
+
+     drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
+     if (!drv_data)
+             return -ENOMEM;
+
+     drv_data->ops = of_device_get_match_data(&pdev->dev);
+     if (!drv_data->ops)
+             return -EINVAL;
+
+     drv_data->base = devm_platform_ioremap_resource(pdev, 0);
+     if (IS_ERR(drv_data->base))
+             return PTR_ERR(drv_data->base);
+
+     cpufreq_virt_driver.driver_data = drv_data;
+
+     ret = cpufreq_register_driver(&cpufreq_virt_driver);
+     if (ret) {
+             dev_err(&pdev->dev, "Virtual CPUFreq driver failed to register: %d\n", ret);
+             return ret;
+     }
+
+     dev_dbg(&pdev->dev, "Virtual CPUFreq driver initialized\n");
+     return 0;
+}
+
+static int virt_cpufreq_driver_remove(struct platform_device *pdev)
+{
+     cpufreq_unregister_driver(&cpufreq_virt_driver);
+     return 0;
+}
+
+static const struct of_device_id virt_cpufreq_match[] = {
+     { .compatible = "virtual,cpufreq", .data = &virt_freq_ops},
+     {}
+};
+MODULE_DEVICE_TABLE(of, virt_cpufreq_match);
+
+static struct platform_driver virt_cpufreq_driver = {
+     .probe = virt_cpufreq_driver_probe,
+     .remove = virt_cpufreq_driver_remove,
+     .driver = {
+             .name = "virt-cpufreq",
+             .of_match_table = virt_cpufreq_match,
+     },
+};
+
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 228

Quoted 5 lines
drivers/cpufreq/virtual-cpufreq.c new file +5 -0
+static int __init virt_cpufreq_init(void)
+{
+     return platform_driver_register(&virt_cpufreq_driver);
+}
+postcore_initcall(virt_cpufreq_init);

Why do you want to use this and not module_init() ? Then you can simply use module_platform_driver().

We found that using postcore_init over module_init results in a small(2-3%) but measurable benefit during boot time for VMs, so this is an optimization that I’d prefer to keep.

Thanks,
David

Quoted 13 lines

drivers/cpufreq/virtual-cpufreq.c line 237

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +9 -0
+
+static void __exit virt_cpufreq_exit(void)
+{
+     platform_driver_unregister(&virt_cpufreq_driver);
+}
+module_exit(virt_cpufreq_exit);
+
+MODULE_DESCRIPTION("Virtual cpufreq driver");
+MODULE_LICENSE("GPL");

--
viresh

maintainer
#14 lore raw

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

Quoted 6 lines · David Dai
Quoted 2 lines

Why do you want to use this and not module_init() ? Then you can simply use module_platform_driver().

We found that using postcore_init over module_init results in a small(2-3%) but measurable benefit during boot time for VMs, so this is an optimization that I’d prefer to keep.

Okay. That's what platforms normally do (kick in cpufreq support earlier), so we can boot at a higher frequency. Just wasn't sure if it matters for this driver too.

Signature · 1 line
viresh
#17 lore raw

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

In reply to Quentin Perret
Quoted 16 lines · Quentin Perret

Hi David,

drivers/cpufreq/virtual-cpufreq.c line 80

Quoted 7 lines · David Dai
drivers/cpufreq/virtual-cpufreq.c new file +7 -0
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+     /*
+      * Use cached frequency to avoid rounding to freq table entries
+      * and undo 25% frequency boost applied by schedutil.
+      */

The VMM would be a better place for this scaling I think, the driver can't/shouldn't make assumptions about the governor it is running with given that this is a guest userspace decision essentially.

Quoted 4 lines

IIRC the fast_switch() path is only used by schedutil, so one could probably make a case to scale things there, but it'd be inconsistent with the "slow" switch case, and would create a fragile dependency, so it's probably not worth pursuing.

Thanks for the input Quentin!

David and I spend several hours over several days discussing this. We were trying to think through and decide if we were really removing the 25% margin applied by the guest side schedutil or the host side schedutil. We ran through different thought experiments on what would happen if the guest used ondemand/conservative/performance/powersave governors and what if in the future we had a configurable schedutil margin.

We changed our opinions multiple times until we finally remembered this goal from my original presentation[1]:

"On an idle host, running the use case in the host vs VM, should result in close to identical DVFS behavior of the physical CPUs and CPU selection for the threads."

For that statement to be true when the guest uses ondemand/conservative governor, we have to remove the 25% margin applied by the host side schedutil governor. Otherwise, running the workload on the VM will result in frequencies 25% higher than running the same load on the host with ondemand/conservative governor.

So, we finally concluded that we are really undoing the host side schedutil margin. And in that case, it makes sense to undo this in the VMM side. So, we'll go with your suggestion in this email instead of making the schedutil margin to be 0 for the guest.

[1] - https://lpc.events/event/16/contributions/1195/attachments/970/1893/LPC%202022%20-%20VM%20DVFS.pdf

Thanks,
Saravana

Quoted 9 lines

drivers/cpufreq/virtual-cpufreq.c line 85

Quoted 6 lines
drivers/cpufreq/virtual-cpufreq.c new file +5 -0
+     u32 freq = mult_frac(policy->cached_target_freq, 80, 100);
+
+     data->ops->set_freq(policy, freq);
+     return 0;
+}

Thanks,
Quentin

#12 lore raw

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

In reply to Viresh Kumar
Quoted 16 lines · Viresh Kumar
Quoted 14 lines · Saravana Kannan

This is mainly an optimization to reduce the latency of the "frequency change" which has a huge impact on the performance (as can be seen from the numbers in the cover letter).

Setting this flag means that the vCPU thread triggering the MMIO handling (on the host side) is the thread on which the host needs to apply any uclamp settings. So this avoids the VMM having to look up the right vCPU thread that corresponds to this CPU, and any permissions issues wrt setting another threads uclamp, etc. This becomes even more important if/when BPF support is added for handling simple MMIO read/writes. Will Deacon has been working on the eBPF part[1] and IIUC, not setting this flag adds a lot of extra overhead on the BPF side.

Quoted 7 lines
Quoted 4 lines

So, yeah, this flag is very helpful wrt reducing latency/simplifying host side implementation and that's why we want it here.

[1] - https://kvm-forum.qemu.org/2023/talk/AZKC77/

Would be good to have a (big) comment in the code explaining that as it isn't obvious. Thanks.

Will do.

Thanks,
Saravana

author
#19 lore raw

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

In reply to Pavan Kondeti

Hi Pavan,

Thanks for reviewing!

Quoted 34 lines · Pavan Kondeti
Quoted 16 lines · 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.

[...]

Quoted 13 lines
drivers/cpufreq/virtual-cpufreq.c new file +13 -0
+static void virt_scale_freq_tick(void)
+{
+     struct cpufreq_policy *policy = cpufreq_cpu_get(smp_processor_id());
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+     u32 max_freq = (u32)policy->cpuinfo.max_freq;
+     u64 cur_freq;
+     u64 scale;
+
+     cpufreq_cpu_put(policy);
+
+     cur_freq = (u64)data->ops->get_freq(policy);
+     cur_freq <<= SCHED_CAPACITY_SHIFT;
+     scale = div_u64(cur_freq, max_freq);
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 68

Quoted 4 lines
drivers/cpufreq/virtual-cpufreq.c new file +4 -0
+
+     this_cpu_write(arch_freq_scale, (unsigned long)scale);
+}
+

We expect the host to provide the frequency in kHz, can you please add a comment about it. It is not very obvious when you look at the REG_CUR_FREQ_OFFSET register name.

I’ll include a KHZ in the offset names.

Quoted 14 lines
Quoted 14 lines
drivers/cpufreq/virtual-cpufreq.c new file +13 -0
+static struct scale_freq_data virt_sfd = {
+     .source = SCALE_FREQ_SOURCE_VIRT,
+     .set_freq_scale = virt_scale_freq_tick,
+};
+
+static unsigned int virt_cpufreq_set_perf(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+     /*
+      * Use cached frequency to avoid rounding to freq table entries
+      * and undo 25% frequency boost applied by schedutil.
+      */
+     u32 freq = mult_frac(policy->cached_target_freq, 80, 100);
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 85

Quoted 4 lines
drivers/cpufreq/virtual-cpufreq.c new file +4 -0
+
+     data->ops->set_freq(policy, freq);
+     return 0;
+}

Why do we undo the frequency boost? A governor may apply other boosts like RT (uclamp), iowait. It is not clear why we need to worry about governor policies here.

See Saravana’s response to Quentin for more details, but in short, we’ll remove this particular snippet in the driver.

Quoted 53 lines
Quoted 53 lines
drivers/cpufreq/virtual-cpufreq.c new file +52 -0
+
+static unsigned int virt_cpufreq_fast_switch(struct cpufreq_policy *policy,
+             unsigned int target_freq)
+{
+     virt_cpufreq_set_perf(policy);
+     return target_freq;
+}
+
+static int virt_cpufreq_target_index(struct cpufreq_policy *policy,
+             unsigned int index)
+{
+     return virt_cpufreq_set_perf(policy);
+}
+
+static int virt_cpufreq_cpu_init(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *drv_data = cpufreq_get_driver_data();
+     struct cpufreq_frequency_table *table;
+     struct device *cpu_dev;
+     int ret;
+
+     cpu_dev = get_cpu_device(policy->cpu);
+     if (!cpu_dev)
+             return -ENODEV;
+
+     ret = dev_pm_opp_of_add_table(cpu_dev);
+     if (ret)
+             return ret;
+
+     ret = dev_pm_opp_get_opp_count(cpu_dev);
+     if (ret <= 0) {
+             dev_err(cpu_dev, "OPP table can't be empty\n");
+             return -ENODEV;
+     }
+
+     ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &table);
+     if (ret) {
+             dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
+             return ret;
+     }
+
+     policy->freq_table = table;
+     policy->dvfs_possible_from_any_cpu = false;
+     policy->fast_switch_possible = true;
+     policy->driver_data = drv_data;
+
+     /*
+      * Only takes effect if another FIE source such as AMUs
+      * have not been registered.
+      */
+     topology_set_scale_freq_source(&virt_sfd, policy->cpus);
+
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 93

Quoted 4 lines
drivers/cpufreq/virtual-cpufreq.c new file +4 -0
+     return 0;
+
+}
+

Do we need to register as FIE source even with the below commit? By registering as a source, we are not supplying any accurate metric. We still fallback on the same source that cpufreq implements it.

The arch_set_freq_scale() done at cpufreq driver’s frequency updates at cpufreq_freq_transition_end() and cpufreq_driver_fast_switch() only represent the guest’s frequency request. However, this does not accurately represent the physical CPU’s frequency that the vCPU is running on. E.g. There may be other processes sharing the same physical CPU that results in a much higher CPU frequency than what’s requested by the vCPU.

Thanks,
David

Quoted 10 lines

874f63531064 ("cpufreq: report whether cpufreq supports Frequency Invariance (FI)")

Thanks,
Pavan

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

#3 lore raw

Re: [PATCH v3 1/2] dt-bindings: cpufreq: add bindings for virtual cpufreq

In reply to David Dai
Quoted 5 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 CPU.

A nit, subject: drop second/last, redundant "bindings for". The "dt-bindings" prefix is already stating that these are bindings.

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

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

Quoted 10 lines
Documentation/devicetree/bindings/cpufreq/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. The host
+  uses the hint to schedule vCPU threads and select physical CPU frequency. It
+  enables accurate Per-Entity Load Tracking for tasks running in the guest by
+  querying host CPU frequency unless a virtualized FIE (ex. AMU) exists.

Why do you need DT for this? You control hypervisor, thus control the interface to the guest. I think Rob made it pretty clear that discoverable usecases (which is yours) are not for DT.

Incomplete style-review follows:

Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml line 22

Quoted 4 lines
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +4 -0
+
+properties:
+  compatible:
+    const: virtual,cpufreq

Missing blank line.

Quoted 53 lines
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +53 -0
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    cpus {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      cpu@0 {
+        compatible = "arm,arm-v8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table0>;
+      };
+
+      cpu@1 {
+        compatible = "arm,arm-v8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table1>;
+      };
+    };
+
+    opp_table0: opp-table-0 {
+      compatible = "operating-points-v2";
+
+      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";
+
+      opp1106000000 {
+        opp-hz = /bits/ 64 <1106000000>;
+        opp-level = <1>;
+      };
+
+      opp1277000000 {

Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml line 85

Quoted 10 lines
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +10 -0
+        opp-hz = /bits/ 64 <1277000000>;
+        opp-level = <2>;
+      };
+    };
+
+    soc {
+      #address-cells = <1>;
+      #size-cells = <1>;
+
+      cpufreq {

Missing unit address

Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml line 87

Quoted 2 lines
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +2 -0
+        reg = <0x1040000 0x10>;
+        compatible = "virtual,cpufreq";

compatible is always the first property.

Also, you did not test it...

Best regards,
Krzysztof

#20 lore raw

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

In reply to David Dai
Quoted 48 lines · David Dai

Hi Pavan,

Thanks for reviewing!

Quoted 34 lines · Pavan Kondeti
Quoted 16 lines · 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.

[...]

Quoted 13 lines
drivers/cpufreq/virtual-cpufreq.c new file +13 -0
+static void virt_scale_freq_tick(void)
+{
+     struct cpufreq_policy *policy = cpufreq_cpu_get(smp_processor_id());
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+     u32 max_freq = (u32)policy->cpuinfo.max_freq;
+     u64 cur_freq;
+     u64 scale;
+
+     cpufreq_cpu_put(policy);
+
+     cur_freq = (u64)data->ops->get_freq(policy);
+     cur_freq <<= SCHED_CAPACITY_SHIFT;
+     scale = div_u64(cur_freq, max_freq);
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 68

Quoted 4 lines
drivers/cpufreq/virtual-cpufreq.c new file +4 -0
+
+     this_cpu_write(arch_freq_scale, (unsigned long)scale);
+}
+

We expect the host to provide the frequency in kHz, can you please add a comment about it. It is not very obvious when you look at the REG_CUR_FREQ_OFFSET register name.

Quoted 2 lines

I’ll include a KHZ in the offset names.

Sure, that would help. Also, can you limit the scale to SCHED_CAPACITY_SCALE? It may be possible that host may be running at a higher frequency than max_freq advertised on this guest.

Quoted 62 lines
Quoted 53 lines
Quoted 53 lines
drivers/cpufreq/virtual-cpufreq.c new file +52 -0
+
+static unsigned int virt_cpufreq_fast_switch(struct cpufreq_policy *policy,
+             unsigned int target_freq)
+{
+     virt_cpufreq_set_perf(policy);
+     return target_freq;
+}
+
+static int virt_cpufreq_target_index(struct cpufreq_policy *policy,
+             unsigned int index)
+{
+     return virt_cpufreq_set_perf(policy);
+}
+
+static int virt_cpufreq_cpu_init(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *drv_data = cpufreq_get_driver_data();
+     struct cpufreq_frequency_table *table;
+     struct device *cpu_dev;
+     int ret;
+
+     cpu_dev = get_cpu_device(policy->cpu);
+     if (!cpu_dev)
+             return -ENODEV;
+
+     ret = dev_pm_opp_of_add_table(cpu_dev);
+     if (ret)
+             return ret;
+
+     ret = dev_pm_opp_get_opp_count(cpu_dev);
+     if (ret <= 0) {
+             dev_err(cpu_dev, "OPP table can't be empty\n");
+             return -ENODEV;
+     }
+
+     ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &table);
+     if (ret) {
+             dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
+             return ret;
+     }
+
+     policy->freq_table = table;
+     policy->dvfs_possible_from_any_cpu = false;
+     policy->fast_switch_possible = true;
+     policy->driver_data = drv_data;
+
+     /*
+      * Only takes effect if another FIE source such as AMUs
+      * have not been registered.
+      */
+     topology_set_scale_freq_source(&virt_sfd, policy->cpus);
+
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 93

Quoted 4 lines
drivers/cpufreq/virtual-cpufreq.c new file +4 -0
+     return 0;
+
+}
+

Do we need to register as FIE source even with the below commit? By registering as a source, we are not supplying any accurate metric. We still fallback on the same source that cpufreq implements it.

Quoted 8 lines

The arch_set_freq_scale() done at cpufreq driver’s frequency updates at cpufreq_freq_transition_end() and cpufreq_driver_fast_switch() only represent the guest’s frequency request. However, this does not accurately represent the physical CPU’s frequency that the vCPU is running on. E.g. There may be other processes sharing the same physical CPU that results in a much higher CPU frequency than what’s requested by the vCPU.

understood that policy->cur may not reflect the actual frequency. Is this something needs to be advertised to cpufreq core so that it query the underlying cpufreq driver and use it for frequency scale updates. This also gives userspace to read the actual frequency when read from sysfs.

In fact, cpufreq_driver_fast_switch() comment says that cpufreq_driver::fast_switch() should return the *actual* frequency and the same is used to update frequency scale updates. I understand that it depends on other things like if host defer the frequency switch, the value read from REG_CUR_FREQ_OFFSET may reflect the old value..

May be a comment in code would help.

Thanks,
Pavan

#4 lore raw

Re: [PATCH v3 1/2] dt-bindings: cpufreq: add bindings for virtual cpufreq

In reply to Krzysztof Kozlowski
Quoted 124 lines · Krzysztof Kozlowski
Quoted 5 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 CPU.

A nit, subject: drop second/last, redundant "bindings for". The "dt-bindings" prefix is already stating that these are bindings.

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

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

Quoted 10 lines
Documentation/devicetree/bindings/cpufreq/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. The host
+  uses the hint to schedule vCPU threads and select physical CPU frequency. It
+  enables accurate Per-Entity Load Tracking for tasks running in the guest by
+  querying host CPU frequency unless a virtualized FIE (ex. AMU) exists.

Why do you need DT for this? You control hypervisor, thus control the interface to the guest. I think Rob made it pretty clear that discoverable usecases (which is yours) are not for DT.

Incomplete style-review follows:

Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml line 22

Quoted 4 lines
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +4 -0
+
+properties:
+  compatible:
+    const: virtual,cpufreq

Missing blank line.

Quoted 53 lines
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +53 -0
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    cpus {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      cpu@0 {
+        compatible = "arm,arm-v8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table0>;
+      };
+
+      cpu@1 {
+        compatible = "arm,arm-v8";
+        device_type = "cpu";
+        reg = <0x0>;
+        operating-points-v2 = <&opp_table1>;
+      };
+    };
+
+    opp_table0: opp-table-0 {
+      compatible = "operating-points-v2";
+
+      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";
+
+      opp1106000000 {
+        opp-hz = /bits/ 64 <1106000000>;
+        opp-level = <1>;
+      };
+
+      opp1277000000 {

Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml line 85

Quoted 10 lines
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +10 -0
+        opp-hz = /bits/ 64 <1277000000>;
+        opp-level = <2>;
+      };
+    };
+
+    soc {
+      #address-cells = <1>;
+      #size-cells = <1>;
+
+      cpufreq {

Missing unit address

Quoted 6 lines

Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml line 87

Quoted 2 lines
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +2 -0
+        reg = <0x1040000 0x10>;
+        compatible = "virtual,cpufreq";

compatible is always the first property.

Also, you did not test it...

Why do you say this? This patch series was obviously tested very well with all the data we collected.

-Saravana

#5 lore raw

Re: [PATCH v3 1/2] dt-bindings: cpufreq: add bindings for virtual cpufreq

Quoted 10 lines · Saravana Kannan
Quoted 7 lines

Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml line 87

Quoted 3 lines
Documentation/devicetree/bindings/cpufreq/cpufreq-virtual.yaml new file +2 -0
+        reg = <0x1040000 0x10>;
+        compatible = "virtual,cpufreq";

compatible is always the first property.

Also, you did not test it...

Why do you say this? This patch series was obviously tested very well with all the data we collected.

Why do I say? Because of warning and huge fat Python exception? Test it... you will see.

Best regards,
Krzysztof

#22 lore raw

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

In reply to David Dai
kernel test robot noticed the following build errors:

Hi David,

kernel test robot noticed the following build errors:

[auto build test ERROR on rafael-pm/linux-next]
[also build test ERROR on robh/for-next linus/master v6.5-rc5 next-20230809] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Dai/dt-bindings-cpufreq-add-bindings-for-virtual-cpufreq/20230801-014946
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
patch link:    https://lore.kernel.org/r/20230731174613.4133167-3-davidai%40google.com
patch subject: [PATCH v3 2/2] cpufreq: add virtual-cpufreq driver
config: arm-randconfig-r061-20230812 (https://download.01.org/0day-ci/archive/20230812/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230812/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

arm-linux-gnueabi-ld: drivers/cpufreq/virtual-cpufreq.o: in function `virt_cpufreq_cpu_exit':

>> virtual-cpufreq.c:(.text+0xf8): undefined reference to `topology_clear_scale_freq_source'
   arm-linux-gnueabi-ld: drivers/cpufreq/virtual-cpufreq.o: in function `virt_cpufreq_cpu_init':
>> virtual-cpufreq.c:(.text+0x1c8): undefined reference to `topology_set_scale_freq_source'
   arm-linux-gnueabi-ld: drivers/cpufreq/virtual-cpufreq.o: in function `virt_scale_freq_tick':
>> virtual-cpufreq.c:(.text+0x330): undefined reference to `arch_freq_scale'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#21 lore raw

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

In reply to Pavan Kondeti
Quoted 50 lines · Pavan Kondeti
Quoted 48 lines · David Dai

Hi Pavan,

Thanks for reviewing!

Quoted 42 lines · Pavan Kondeti
Quoted 16 lines · 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.

[...]

Quoted 7 lines
drivers/cpufreq/virtual-cpufreq.c new file +7 -0
+static void virt_scale_freq_tick(void)
+{
+     struct cpufreq_policy *policy = cpufreq_cpu_get(smp_processor_id());
+     struct virt_cpufreq_drv_data *data = policy->driver_data;
+     u32 max_freq = (u32)policy->cpuinfo.max_freq;
+     u64 cur_freq;
+     u64 scale;

drivers/cpufreq/virtual-cpufreq.c line 68

Quoted 10 lines
drivers/cpufreq/virtual-cpufreq.c new file +10 -0
+
+     cpufreq_cpu_put(policy);
+
+     cur_freq = (u64)data->ops->get_freq(policy);
+     cur_freq <<= SCHED_CAPACITY_SHIFT;
+     scale = div_u64(cur_freq, max_freq);
+
+     this_cpu_write(arch_freq_scale, (unsigned long)scale);
+}
+

We expect the host to provide the frequency in kHz, can you please add a comment about it. It is not very obvious when you look at the REG_CUR_FREQ_OFFSET register name.

Quoted 3 lines
Quoted 2 lines

I’ll include a KHZ in the offset names.

Hi Pavan,

Apologies for the slow responses, I was out on vacation for the week prior to last week.

Quoted 3 lines

Sure, that would help. Also, can you limit the scale to SCHED_CAPACITY_SCALE? It may be possible that host may be running at a higher frequency than max_freq advertised on this guest.

Good catch, will include a check to limit freq_scale to SCHED_CAPACITY_SCALE.

Quoted 72 lines
Quoted 63 lines
Quoted 54 lines
Quoted 54 lines
drivers/cpufreq/virtual-cpufreq.c new file +52 -0
+
+static unsigned int virt_cpufreq_fast_switch(struct cpufreq_policy *policy,
+             unsigned int target_freq)
+{
+     virt_cpufreq_set_perf(policy);
+     return target_freq;
+}
+
+static int virt_cpufreq_target_index(struct cpufreq_policy *policy,
+             unsigned int index)
+{
+     return virt_cpufreq_set_perf(policy);
+}
+
+static int virt_cpufreq_cpu_init(struct cpufreq_policy *policy)
+{
+     struct virt_cpufreq_drv_data *drv_data = cpufreq_get_driver_data();
+     struct cpufreq_frequency_table *table;
+     struct device *cpu_dev;
+     int ret;
+
+     cpu_dev = get_cpu_device(policy->cpu);
+     if (!cpu_dev)
+             return -ENODEV;
+
+     ret = dev_pm_opp_of_add_table(cpu_dev);
+     if (ret)
+             return ret;
+
+     ret = dev_pm_opp_get_opp_count(cpu_dev);
+     if (ret <= 0) {
+             dev_err(cpu_dev, "OPP table can't be empty\n");
+             return -ENODEV;
+     }
+
+     ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &table);
+     if (ret) {
+             dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
+             return ret;
+     }
+
+     policy->freq_table = table;
+     policy->dvfs_possible_from_any_cpu = false;
+     policy->fast_switch_possible = true;
+     policy->driver_data = drv_data;
+
+     /*
+      * Only takes effect if another FIE source such as AMUs
+      * have not been registered.
+      */
+     topology_set_scale_freq_source(&virt_sfd, policy->cpus);
+
Quoted 8 lines

drivers/cpufreq/virtual-cpufreq.c line 93

Quoted 4 lines
drivers/cpufreq/virtual-cpufreq.c new file +4 -0
+     return 0;
+
+}
+

Do we need to register as FIE source even with the below commit? By registering as a source, we are not supplying any accurate metric. We still fallback on the same source that cpufreq implements it.

Quoted 8 lines

The arch_set_freq_scale() done at cpufreq driver’s frequency updates at cpufreq_freq_transition_end() and cpufreq_driver_fast_switch() only represent the guest’s frequency request. However, this does not accurately represent the physical CPU’s frequency that the vCPU is running on. E.g. There may be other processes sharing the same physical CPU that results in a much higher CPU frequency than what’s requested by the vCPU.

Quoted 5 lines

understood that policy->cur may not reflect the actual frequency. Is this something needs to be advertised to cpufreq core so that it query the underlying cpufreq driver and use it for frequency scale updates. This also gives userspace to read the actual frequency when read from sysfs.

Adding a ->get() callback in the driver to advertise to the cpufreq core does not actually update the freq_scale if fast_switch is enabled. Since fast_switch is required for performance reasons, I don’t see value in adding ->get().

Quoted 8 lines

In fact, cpufreq_driver_fast_switch() comment says that cpufreq_driver::fast_switch() should return the *actual* frequency and the same is used to update frequency scale updates. I understand that it depends on other things like if host defer the frequency switch, the value read from REG_CUR_FREQ_OFFSET may reflect the old value..

May be a comment in code would help.

Sounds good, I'll include a comment.

Thanks,
David

Quoted 6 lines

Thanks,
Pavan

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].