Skip to contentSkip to conversation

KVM statistics data fd-based binary interface

PATCH Superseded Jing Zhang 4 participants 29 messages Jun 17, 2021

Subsystems (13)
Documentation x86 KVM PowerPC +9 more subsystems

v10 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Jing Zhang

author
#0 lore raw

v10 cover KVM statistics data fd-based binary interface

This patchset provides a file descriptor for every VM and VCPU to read KVM statistics data in binary format.
It is meant to provide a lightweight, flexible, scalable and efficient lock-free solution for user space telemetry applications to pull the statistics data periodically for large scale systems. The pulling frequency could be as high as a few times per second.
In this patchset, every statistics data are treated to have some attributes as below:
* architecture dependent or generic
* VM statistics data or VCPU statistics data
* type: cumulative, instantaneous, peak
* unit: none for simple counter, nanosecond, microsecond, millisecond, second, Byte, KiByte, MiByte, GiByte, Clock Cycles
Since no lock/synchronization is used, the consistency between all the statistics data is not guaranteed. That means not all statistics data are read out at the exact same time, since the statistics data are still being updated by KVM subsystems while they are read out.

---

* v9 -> v10
- Relocate vcpu stat in vcpu's slab's usercopy region
- Fix test issue for capability checking
- Update commit message to explain why/how we need to add this new API for KVM statistics

* v8 -> v9
- Rebase to commit 8331a2bc0898
(KVM: X86: Introduce KVM_HC_MAP_GPA_RANGE hypercall)
- Reduce code duplication between binary and debugfs interface
- Add field "offset" in stats descriptor to let us define stats descriptors in any order (not necessary in the order of stats defined in vm/vcpu stats structures)
- Add static check to make sure the number of stats descriptors is the same as the number of stats defined in vm/vcpu stats structures
- Fix missing/mismatched stats descriptor definition caused by rebase

* v7 -> v8
- Rebase to kvm/queue, commit c1dc20e254b4 ("KVM: switch per-VM stats to u64")
- Revise code to reflect the per-VM stats type from ulong to u64
- Addressed some other nits

* v6 -> v7
- Improve file descriptor allocation function by Krish suggestion
- Use "generic stats" instead of "common stats" as Krish suggested
- Addressed some other nits from Krish and David Matlack

* v5 -> v6
- Use designated initializers for STATS_DESC
- Change KVM_STATS_SCALE... to KVM_STATS_BASE...
- Use a common function for kvm_[vm|vcpu]_stats_read
- Fix some documentation errors/missings
- Use TEST_ASSERT in selftest
- Use a common function for [vm|vcpu]_stats_test in selftest

* v4 -> v5
- Rebase to kvm/queue, commit a4345a7cecfb ("Merge tag

    'kvmarm-fixes-5.13-1'")

- Change maximum stats name length to 48
- Replace VM_STATS_COMMON/VCPU_STATS_COMMON macros with stats descriptor definition macros.
- Fixed some errors/warnings reported by checkpatch.pl

* v3 -> v4
- Rebase to kvm/queue, commit 9f242010c3b4 ("KVM: avoid "deadlock" between install_new_memslots and MMU notifier")
- Use C-stype comments in the whole patch
- Fix wrong count for x86 VCPU stats descriptors
- Fix KVM stats data size counting and validity check in selftest

* v2 -> v3
- Rebase to kvm/queue, commit edf408f5257b ("KVM: avoid "deadlock" between install_new_memslots and MMU notifier")
- Resolve some nitpicks about format

* v1 -> v2
- Use ARRAY_SIZE to count the number of stats descriptors
- Fix missing size field initialization in macro STATS_DESC

[1] https://lore.kernel.org/kvm/[email protected]
[2] https://lore.kernel.org/kvm/[email protected]
[3] https://lore.kernel.org/kvm/[email protected]
[4] https://lore.kernel.org/kvm/[email protected]
[5] https://lore.kernel.org/kvm/[email protected]
[6] https://lore.kernel.org/kvm/[email protected]
[7] https://lore.kernel.org/kvm/[email protected]
[8] https://lore.kernel.org/kvm/[email protected]
[9] https://lore.kernel.org/kvm/[email protected]

---

Jing Zhang (5):
KVM: stats: Separate generic stats from architecture specific ones
KVM: stats: Add fd-based API to read binary stats data
KVM: stats: Add documentation for binary statistics interface
KVM: selftests: Add selftest for KVM statistics data binary interface
KVM: stats: Remove code duplication for binary and debugfs stats

 Documentation/virt/kvm/api.rst                | 177 +++++++++++-
 arch/arm64/include/asm/kvm_host.h             |   9 +-
 arch/arm64/kvm/guest.c                        |  50 +++-
 arch/mips/include/asm/kvm_host.h              |   9 +-
 arch/mips/kvm/mips.c                          |  92 +++---
 arch/powerpc/include/asm/kvm_host.h           |   9 +-
 arch/powerpc/kvm/book3s.c                     |  93 ++++--
 arch/powerpc/kvm/book3s_hv.c                  |  12 +-
 arch/powerpc/kvm/book3s_pr.c                  |   2 +-
 arch/powerpc/kvm/book3s_pr_papr.c             |   2 +-
 arch/powerpc/kvm/booke.c                      |  78 +++--
 arch/s390/include/asm/kvm_host.h              |   9 +-
 arch/s390/kvm/kvm-s390.c                      | 234 ++++++++-------
 arch/x86/include/asm/kvm_host.h               |   9 +-
 arch/x86/kvm/x86.c                            | 111 ++++---
 include/linux/kvm_host.h                      | 180 +++++++++++-
 include/linux/kvm_types.h                     |  12 +
 include/uapi/linux/kvm.h                      |  48 ++++
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   3 +
 .../testing/selftests/kvm/include/kvm_util.h  |   3 +
 .../selftests/kvm/kvm_binary_stats_test.c     | 225 +++++++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    |  12 +
 virt/kvm/kvm_main.c                           | 270 +++++++++++++++---
 24 files changed, 1299 insertions(+), 351 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/kvm_binary_stats_test.c

base-commit: 8331a2bc089881d7fd2fc9a6658f39780817e4e0

Signature · 1 line
2.32.0.272.g935e593368-goog
author
#1 lore raw

PATCH v10 1/5 KVM: stats: Separate generic stats from architecture specific ones

Put all generic statistics in a separate structure to ease statistics handling for the incoming new statistics API. Generic KVM stats are those collected in architecture independent code or those supported by all architectures.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h   |  9 ++-------
 arch/arm64/kvm/guest.c              | 12 ++++++------
 arch/mips/include/asm/kvm_host.h    |  9 ++-------
 arch/mips/kvm/mips.c                | 12 ++++++------
 arch/powerpc/include/asm/kvm_host.h |  9 ++-------
 arch/powerpc/kvm/book3s.c           | 12 ++++++------
 arch/powerpc/kvm/book3s_hv.c        | 12 ++++++------
 arch/powerpc/kvm/book3s_pr.c        |  2 +-
 arch/powerpc/kvm/book3s_pr_papr.c   |  2 +-
 arch/powerpc/kvm/booke.c            | 14 +++++++-------
 arch/s390/include/asm/kvm_host.h    |  9 ++-------
 arch/s390/kvm/kvm-s390.c            | 12 ++++++------
 arch/x86/include/asm/kvm_host.h     |  9 ++-------
 arch/x86/kvm/x86.c                  | 14 +++++++-------
 include/linux/kvm_host.h            |  9 +++++++--
 include/linux/kvm_types.h           | 12 ++++++++++++
 virt/kvm/kvm_main.c                 | 14 +++++++-------
 17 files changed, 82 insertions(+), 90 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -7
index d56f365b38a8..5a2c82f63baa 100644
@@ -556,16 +556,11 @@ static inline bool __vcpu_write_sys_reg_to_cpu(u64 val, int reg)
 }
 
 struct kvm_vm_stat {
-	u64 remote_tlb_flush;
+	struct kvm_vm_stat_generic generic;
 };
 
 struct kvm_vcpu_stat {
-	u64 halt_successful_poll;
-	u64 halt_attempted_poll;
-	u64 halt_poll_success_ns;
-	u64 halt_poll_fail_ns;
-	u64 halt_poll_invalid;
-	u64 halt_wakeup;
+	struct kvm_vcpu_stat_generic generic;
 	u64 hvc_exit_stat;
 	u64 wfe_exit_stat;
 	u64 wfi_exit_stat;
arch/arm64/kvm/guest.c +6 -6
index 5cb4a1cd5603..4962331d01e6 100644
@@ -29,18 +29,18 @@
 #include "trace.h"
 
 struct kvm_stats_debugfs_item debugfs_entries[] = {
-	VCPU_STAT("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
-	VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
-	VCPU_STAT("halt_wakeup", halt_wakeup),
+	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
+	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
+	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
+	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
 	VCPU_STAT("hvc_exit_stat", hvc_exit_stat),
 	VCPU_STAT("wfe_exit_stat", wfe_exit_stat),
 	VCPU_STAT("wfi_exit_stat", wfi_exit_stat),
 	VCPU_STAT("mmio_exit_user", mmio_exit_user),
 	VCPU_STAT("mmio_exit_kernel", mmio_exit_kernel),
 	VCPU_STAT("exits", exits),
-	VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
+	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
+	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
 	{ NULL }
 };
 
arch/mips/include/asm/kvm_host.h +2 -7
index 4245c082095f..696f6b009377 100644
@@ -109,10 +109,11 @@ static inline bool kvm_is_error_hva(unsigned long addr)
 }
 
 struct kvm_vm_stat {
-	u64 remote_tlb_flush;
+	struct kvm_vm_stat_generic generic;
 };
 
 struct kvm_vcpu_stat {
+	struct kvm_vcpu_stat_generic generic;
 	u64 wait_exits;
 	u64 cache_exits;
 	u64 signal_exits;
@@ -142,12 +143,6 @@ struct kvm_vcpu_stat {
 #ifdef CONFIG_CPU_LOONGSON64
 	u64 vz_cpucfg_exits;
 #endif
-	u64 halt_successful_poll;
-	u64 halt_attempted_poll;
-	u64 halt_poll_success_ns;
-	u64 halt_poll_fail_ns;
-	u64 halt_poll_invalid;
-	u64 halt_wakeup;
 };
 
 struct kvm_arch_memory_slot {
arch/mips/kvm/mips.c +6 -6
index 4d4af97dcc88..ff205b35719b 100644
@@ -68,12 +68,12 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 #ifdef CONFIG_CPU_LOONGSON64
 	VCPU_STAT("vz_cpucfg", vz_cpucfg_exits),
 #endif
-	VCPU_STAT("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
-	VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
-	VCPU_STAT("halt_wakeup", halt_wakeup),
-	VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
+	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
+	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
+	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
+	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
+	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
+	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
 	{NULL}
 };
 
arch/powerpc/include/asm/kvm_host.h +2 -7
index ae3d4af61b66..80cfe4f42894 100644
@@ -81,12 +81,13 @@ struct kvmppc_book3s_shadow_vcpu;
 struct kvm_nested_guest;
 
 struct kvm_vm_stat {
-	u64 remote_tlb_flush;
+	struct kvm_vm_stat_generic generic;
 	u64 num_2M_pages;
 	u64 num_1G_pages;
 };
 
 struct kvm_vcpu_stat {
+	struct kvm_vcpu_stat_generic generic;
 	u64 sum_exits;
 	u64 mmio_exits;
 	u64 signal_exits;
@@ -102,14 +103,8 @@ struct kvm_vcpu_stat {
 	u64 emulated_inst_exits;
 	u64 dec_exits;
 	u64 ext_intr_exits;
-	u64 halt_poll_success_ns;
-	u64 halt_poll_fail_ns;
 	u64 halt_wait_ns;
-	u64 halt_successful_poll;
-	u64 halt_attempted_poll;
 	u64 halt_successful_wait;
-	u64 halt_poll_invalid;
-	u64 halt_wakeup;
 	u64 dbell_exits;
 	u64 gdbell_exits;
 	u64 ld;
arch/powerpc/kvm/book3s.c +6 -6
index 2b691f4d1f26..92cdb4175945 100644
@@ -47,14 +47,14 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 	VCPU_STAT("dec", dec_exits),
 	VCPU_STAT("ext_intr", ext_intr_exits),
 	VCPU_STAT("queue_intr", queue_intr),
-	VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
+	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
+	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
 	VCPU_STAT("halt_wait_ns", halt_wait_ns),
-	VCPU_STAT("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
+	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
+	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
 	VCPU_STAT("halt_successful_wait", halt_successful_wait),
-	VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
-	VCPU_STAT("halt_wakeup", halt_wakeup),
+	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
+	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
 	VCPU_STAT("pf_storage", pf_storage),
 	VCPU_STAT("sp_storage", sp_storage),
 	VCPU_STAT("pf_instruc", pf_instruc),
arch/powerpc/kvm/book3s_hv.c +4 -6
index 7360350e66ff..b3506c35a46b 100644
@@ -236,7 +236,7 @@ static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
 
 	waitp = kvm_arch_vcpu_get_wait(vcpu);
 	if (rcuwait_wake_up(waitp))
-		++vcpu->stat.halt_wakeup;
+		++vcpu->stat.generic.halt_wakeup;
 
 	cpu = READ_ONCE(vcpu->arch.thread_cpu);
 	if (cpu >= 0 && kvmppc_ipi_thread(cpu))
@@ -3925,7 +3925,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 	cur = start_poll = ktime_get();
 	if (vc->halt_poll_ns) {
 		ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
-		++vc->runner->stat.halt_attempted_poll;
+		++vc->runner->stat.generic.halt_attempted_poll;
 
 		vc->vcore_state = VCORE_POLLING;
 		spin_unlock(&vc->lock);
@@ -3942,7 +3942,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 		vc->vcore_state = VCORE_INACTIVE;
 
 		if (!do_sleep) {
-			++vc->runner->stat.halt_successful_poll;
+			++vc->runner->stat.generic.halt_successful_poll;
 			goto out;
 		}
 	}
@@ -3954,7 +3954,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 		do_sleep = 0;
 		/* If we polled, count this as a successful poll */
 		if (vc->halt_poll_ns)
-			++vc->runner->stat.halt_successful_poll;
+			++vc->runner->stat.generic.halt_successful_poll;
 		goto out;
 	}
 
@@ -3981,13 +3981,13 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 			ktime_to_ns(cur) - ktime_to_ns(start_wait);
 		/* Attribute failed poll time */
 		if (vc->halt_poll_ns)
-			vc->runner->stat.halt_poll_fail_ns ++			vc->runner->stat.generic.halt_poll_fail_ns + 				ktime_to_ns(start_wait) -
 				ktime_to_ns(start_poll);
 	} else {
 		/* Attribute successful poll time */
 		if (vc->halt_poll_ns)
-			vc->runner->stat.halt_poll_success_ns ++			vc->runner->stat.generic.halt_poll_success_ns + 				ktime_to_ns(cur) -
 				ktime_to_ns(start_poll);
 	}
arch/powerpc/kvm/book3s_pr.c +1 -1
index d7733b07f489..71bcb0140461 100644
@@ -493,7 +493,7 @@ static void kvmppc_set_msr_pr(struct kvm_vcpu *vcpu, u64 msr)
 		if (!vcpu->arch.pending_exceptions) {
 			kvm_vcpu_block(vcpu);
 			kvm_clear_request(KVM_REQ_UNHALT, vcpu);
-			vcpu->stat.halt_wakeup++;
+			vcpu->stat.generic.halt_wakeup++;
 
 			/* Unset POW bit after we woke up */
 			msr &= ~MSR_POW;
arch/powerpc/kvm/book3s_pr_papr.c +1 -1
index 031c8015864a..ac14239f3424 100644
@@ -378,7 +378,7 @@ int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
 		kvmppc_set_msr_fast(vcpu, kvmppc_get_msr(vcpu) | MSR_EE);
 		kvm_vcpu_block(vcpu);
 		kvm_clear_request(KVM_REQ_UNHALT, vcpu);
-		vcpu->stat.halt_wakeup++;
+		vcpu->stat.generic.halt_wakeup++;
 		return EMULATE_DONE;
 	case H_LOGICAL_CI_LOAD:
 		return kvmppc_h_pr_logical_ci_load(vcpu);
arch/powerpc/kvm/booke.c +7 -7
index 7d5fe43f85c4..80d3b39aa7ac 100644
@@ -49,15 +49,15 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 	VCPU_STAT("inst_emu", emulated_inst_exits),
 	VCPU_STAT("dec", dec_exits),
 	VCPU_STAT("ext_intr", ext_intr_exits),
-	VCPU_STAT("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
-	VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
-	VCPU_STAT("halt_wakeup", halt_wakeup),
+	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
+	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
+	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
+	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
 	VCPU_STAT("doorbell", dbell_exits),
 	VCPU_STAT("guest doorbell", gdbell_exits),
-	VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
-	VM_STAT("remote_tlb_flush", remote_tlb_flush),
+	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
+	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
+	VM_STAT_GENERIC("remote_tlb_flush", remote_tlb_flush),
 	{ NULL }
 };
 
arch/s390/include/asm/kvm_host.h +2 -7
index 8925f3969478..9b4473f76e56 100644
@@ -361,6 +361,7 @@ struct sie_page {
 };
 
 struct kvm_vcpu_stat {
+	struct kvm_vcpu_stat_generic generic;
 	u64 exit_userspace;
 	u64 exit_null;
 	u64 exit_external_request;
@@ -370,13 +371,7 @@ struct kvm_vcpu_stat {
 	u64 exit_validity;
 	u64 exit_instruction;
 	u64 exit_pei;
-	u64 halt_successful_poll;
-	u64 halt_attempted_poll;
-	u64 halt_poll_invalid;
 	u64 halt_no_poll_steal;
-	u64 halt_wakeup;
-	u64 halt_poll_success_ns;
-	u64 halt_poll_fail_ns;
 	u64 instruction_lctl;
 	u64 instruction_lctlg;
 	u64 instruction_stctl;
@@ -755,12 +750,12 @@ struct kvm_vcpu_arch {
 };
 
 struct kvm_vm_stat {
+	struct kvm_vm_stat_generic generic;
 	u64 inject_io;
 	u64 inject_float_mchk;
 	u64 inject_pfault_done;
 	u64 inject_service_signal;
 	u64 inject_virtio;
-	u64 remote_tlb_flush;
 };
 
 struct kvm_arch_memory_slot {
arch/s390/kvm/kvm-s390.c +6 -6
index 1296fc10f80c..e8bc7cd06794 100644
@@ -72,13 +72,13 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 	VCPU_STAT("exit_program_interruption", exit_program_interruption),
 	VCPU_STAT("exit_instr_and_program_int", exit_instr_and_program),
 	VCPU_STAT("exit_operation_exception", exit_operation_exception),
-	VCPU_STAT("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
-	VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
+	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
+	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
+	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
 	VCPU_STAT("halt_no_poll_steal", halt_no_poll_steal),
-	VCPU_STAT("halt_wakeup", halt_wakeup),
-	VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
+	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
+	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
+	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
 	VCPU_STAT("instruction_lctlg", instruction_lctlg),
 	VCPU_STAT("instruction_lctl", instruction_lctl),
 	VCPU_STAT("instruction_stctl", instruction_stctl),
arch/x86/include/asm/kvm_host.h +2 -7
index 5d7e4b99993d..f524fceaffaa 100644
@@ -1167,6 +1167,7 @@ struct kvm_arch {
 };
 
 struct kvm_vm_stat {
+	struct kvm_vm_stat_generic generic;
 	u64 mmu_shadow_zapped;
 	u64 mmu_pte_write;
 	u64 mmu_pde_zapped;
@@ -1174,13 +1175,13 @@ struct kvm_vm_stat {
 	u64 mmu_recycled;
 	u64 mmu_cache_miss;
 	u64 mmu_unsync;
-	u64 remote_tlb_flush;
 	u64 lpages;
 	u64 nx_lpage_splits;
 	u64 max_mmu_page_hash_collisions;
 };
 
 struct kvm_vcpu_stat {
+	struct kvm_vcpu_stat_generic generic;
 	u64 pf_fixed;
 	u64 pf_guest;
 	u64 tlb_flush;
@@ -1194,10 +1195,6 @@ struct kvm_vcpu_stat {
 	u64 nmi_window_exits;
 	u64 l1d_flush;
 	u64 halt_exits;
-	u64 halt_successful_poll;
-	u64 halt_attempted_poll;
-	u64 halt_poll_invalid;
-	u64 halt_wakeup;
 	u64 request_irq_exits;
 	u64 irq_exits;
 	u64 host_state_reload;
@@ -1208,8 +1205,6 @@ struct kvm_vcpu_stat {
 	u64 irq_injections;
 	u64 nmi_injections;
 	u64 req_event;
-	u64 halt_poll_success_ns;
-	u64 halt_poll_fail_ns;
 	u64 nested_run;
 	u64 directed_yield_attempted;
 	u64 directed_yield_successful;
arch/x86/kvm/x86.c +7 -7
index 8b898ec8d349..157212157aee 100644
@@ -235,10 +235,10 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 	VCPU_STAT("irq_window", irq_window_exits),
 	VCPU_STAT("nmi_window", nmi_window_exits),
 	VCPU_STAT("halt_exits", halt_exits),
-	VCPU_STAT("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
-	VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
-	VCPU_STAT("halt_wakeup", halt_wakeup),
+	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
+	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
+	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
+	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
 	VCPU_STAT("hypercalls", hypercalls),
 	VCPU_STAT("request_irq", request_irq_exits),
 	VCPU_STAT("irq_exits", irq_exits),
@@ -250,8 +250,8 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 	VCPU_STAT("nmi_injections", nmi_injections),
 	VCPU_STAT("req_event", req_event),
 	VCPU_STAT("l1d_flush", l1d_flush),
-	VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
+	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
+	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
 	VCPU_STAT("nested_run", nested_run),
 	VCPU_STAT("directed_yield_attempted", directed_yield_attempted),
 	VCPU_STAT("directed_yield_successful", directed_yield_successful),
@@ -263,7 +263,7 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 	VM_STAT("mmu_recycled", mmu_recycled),
 	VM_STAT("mmu_cache_miss", mmu_cache_miss),
 	VM_STAT("mmu_unsync", mmu_unsync),
-	VM_STAT("remote_tlb_flush", remote_tlb_flush),
+	VM_STAT_GENERIC("remote_tlb_flush", remote_tlb_flush),
 	VM_STAT("largepages", lpages, .mode = 0444),
 	VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444),
 	VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions),
include/linux/kvm_host.h +7 -2
index 37cbb56ccd09..5a31e0696360 100644
@@ -1275,10 +1275,15 @@ struct kvm_stats_debugfs_item {
 #define KVM_DBGFS_GET_MODE(dbgfs_item)                                         \
 	((dbgfs_item)->mode ? (dbgfs_item)->mode : 0644)
 
-#define VM_STAT(n, x, ...) 							\
+#define VM_STAT(n, x, ...)						       \
 	{ n, offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__ }
-#define VCPU_STAT(n, x, ...)							\
+#define VCPU_STAT(n, x, ...)						       \
 	{ n, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__ }
+#define VM_STAT_GENERIC(n, x, ...)					       \
+	{ n, offsetof(struct kvm, stat.generic.x), KVM_STAT_VM, ## __VA_ARGS__ }
+#define VCPU_STAT_GENERIC(n, x, ...)					       \
+	{ n, offsetof(struct kvm_vcpu, stat.generic.x),			       \
+	  KVM_STAT_VCPU, ## __VA_ARGS__ }
 
 extern struct kvm_stats_debugfs_item debugfs_entries[];
 extern struct dentry *kvm_debugfs_dir;
include/linux/kvm_types.h +12 -0
index a7580f69dda0..48db778291b7 100644
@@ -76,5 +76,17 @@ struct kvm_mmu_memory_cache {
 };
 #endif
 
+struct kvm_vm_stat_generic {
+	u64 remote_tlb_flush;
+};
+
+struct kvm_vcpu_stat_generic {
+	u64 halt_successful_poll;
+	u64 halt_attempted_poll;
+	u64 halt_poll_invalid;
+	u64 halt_wakeup;
+	u64 halt_poll_success_ns;
+	u64 halt_poll_fail_ns;
+};
 
 #endif /* __KVM_TYPES_H__ */
virt/kvm/kvm_main.c +7 -7
index ed4d1581d502..cec986487b30 100644
@@ -332,7 +332,7 @@ void kvm_flush_remote_tlbs(struct kvm *kvm)
 	 */
 	if (!kvm_arch_flush_remote_tlb(kvm)
 	    || kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
-		++kvm->stat.remote_tlb_flush;
+		++kvm->stat.generic.remote_tlb_flush;
 	cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
 }
 EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
@@ -3029,9 +3029,9 @@ static inline void
 update_halt_poll_stats(struct kvm_vcpu *vcpu, u64 poll_ns, bool waited)
 {
 	if (waited)
-		vcpu->stat.halt_poll_fail_ns += poll_ns;
+		vcpu->stat.generic.halt_poll_fail_ns += poll_ns;
 	else
-		vcpu->stat.halt_poll_success_ns += poll_ns;
+		vcpu->stat.generic.halt_poll_success_ns += poll_ns;
 }
 
 /*
@@ -3049,16 +3049,16 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
 	if (vcpu->halt_poll_ns && !kvm_arch_no_poll(vcpu)) {
 		ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns);
 
-		++vcpu->stat.halt_attempted_poll;
+		++vcpu->stat.generic.halt_attempted_poll;
 		do {
 			/*
 			 * This sets KVM_REQ_UNHALT if an interrupt
 			 * arrives.
 			 */
 			if (kvm_vcpu_check_block(vcpu) < 0) {
-				++vcpu->stat.halt_successful_poll;
+				++vcpu->stat.generic.halt_successful_poll;
 				if (!vcpu_valid_wakeup(vcpu))
-					++vcpu->stat.halt_poll_invalid;
+					++vcpu->stat.generic.halt_poll_invalid;
 				goto out;
 			}
 			poll_end = cur = ktime_get();
@@ -3115,7 +3115,7 @@ bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
 	waitp = kvm_arch_vcpu_get_wait(vcpu);
 	if (rcuwait_wake_up(waitp)) {
 		WRITE_ONCE(vcpu->ready, true);
-		++vcpu->stat.halt_wakeup;
+		++vcpu->stat.generic.halt_wakeup;
 		return true;
 	}
 
Signature · 1 line
2.32.0.272.g935e593368-goog
author
#2 lore raw

PATCH v10 2/5 KVM: stats: Add fd-based API to read binary stats data

In reply to Jing Zhang

Provides a file descriptor per VM to read VM stats info/data. Provides a file descriptor per vCPU to read vCPU stats info/data.

The KVM stats now is only accessible by debugfs, which has some shortcomings this change are supposed to fix:
1. Debugfs is not a stable interface for production and it is disabled when kernel Lockdown mode is enabled.
2. Debugfs is organized as "one value per file", it is good for debugging, but not supposed to be used for production.
3. Debugfs read/clear in KVM are protected by the global kvm_lock.

Besides that, there are some other benefits with this change:
1. All KVM VM/VCPU stats can be read out in a bulk by one copy to userspace.
2. A schema is used to describe KVM statistics. From userspace's perspective, the KVM statistics are self-describing.
3. Fd-based solution provides the possibility that a telemetry can read KVM stats in a less privileged situation.
4. After the initial setup by reading in stats descriptors, a telemetry only needs to read the stats data itself, no more parsing or setup is needed.

---
 arch/arm64/kvm/guest.c    |  38 +++++++++
 arch/mips/kvm/mips.c      |  61 ++++++++++++++
 arch/powerpc/kvm/book3s.c |  64 +++++++++++++++
 arch/powerpc/kvm/booke.c  |  57 +++++++++++++
 arch/s390/kvm/kvm-s390.c  | 130 ++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c        |  68 ++++++++++++++++
 include/linux/kvm_host.h  | 164 +++++++++++++++++++++++++++++++++++++-
 include/uapi/linux/kvm.h  |  46 +++++++++++
 virt/kvm/kvm_main.c       | 152 ++++++++++++++++++++++++++++++++++-
 9 files changed, 775 insertions(+), 5 deletions(-)
arch/arm64/kvm/guest.c +38 -0
index 4962331d01e6..99842e29c2de 100644
@@ -28,6 +28,44 @@
 
 #include "trace.h"
 
+struct _kvm_stats_desc kvm_vm_stats_desc[] = {
+	KVM_GENERIC_VM_STATS()
+};
+static_assert(ARRAY_SIZE(kvm_vm_stats_desc) =
+		sizeof(struct kvm_vm_stat) / sizeof(u64));
+
+struct _kvm_stats_header kvm_vm_stats_header = {
+	{
+		.name_size = KVM_STATS_NAME_LEN,
+		.count = ARRAY_SIZE(kvm_vm_stats_desc),
+		.desc_offset = sizeof(struct _kvm_stats_header),
+		.data_offset = sizeof(struct _kvm_stats_header) +
+			sizeof(kvm_vm_stats_desc),
+	}
+};
+
+struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
+	KVM_GENERIC_VCPU_STATS(),
+	STATS_DESC_COUNTER(VCPU, hvc_exit_stat),
+	STATS_DESC_COUNTER(VCPU, wfe_exit_stat),
+	STATS_DESC_COUNTER(VCPU, wfi_exit_stat),
+	STATS_DESC_COUNTER(VCPU, mmio_exit_user),
+	STATS_DESC_COUNTER(VCPU, mmio_exit_kernel),
+	STATS_DESC_COUNTER(VCPU, exits)
+};
+static_assert(ARRAY_SIZE(kvm_vcpu_stats_desc) =
+		sizeof(struct kvm_vcpu_stat) / sizeof(u64));
+
+struct _kvm_stats_header kvm_vcpu_stats_header = {
+	{
+		.name_size = KVM_STATS_NAME_LEN,
+		.count = ARRAY_SIZE(kvm_vcpu_stats_desc),
+		.desc_offset = sizeof(struct _kvm_stats_header),
+		.data_offset = sizeof(struct _kvm_stats_header) +
+			sizeof(kvm_vcpu_stats_desc),
+	}
+};
+
 struct kvm_stats_debugfs_item debugfs_entries[] = {
 	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
 	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
arch/mips/kvm/mips.c +61 -0

67 lines not on this page · raw message

arch/powerpc/kvm/book3s.c +64 -0

70 lines not on this page · raw message

arch/powerpc/kvm/booke.c +57 -0

63 lines not on this page · raw message

arch/s390/kvm/kvm-s390.c +130 -0

136 lines not on this page · raw message

arch/x86/kvm/x86.c +68 -0

74 lines not on this page · raw message

include/linux/kvm_host.h +163 -1

190 lines not on this page · raw message

include/uapi/linux/kvm.h +46 -0

56 lines not on this page · raw message

virt/kvm/kvm_main.c +148 -4

187 lines not on this page · raw message

Signature · 1 line
2.32.0.272.g935e593368-goog
#3 lore raw

Re: [PATCH v10 2/5] KVM: stats: Add fd-based API to read binary stats data

Quoted 7 lines · Jing Zhang

Provides a file descriptor per VM to read VM stats info/data. Provides a file descriptor per vCPU to read vCPU stats info/data.

The KVM stats now is only accessible by debugfs, which has some shortcomings this change are supposed to fix:
1. Debugfs is not a stable interface for production and it is disabled when kernel Lockdown mode is enabled.

debugfs _could_ be a stable interface if you want it to be and make that rule for your subsystem. Disabling it for lockdown mode is a different issue, and that is a system-wide-policy-decision, not a debugfs-specific thing.

Quoted 2 lines

2. Debugfs is organized as "one value per file", it is good for debugging, but not supposed to be used for production.

debugfs IS NOT one-value-per-file, you can do whatever you want in there. sysfs IS one-value-per-file, do not get the two confused there.

Quoted 1 line

3. Debugfs read/clear in KVM are protected by the global kvm_lock.

That's your implementation issue, not a debugfs issue.

The only "rule" in debugfs is:
There are no rules.

So while your subsystem might have issues with using debugfs for statistics like this, that's not debugfs's fault, that's how you want to use the debugfs files for your subsystem.

Quoted 7 lines

Besides that, there are some other benefits with this change:
1. All KVM VM/VCPU stats can be read out in a bulk by one copy to userspace.
2. A schema is used to describe KVM statistics. From userspace's perspective, the KVM statistics are self-describing.
3. Fd-based solution provides the possibility that a telemetry can read KVM stats in a less privileged situation.

"possiblity"? Does this work or not? Have you tested it?

virt/kvm/kvm_main.c static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, · line 4167

Quoted 7 lines
virt/kvm/kvm_main.c +7 -0
+static ssize_t kvm_vm_stats_read(struct file *file, char __user *user_buffer,
+			      size_t size, loff_t *offset)
+{
+	struct kvm *kvm = file->private_data;
+
+	snprintf(&kvm_vm_stats_header.id[0], sizeof(kvm_vm_stats_header.id),
+			"kvm-%d", task_pid_nr(current));

Why do you write to this static variable for EVERY read? Shouldn't you just do it once at open? How can it change?

Wait, it's a single shared variable, what happens when multiple tasks open this thing and read from it? You race between writing to this variable here and then:

virt/kvm/kvm_main.c static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, · line 4169

Quoted 2 lines
virt/kvm/kvm_main.c +2 -0
+	return kvm_stats_read(&kvm_vm_stats_header, &kvm_vm_stats_desc[0],
+		&kvm->stat, sizeof(kvm->stat), user_buffer, size, offset);

Accessing it here.

So how is this really working?

thanks,

greg k-h

maintainer
#4 lore raw

Re: [PATCH v10 2/5] KVM: stats: Add fd-based API to read binary stats data

Quoted 4 lines · Greg KH
Quoted 2 lines

3. Fd-based solution provides the possibility that a telemetry can read KVM stats in a less privileged situation.

"possiblity"? Does this work or not? Have you tested it?

I think this is essentially s/that/for/. But more precisely:

3. Compared for example to a ioctl, a separate file descriptor makes it possible for an external program to read statistics, while maintaining privilege separation between VMM and telemetry code.

Quoted 11 lines

virt/kvm/kvm_main.c static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, · line 4167

Quoted 3 lines
virt/kvm/kvm_main.c +2 -0
+	snprintf(&kvm_vm_stats_header.id[0], sizeof(kvm_vm_stats_header.id),
+			"kvm-%d", task_pid_nr(current));

Why do you write to this static variable for EVERY read? Shouldn't you just do it once at open? How can it change?

Wait, it's a single shared variable, what happens when multiple tasks open this thing and read from it? You race between writing to this variable here and then:

Quoted 6 lines

virt/kvm/kvm_main.c static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, · line 4169

Quoted 2 lines
virt/kvm/kvm_main.c +2 -0
+	return kvm_stats_read(&kvm_vm_stats_header, &kvm_vm_stats_desc[0],
+		&kvm->stat, sizeof(kvm->stat), user_buffer, size, offset);

Accessing it here.

So how is this really working?

It's not - Jing, kvm_vm_stats_header is small enough that you can store a copy in struct kvm.

Paolo

author
#5 lore raw

Re: [PATCH v10 2/5] KVM: stats: Add fd-based API to read binary stats data

In reply to Greg KH

Hi Greg,

Quoted 23 lines · Greg KH
Quoted 7 lines · Jing Zhang

Provides a file descriptor per VM to read VM stats info/data. Provides a file descriptor per vCPU to read vCPU stats info/data.

The KVM stats now is only accessible by debugfs, which has some shortcomings this change are supposed to fix:
1. Debugfs is not a stable interface for production and it is disabled when kernel Lockdown mode is enabled.

debugfs _could_ be a stable interface if you want it to be and make that rule for your subsystem. Disabling it for lockdown mode is a different issue, and that is a system-wide-policy-decision, not a debugfs-specific thing.

Quoted 2 lines

2. Debugfs is organized as "one value per file", it is good for debugging, but not supposed to be used for production.

debugfs IS NOT one-value-per-file, you can do whatever you want in there. sysfs IS one-value-per-file, do not get the two confused there.

Quoted 1 line

3. Debugfs read/clear in KVM are protected by the global kvm_lock.

Quoted 9 lines

That's your implementation issue, not a debugfs issue.

The only "rule" in debugfs is:
There are no rules.

So while your subsystem might have issues with using debugfs for statistics like this, that's not debugfs's fault, that's how you want to use the debugfs files for your subsystem.

You are right. The issues are from how the debugfs is used in KVM stats. Will fix the text accordingly.

Quoted 10 lines
Quoted 7 lines

Besides that, there are some other benefits with this change:
1. All KVM VM/VCPU stats can be read out in a bulk by one copy to userspace.
2. A schema is used to describe KVM statistics. From userspace's perspective, the KVM statistics are self-describing.
3. Fd-based solution provides the possibility that a telemetry can read KVM stats in a less privileged situation.

"possiblity"? Does this work or not? Have you tested it?

I should've said "We are able to read KVM stats in a less privileged process".

Quoted 15 lines

virt/kvm/kvm_main.c static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, · line 4167

Quoted 7 lines
virt/kvm/kvm_main.c +7 -0
+static ssize_t kvm_vm_stats_read(struct file *file, char __user *user_buffer,
+                           size_t size, loff_t *offset)
+{
+     struct kvm *kvm = file->private_data;
+
+     snprintf(&kvm_vm_stats_header.id[0], sizeof(kvm_vm_stats_header.id),
+                     "kvm-%d", task_pid_nr(current));

Why do you write to this static variable for EVERY read? Shouldn't you just do it once at open? How can it change?

Wait, it's a single shared variable, what happens when multiple tasks open this thing and read from it? You race between writing to this variable here and then:

Quoted 7 lines

virt/kvm/kvm_main.c static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, · line 4169

Quoted 2 lines
virt/kvm/kvm_main.c +2 -0
+     return kvm_stats_read(&kvm_vm_stats_header, &kvm_vm_stats_desc[0],
+             &kvm->stat, sizeof(kvm->stat), user_buffer, size, offset);

Accessing it here.

So how is this really working?

You are right. We only need to do it once at the open. Will fix it according to Paolo's suggestion.

Quoted 3 lines

thanks,

greg k-h

Thanks,
Jing

#6 lore raw

Re: [PATCH v10 2/5] KVM: stats: Add fd-based API to read binary stats data

In reply to Jing Zhang
Quoted 2 lines · Jing Zhang

Provides a file descriptor per VM to read VM stats info/data. Provides a file descriptor per vCPU to read vCPU stats info/data.

Shouldn't this be two separate patches, one for each thing as these are two different features being added?

Anyway, an implementation question for both of these:

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3504

Quoted 5 lines
virt/kvm/kvm_main.c +5 -0
+static ssize_t kvm_stats_read(struct _kvm_stats_header *header,
+		struct _kvm_stats_desc *desc, void *stats, size_t size_stats,
+		char __user *user_buffer, size_t size, loff_t *offset)
+{
+	ssize_t copylen, len, remain = size;

You are "burying" the fact that remain is initialized here, not nice, I totally missed it when reading this the first time.

This should be:

	ssize_t copylen;
	ssize_t len;
	ssize_t remain = size;

to be obvious.

Remember you will be looking at this code for the next 20 years, make it easy to read.

Quoted 15 lines
virt/kvm/kvm_main.c +15 -0
+	size_t size_header, size_desc;
+	loff_t pos = *offset;
+	char __user *dest = user_buffer;
+	void *src;
+
+	size_header = sizeof(*header);
+	size_desc = header->header.count * sizeof(*desc);
+
+	len = size_header + size_desc + size_stats - pos;
+	len = min(len, remain);
+	if (len <= 0)
+		return 0;
+	remain = len;
+
+	/* Copy kvm stats header */

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3529

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+	copylen = size_header - pos;
+	copylen = min(copylen, remain);
+	if (copylen > 0) {
+		src = (void *)header + pos;
+		if (copy_to_user(dest, src, copylen))
+			return -EFAULT;
+		remain -= copylen;
+		pos += copylen;
+		dest += copylen;
+	}

I thought you said that you would not provide the header for each read, if you keep reading from the fd. It looks like you are adding it here to each read, or is there some "magic" with pos happening here that I do not understand?

And if there is "magic" with pos, you should document it as it's not very obvious :)

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3541

Quoted 12 lines
virt/kvm/kvm_main.c +12 -0
+	/* Copy kvm stats descriptors */
+	copylen = header->header.desc_offset + size_desc - pos;
+	copylen = min(copylen, remain);
+	if (copylen > 0) {
+		src = (void *)desc + pos - header->header.desc_offset;
+		if (copy_to_user(dest, src, copylen))
+			return -EFAULT;
+		remain -= copylen;
+		pos += copylen;
+		dest += copylen;
+	}
+	/* Copy kvm stats values */

New lines between code blocks of doing things?

And again, why copy the decriptor again? or is it being skipped somehow? Ah, I think I see how it's being skipped, if I look really closely. But again, it's not obvious, and I could be wrong. Please document this REALLY well.

Write code for the developer first, compiler second. Again, you are going to be maintaining it for 20+ years, think of your future self...

Quoted 4 lines
virt/kvm/kvm_main.c +4 -0
+	copylen = header->header.data_offset + size_stats - pos;
+	copylen = min(copylen, remain);
+	if (copylen > 0) {
+		src = stats + pos - header->header.data_offset;

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3555

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+		if (copy_to_user(dest, src, copylen))
+			return -EFAULT;
+		remain -= copylen;
+		pos += copylen;
+		dest += copylen;
+	}
+
+	*offset = pos;
+	return len;
+}

thanks,

greg k-h

maintainer
#7 lore raw

Re: [PATCH v10 2/5] KVM: stats: Add fd-based API to read binary stats data

Quoted 4 lines · Greg KH
Quoted 2 lines

Provides a file descriptor per VM to read VM stats info/data. Provides a file descriptor per vCPU to read vCPU stats info/data.

Shouldn't this be two separate patches, one for each thing as these are two different features being added?

They share a lot of code. We could have three patches though:

- add common code for binary statistics file descriptor

- add VM ioctl to retrieve a statistics file descriptor [including the definition of VM stats descriptors]

- add VCPU ioctl to retrieve a statistics file descriptor [again including the definitions of VCPU stats].

Paolo

author
#8 lore raw

Re: [PATCH v10 2/5] KVM: stats: Add fd-based API to read binary stats data

In reply to Greg KH

Hi Greg,

Quoted 8 lines · Greg KH
Quoted 2 lines · Jing Zhang

Provides a file descriptor per VM to read VM stats info/data. Provides a file descriptor per vCPU to read vCPU stats info/data.

Shouldn't this be two separate patches, one for each thing as these are two different features being added?

Actually, it is really not easy to separate this change into two patches even by following Paolo's suggestion. And it would be a surprise to userspace to see only VM stats, no VCPU stats.
I guess it is the text that caused the confusion, I'll change the commit message for this.

Quoted 11 lines

Anyway, an implementation question for both of these:

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3504

Quoted 5 lines
virt/kvm/kvm_main.c +5 -0
+static ssize_t kvm_stats_read(struct _kvm_stats_header *header,
+             struct _kvm_stats_desc *desc, void *stats, size_t size_stats,
+             char __user *user_buffer, size_t size, loff_t *offset)
+{
+     ssize_t copylen, len, remain = size;

You are "burying" the fact that remain is initialized here, not nice, I totally missed it when reading this the first time.

Quoted 9 lines

This should be:

        ssize_t copylen;
        ssize_t len;
        ssize_t remain = size;

to be obvious.

Remember you will be looking at this code for the next 20 years, make it easy to read.

Nice! Will do.

Quoted 31 lines
Quoted 15 lines
virt/kvm/kvm_main.c +15 -0
+     size_t size_header, size_desc;
+     loff_t pos = *offset;
+     char __user *dest = user_buffer;
+     void *src;
+
+     size_header = sizeof(*header);
+     size_desc = header->header.count * sizeof(*desc);
+
+     len = size_header + size_desc + size_stats - pos;
+     len = min(len, remain);
+     if (len <= 0)
+             return 0;
+     remain = len;
+
+     /* Copy kvm stats header */

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3529

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+     copylen = size_header - pos;
+     copylen = min(copylen, remain);
+     if (copylen > 0) {
+             src = (void *)header + pos;
+             if (copy_to_user(dest, src, copylen))
+                     return -EFAULT;
+             remain -= copylen;
+             pos += copylen;
+             dest += copylen;
+     }

I thought you said that you would not provide the header for each read, if you keep reading from the fd. It looks like you are adding it here to each read, or is there some "magic" with pos happening here that I do not understand?

Quoted 3 lines

And if there is "magic" with pos, you should document it as it's not very obvious :)

Will do.

Quoted 13 lines

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3541

Quoted 12 lines
virt/kvm/kvm_main.c +12 -0
+     /* Copy kvm stats descriptors */
+     copylen = header->header.desc_offset + size_desc - pos;
+     copylen = min(copylen, remain);
+     if (copylen > 0) {
+             src = (void *)desc + pos - header->header.desc_offset;
+             if (copy_to_user(dest, src, copylen))
+                     return -EFAULT;
+             remain -= copylen;
+             pos += copylen;
+             dest += copylen;
+     }
+     /* Copy kvm stats values */
Quoted 2 lines

New lines between code blocks of doing things?

Will add lines between code blocks.

Quoted 9 lines

And again, why copy the decriptor again? or is it being skipped somehow? Ah, I think I see how it's being skipped, if I look really closely. But again, it's not obvious, and I could be wrong. Please document this REALLY well.

Write code for the developer first, compiler second. Again, you are going to be maintaining it for 20+ years, think of your future self...

Sure, will document it.

Quoted 17 lines
Quoted 4 lines
virt/kvm/kvm_main.c +4 -0
+     copylen = header->header.data_offset + size_stats - pos;
+     copylen = min(copylen, remain);
+     if (copylen > 0) {
+             src = stats + pos - header->header.data_offset;

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3555

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+             if (copy_to_user(dest, src, copylen))
+                     return -EFAULT;
+             remain -= copylen;
+             pos += copylen;
+             dest += copylen;
+     }
+
+     *offset = pos;
+     return len;
+}

thanks,

Quoted 1 line

greg k-h

Thanks,
Jing

maintainer
#9 lore raw

Re: [PATCH v10 2/5] KVM: stats: Add fd-based API to read binary stats data

Quoted 3 lines · Jing Zhang

Actually, it is really not easy to separate this change into two patches even by following Paolo's suggestion. And it would be a surprise to userspace to see only VM stats, no VCPU stats.

That does not matter. Having two or three patches is useful because it makes review easier; they will never appear separately in a release.

Paolo

author
#10 lore raw

Re: [PATCH v10 2/5] KVM: stats: Add fd-based API to read binary stats data

Quoted 11 lines · Paolo Bonzini
Quoted 3 lines · Jing Zhang

Actually, it is really not easy to separate this change into two patches even by following Paolo's suggestion. And it would be a surprise to userspace to see only VM stats, no VCPU stats.

That does not matter. Having two or three patches is useful because it makes review easier; they will never appear separately in a release.

Paolo

Cool, then I'll separate them.

Thanks,
Jing

author
#11 lore raw

PATCH v10 3/5 KVM: stats: Add documentation for binary statistics interface

In reply to Jing Zhang

This new API provides a file descriptor for every VM and VCPU to read KVM statistics data in binary format.
It is meant to provide a lightweight, flexible, scalable and efficient lock-free solution for user space telemetry applications to pull the statistics data periodically for large scale systems. The pulling frequency could be as high as a few times per second.
The statistics descriptors are defined by KVM in kernel and can be by userspace to discover VM/VCPU statistics during the one-time setup stage.
The statistics data itself could be read out by userspace telemetry periodically without any extra parsing or setup effort. There are a few existed interface protocols and definitions, but no one can fulfil all the requirements this interface implemented as below:
1. During high frequency periodic stats reading, there should be no extra efforts except the stats data read itself.
2. Support stats annotation, like type (cumulative, instantaneous, peak, histogram, etc) and unit (counter, time, size, cycles, etc).
3. The stats data reading should be free of lock/synchronization. We don't care about the consistency between all the stats data. All stats data can not be read out at exactly the same time. We really care about the change or trend of the stats data. The lock-free solution is not just for efficiency and scalability, also for the stats data accuracy and usability. For example, in the situation that all the stats data readings are protected by a global lock, if one VCPU died somehow with that lock held, then all stats data reading would be blocked, then we have no way from stats data that which VCPU has died.
4. The stats data reading workload can be handed over to other unprivileged process.

---
 Documentation/virt/kvm/api.rst | 177 ++++++++++++++++++++++++++++++++-
 1 file changed, 176 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +176 -1
index e328caa35d6c..35ee52dbec89 100644
@@ -5034,7 +5034,6 @@ see KVM_XEN_VCPU_SET_ATTR above.
 The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used
 with the KVM_XEN_VCPU_GET_ATTR ioctl.
 
-
 4.131 KVM_GET_SREGS2
 ------------------
 
@@ -5081,6 +5080,174 @@ Writes special registers into the vcpu.
 See KVM_GET_SREGS2 for the data structures.
 This ioctl (when supported) replaces the KVM_SET_SREGS.
 
+4.133 KVM_GET_STATS_FD
+----------------------
+
+:Capability: KVM_CAP_STATS_BINARY_FD
+:Architectures: all
+:Type: vm ioctl, vcpu ioctl
+:Parameters: none
+:Returns: statistics file descriptor on success, < 0 on error
+
+Errors:
+
+  ===     ===========================
+  ENOMEM     if the fd could not be created due to lack of memory
+  EMFILE     if the number of opened files exceeds the limit
+  ===     ===========================
+
+The file descriptor can be used to read VM/vCPU statistics data in binary
+format. The file data is organized into three blocks as below:
++-------------+
+|   Header    |
++-------------+
+| Descriptors |
++-------------+
+| Stats Data  |
++-------------+
+
+The Header block is always at the start of the file. It is only needed to be
+read one time for the lifetime of the file descriptor.
+It is in the form of ``struct kvm_stats_header`` as below::
+
+	#define KVM_STATS_ID_MAXLEN		64
+
+	struct kvm_stats_header {
+		__u32 name_size;
+		__u32 count;
+		__u32 desc_offset;
+		__u32 data_offset;
+		char id[0];
+	};
+
+The ``id`` field is identification for the corresponding KVM statistics. For
+VM statistics, it is in the form of "kvm-{kvm pid}", like "kvm-12345". For
+VCPU statistics, it is in the form of "kvm-{kvm pid}/vcpu-{vcpu id}", like
+"kvm-12345/vcpu-12".
+
+The ``name_size`` field is the size (byte) of the statistics name string
+(including trailing '\0') appended to the end of every statistics descriptor.
+
+The ``count`` field is the number of statistics.
+
+The ``desc_offset`` field is the offset of the Descriptors block from the start
+of the file indicated by the file descriptor.
+
+The ``data_offset`` field is the offset of the Stats Data block from the start
+of the file indicated by the file descriptor.
+
+The Descriptors block is only needed to be read once for the lifetime of the
+file descriptor. It is an array of ``struct kvm_stats_desc`` as shown in
+below code block::
+
+	#define KVM_STATS_TYPE_SHIFT		0
+	#define KVM_STATS_TYPE_MASK		(0xF << KVM_STATS_TYPE_SHIFT)
+	#define KVM_STATS_TYPE_CUMULATIVE	(0x0 << KVM_STATS_TYPE_SHIFT)
+	#define KVM_STATS_TYPE_INSTANT		(0x1 << KVM_STATS_TYPE_SHIFT)
+	#define KVM_STATS_TYPE_MAX		KVM_STATS_TYPE_INSTANT
+
+	#define KVM_STATS_UNIT_SHIFT		4
+	#define KVM_STATS_UNIT_MASK		(0xF << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_NONE		(0x0 << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_BYTES		(0x1 << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_SECONDS		(0x2 << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_CYCLES		(0x3 << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_MAX		KVM_STATS_UNIT_CYCLES
+
+	#define KVM_STATS_BASE_SHIFT		8
+	#define KVM_STATS_BASE_MASK		(0xF << KVM_STATS_BASE_SHIFT)
+	#define KVM_STATS_BASE_POW10		(0x0 << KVM_STATS_BASE_SHIFT)
+	#define KVM_STATS_BASE_POW2		(0x1 << KVM_STATS_BASE_SHIFT)
+	#define KVM_STATS_BASE_MAX		KVM_STATS_BASE_POW2
+
+	struct kvm_stats_desc {
+		__u32 flags;
+		__s16 exponent;
+		__u16 size;
+		__u32 offset;
+		__u32 unused;
+		char name[0];
+	};
+
+The ``flags`` field contains the type and unit of the statistics data described
+by this descriptor. The following flags are supported:
+
+Bits 0-3 of ``flags`` encode the type:
+  * ``KVM_STATS_TYPE_CUMULATIVE``
+    The statistics data is cumulative. The value of data can only be increased.
+    Most of the counters used in KVM are of this type.
+    The corresponding ``count`` field for this type is always 1.
+  * ``KVM_STATS_TYPE_INSTANT``
+    The statistics data is instantaneous. Its value can be increased or
+    decreased. This type is usually used as a measurement of some resources,
+    like the number of dirty pages, the number of large pages, etc.
+    The corresponding ``count`` field for this type is always 1.
+
+Bits 4-7 of ``flags`` encode the unit:
+  * ``KVM_STATS_UNIT_NONE``
+    There is no unit for the value of statistics data. This usually means that
+    the value is a simple counter of an event.
+  * ``KVM_STATS_UNIT_BYTES``
+    It indicates that the statistics data is used to measure memory size, in the
+    unit of Byte, KiByte, MiByte, GiByte, etc. The unit of the data is
+    determined by the ``exponent`` field in the descriptor. The
+    ``KVM_STATS_BASE_POW2`` flag is valid in this case. The unit of the data is
+    determined by ``pow(2, exponent)``. For example, if value is 10,
+    ``exponent`` is 20, which means the unit of statistics data is MiByte, we
+    can get the statistics data in the unit of Byte by
+    ``value * pow(2, exponent) = 10 * pow(2, 20) = 10 MiByte`` which is
+    10 * 1024 * 1024 Bytes.
+  * ``KVM_STATS_UNIT_SECONDS``
+    It indicates that the statistics data is used to measure time/latency, in
+    the unit of nanosecond, microsecond, millisecond and second. The unit of the
+    data is determined by the ``exponent`` field in the descriptor. The
+    ``KVM_STATS_BASE_POW10`` flag is valid in this case. The unit of the data
+    is determined by ``pow(10, exponent)``. For example, if value is 2000000,
+    ``exponent`` is -6, which means the unit of statistics data is microsecond,
+    we can get the statistics data in the unit of second by
+    ``value * pow(10, exponent) = 2000000 * pow(10, -6) = 2 seconds``.
+  * ``KVM_STATS_UNIT_CYCLES``
+    It indicates that the statistics data is used to measure CPU clock cycles.
+    The ``KVM_STATS_BASE_POW10`` flag is valid in this case. For example, if
+    value is 200, ``exponent`` is 4, we can get the number of CPU clock cycles
+    by ``value * pow(10, exponent) = 200 * pow(10, 4) = 2000000``.
+
+Bits 8-11 of ``flags`` encode the base:
+  * ``KVM_STATS_BASE_POW10``
+    The scale is based on power of 10. It is used for measurement of time and
+    CPU clock cycles.
+  * ``KVM_STATS_BASE_POW2``
+    The scale is based on power of 2. It is used for measurement of memory size.
+
+The ``exponent`` field is the scale of corresponding statistics data. For
+example, if the unit is ``KVM_STATS_UNIT_BYTES``, the base is
+``KVM_STATS_BASE_POW2``, the ``exponent`` is 10, then we know that the real
+unit of the statistics data is KBytes a.k.a pow(2, 10) = 1024 bytes.
+
+The ``size`` field is the number of values (u64) of this statistics data. Its
+value is usually 1 for most of simple statistics.
+
+The ``offset`` field is the offset from the start of Data Block to the start of
+the corresponding statistics data.
+
+The ``unused`` fields are reserved for future support for other types of
+statistics data, like log/linear histogram.
+
+The ``name`` field points to the name string of the statistics data. The name
+string starts at the end of ``struct kvm_stats_desc``.
+The maximum length (including trailing '\0') is indicated by ``name_size``
+in ``struct kvm_stats_header``.
+
+The Stats Data block contains an array of data values of type ``struct
+kvm_vm_stats_data`` or ``struct kvm_vcpu_stats_data``. It would be read by
+userspace periodically to pull statistics data.
+The order of data value in Stats Data block is the same as the order of
+descriptors in Descriptors block.
+  * Statistics data for VM/VCPU::
+
+	struct kvm_stats_data {
+		__u64 value[0];
+	};
 
 5. The kvm_run structure
 ============
@@ -6969,3 +7136,11 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset
 of the result of KVM_CHECK_EXTENSION.  KVM will forward to userspace
 the hypercalls whose corresponding bit is in the argument, and return
 ENOSYS for the others.
+
+8.35 KVM_CAP_STATS_BINARY_FD
+----------------------------
+
+:Architectures: all
+
+This capability indicates the feature that userspace can create get a file
+descriptor for every VM and VCPU to read statistics data in binary format.
Signature · 1 line
2.32.0.272.g935e593368-goog
#12 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5175

Quoted 13 lines · Jing Zhang
Documentation/virt/kvm/api.rst +13 -0
+	struct kvm_stats_desc {
+		__u32 flags;
+		__s16 exponent;
+		__u16 size;
+		__u32 offset;
+		__u32 unused;
+		char name[0];
+	};
+
+The ``flags`` field contains the type and unit of the statistics data described
+by this descriptor. The following flags are supported:
+
+Bits 0-3 of ``flags`` encode the type:

<snip>

As flags is a bit field, what is the endian of it? Native or LE or BE? I'm guessing "cpu native", but you should be explicit as userspace will have to deal with this somehow.

thanks,

greg k-h

author
#13 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Hi Greg,

Quoted 18 lines · Greg KH
Quoted 13 lines · Jing Zhang
Documentation/virt/kvm/api.rst +13 -0
+     struct kvm_stats_desc {
+             __u32 flags;
+             __s16 exponent;
+             __u16 size;
+             __u32 offset;
+             __u32 unused;
+             char name[0];
+     };
+
+The ``flags`` field contains the type and unit of the statistics data described
+by this descriptor. The following flags are supported:
+
+Bits 0-3 of ``flags`` encode the type:

<snip>

Quoted 3 lines

As flags is a bit field, what is the endian of it? Native or LE or BE? I'm guessing "cpu native", but you should be explicit as userspace will have to deal with this somehow.

Sure, will add clarification for endianness.

Quoted 4 lines

thanks,

greg k-h

Thanks,
Jing

#14 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

In reply to Jing Zhang
Quoted 8 lines · Jing Zhang
Documentation/virt/kvm/api.rst +8 -0
+	struct kvm_stats_desc {
+		__u32 flags;
+		__s16 exponent;
+		__u16 size;
+		__u32 offset;
+		__u32 unused;
+		char name[0];
+	};

<snip>

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5234

Quoted 2 lines
Documentation/virt/kvm/api.rst +2 -0
+The ``unused`` fields are reserved for future support for other types of
+statistics data, like log/linear histogram.

you HAVE to set unused to 0 for now, otherwise userspace does not know it is unused, right? And then, really it is "used", so why not just say that now? It's tricky, but you have to get this right now otherwise you can never use it in the future.

Quoted 1 line

+The ``name`` field points to the name string of the statistics data. The name

It is not a pointer, it is the data itself.

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5239

Quoted 3 lines
Documentation/virt/kvm/api.rst +3 -0
+string starts at the end of ``struct kvm_stats_desc``.
+The maximum length (including trailing '\0') is indicated by ``name_size``
+in ``struct kvm_stats_header``.

I thought we were replacing [0] arrays with [], are you sure you should be declaring this as [0]? Same for all structures in this document (and code).

thanks,

greg k-h

maintainer
#15 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Quoted 12 lines · Greg KH
Quoted 8 lines · Jing Zhang
Documentation/virt/kvm/api.rst +8 -0
+	struct kvm_stats_desc {
+		__u32 flags;
+		__s16 exponent;
+		__u16 size;
+		__u32 offset;
+		__u32 unused;
+		char name[0];
+	};

<snip>

Quoted 5 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5234

Quoted 2 lines
Documentation/virt/kvm/api.rst +2 -0
+The ``unused`` fields are reserved for future support for other types of
+statistics data, like log/linear histogram.

you HAVE to set unused to 0 for now, otherwise userspace does not know it is unused, right?

Jing, I think you planned to use it with other flags that are unused for now? But please do check that it's zero in the testcase.

Quoted 9 lines

It is not a pointer, it is the data itself.

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5239

Quoted 3 lines
Documentation/virt/kvm/api.rst +3 -0
+string starts at the end of ``struct kvm_stats_desc``.
+The maximum length (including trailing '\0') is indicated by ``name_size``
+in ``struct kvm_stats_header``.

I thought we were replacing [0] arrays with [], are you sure you should be declaring this as [0]? Same for all structures in this document (and code).

In C code [0] is a bit more flexible than []. I think in this particular case [] won't work due to how the structures are declared. In the documentation [] is certainly clearer.

Paolo

#16 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Quoted 32 lines · Paolo Bonzini
Quoted 12 lines · Greg KH
Quoted 8 lines · Jing Zhang
Documentation/virt/kvm/api.rst +8 -0
+	struct kvm_stats_desc {
+		__u32 flags;
+		__s16 exponent;
+		__u16 size;
+		__u32 offset;
+		__u32 unused;
+		char name[0];
+	};

<snip>

Quoted 5 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5234

Quoted 2 lines
Documentation/virt/kvm/api.rst +2 -0
+The ``unused`` fields are reserved for future support for other types of
+statistics data, like log/linear histogram.

you HAVE to set unused to 0 for now, otherwise userspace does not know it is unused, right?

Jing, I think you planned to use it with other flags that are unused for now? But please do check that it's zero in the testcase.

Quoted 9 lines

It is not a pointer, it is the data itself.

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5239

Quoted 3 lines
Documentation/virt/kvm/api.rst +3 -0
+string starts at the end of ``struct kvm_stats_desc``.
+The maximum length (including trailing '\0') is indicated by ``name_size``
+in ``struct kvm_stats_header``.

I thought we were replacing [0] arrays with [], are you sure you should be declaring this as [0]? Same for all structures in this document (and code).

Quoted 3 lines

In C code [0] is a bit more flexible than []. I think in this particular case [] won't work due to how the structures are declared. In the documentation [] is certainly clearer.

Look at all of the patches that Gustavo has been doing all over the tree for this work, you do not want to make him do this again here.

Gustavo, is [0] ok for fields like these?

thanks,

greg k-h

maintainer
#17 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Quoted 11 lines · Greg KH
Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Greg KH

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5239

Quoted 3 lines · Jing Zhang
Documentation/virt/kvm/api.rst +3 -0
+string starts at the end of ``struct kvm_stats_desc``.
+The maximum length (including trailing '\0') is indicated by ``name_size``
+in ``struct kvm_stats_header``.

I thought we were replacing [0] arrays with [], are you sure you should be declaring this as [0]? Same for all structures in this document (and code).

Quoted 8 lines
Quoted 3 lines

In C code [0] is a bit more flexible than []. I think in this particular case [] won't work due to how the structures are declared. In the documentation [] is certainly clearer.

Look at all of the patches that Gustavo has been doing all over the tree for this work, you do not want to make him do this again here.

Gustavo, is [0] ok for fields like these?

I should be able to get back to KVM stuff later today, I'll check myself if [] can be applied and reply. I had queued an early version of these for my local build to play with them but I haven't been able to do a complete review.

Paolo

author
#18 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

In reply to Paolo Bonzini
Quoted 14 lines · Paolo Bonzini
Quoted 12 lines · Greg KH
Quoted 8 lines · Jing Zhang
Documentation/virt/kvm/api.rst +8 -0
+    struct kvm_stats_desc {
+            __u32 flags;
+            __s16 exponent;
+            __u16 size;
+            __u32 offset;
+            __u32 unused;
+            char name[0];
+    };

<snip>

Quoted 9 lines
Quoted 5 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5234

Quoted 2 lines
Documentation/virt/kvm/api.rst +2 -0
+The ``unused`` fields are reserved for future support for other types of
+statistics data, like log/linear histogram.

you HAVE to set unused to 0 for now, otherwise userspace does not know it is unused, right?

Jing, I think you planned to use it with other flags that are unused for now? But please do check that it's zero in the testcase.

Yes, it was planned for future use (to support stats type of histogram). Will add check in testcase and clarify it in doc.

Quoted 10 lines
Quoted 9 lines

It is not a pointer, it is the data itself.

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5239

Quoted 3 lines
Documentation/virt/kvm/api.rst +3 -0
+string starts at the end of ``struct kvm_stats_desc``.
+The maximum length (including trailing '\0') is indicated by ``name_size``
+in ``struct kvm_stats_header``.

I thought we were replacing [0] arrays with [], are you sure you should be declaring this as [0]? Same for all structures in this document (and code).

Quoted 6 lines

In C code [0] is a bit more flexible than []. I think in this particular case [] won't work due to how the structures are declared. In the documentation [] is certainly clearer.

Paolo

Thanks,
Jing

author
#19 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

In reply to Greg KH

Hi Greg,

Quoted 13 lines · Greg KH
Quoted 8 lines · Jing Zhang
Documentation/virt/kvm/api.rst +8 -0
+     struct kvm_stats_desc {
+             __u32 flags;
+             __s16 exponent;
+             __u16 size;
+             __u32 offset;
+             __u32 unused;
+             char name[0];
+     };

<snip>

Quoted 8 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5234

Quoted 2 lines
Documentation/virt/kvm/api.rst +2 -0
+The ``unused`` fields are reserved for future support for other types of
+statistics data, like log/linear histogram.

you HAVE to set unused to 0 for now, otherwise userspace does not know it is unused, right? And then, really it is "used", so why not just say that now? It's tricky, but you have to get this right now otherwise you can never use it in the future.

Sure, will do that.

Quoted 4 lines
Quoted 1 line

+The ``name`` field points to the name string of the statistics data. The name

It is not a pointer, it is the data itself.

Will fix it.

Quoted 8 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5239

Quoted 3 lines
Documentation/virt/kvm/api.rst +3 -0
+string starts at the end of ``struct kvm_stats_desc``.
+The maximum length (including trailing '\0') is indicated by ``name_size``
+in ``struct kvm_stats_header``.

I thought we were replacing [0] arrays with [], are you sure you should be declaring this as [0]? Same for all structures in this document (and code).

The reason to declare it as [0] is to have the flexibility to change the maximum length of KVM stats name. For now, the max len is defined as 48, which can be read from the header. Then the userspace can get the length of descriptor by adding sizeof(struct_kvm_stats_desc) + 48. Whenever the max len is changed in KVM, the userspace would not have to update code to reflect that. However, if we are OK to restrict the maximum KVM stats' length to 48 (or any other
number), we can just declear it with [] instead of [0].

Quoted 3 lines

thanks,

greg k-h

Thanks,
Jing

author
#20 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Hi Greg,

Quoted 39 lines · Jing Zhang

Hi Greg,

Quoted 13 lines · Greg KH
Quoted 8 lines · Jing Zhang
Documentation/virt/kvm/api.rst +8 -0
+     struct kvm_stats_desc {
+             __u32 flags;
+             __s16 exponent;
+             __u16 size;
+             __u32 offset;
+             __u32 unused;
+             char name[0];
+     };

<snip>

Quoted 8 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5234

Quoted 2 lines
Documentation/virt/kvm/api.rst +2 -0
+The ``unused`` fields are reserved for future support for other types of
+statistics data, like log/linear histogram.

you HAVE to set unused to 0 for now, otherwise userspace does not know it is unused, right? And then, really it is "used", so why not just say that now? It's tricky, but you have to get this right now otherwise you can never use it in the future.

Sure, will do that.

Quoted 4 lines
Quoted 1 line

+The ``name`` field points to the name string of the statistics data. The name

It is not a pointer, it is the data itself.

Will fix it.

Quoted 8 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5239

Quoted 3 lines
Documentation/virt/kvm/api.rst +3 -0
+string starts at the end of ``struct kvm_stats_desc``.
+The maximum length (including trailing '\0') is indicated by ``name_size``
+in ``struct kvm_stats_header``.

I thought we were replacing [0] arrays with [], are you sure you should be declaring this as [0]? Same for all structures in this document (and code).

Quoted 8 lines

The reason to declare it as [0] is to have the flexibility to change the maximum length of KVM stats name. For now, the max len is defined as 48, which can be read from the header. Then the userspace can get the length of descriptor by adding sizeof(struct_kvm_stats_desc) + 48. Whenever the max len is changed in KVM, the userspace would not have to update code to reflect that. However, if we are OK to restrict the maximum KVM stats' length to 48 (or any other
number), we can just declear it with [] instead of [0].

Please ignore my above comments.
You are right. We can just replace all zero-length arrays [0] with a flexible array
member []. Thanks.

Quoted 6 lines
Quoted 3 lines

thanks,

greg k-h

Thanks,
Jing

#21 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

In reply to Jing Zhang
Quoted 13 lines · Jing Zhang
Documentation/virt/kvm/api.rst +13 -0
+The file descriptor can be used to read VM/vCPU statistics data in binary
+format. The file data is organized into three blocks as below:
++-------------+
+|   Header    |
++-------------+
+| Descriptors |
++-------------+
+| Stats Data  |
++-------------+
+
+The Header block is always at the start of the file. It is only needed to be
+read one time for the lifetime of the file descriptor.
+It is in the form of ``struct kvm_stats_header`` as below::

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5121

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+
+	#define KVM_STATS_ID_MAXLEN		64
+
+	struct kvm_stats_header {
+		__u32 name_size;
+		__u32 count;
+		__u32 desc_offset;
+		__u32 data_offset;
+		char id[0];
+	};

So you have no idea the size of the whole header when reading it? That feels odd, are you sure it's not needed?

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5126

Quoted 4 lines
Documentation/virt/kvm/api.rst +4 -0
+The ``id`` field is identification for the corresponding KVM statistics. For
+VM statistics, it is in the form of "kvm-{kvm pid}", like "kvm-12345". For
+VCPU statistics, it is in the form of "kvm-{kvm pid}/vcpu-{vcpu id}", like
+"kvm-12345/vcpu-12".

Why do you have "name_size" but not "id_size"?

And is this a \0 terminated string? If so, please state it here.

And what is the max size of this string?

And again, should it be [], not [0]?

Will the header be padded out to any specific byte boundry (4/8/32/whatever) before the other headers?

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+The ``name_size`` field is the size (byte) of the statistics name string

s/byte/in bytes/

Quoted 87 lines
Documentation/virt/kvm/api.rst +87 -0
+(including trailing '\0') appended to the end of every statistics descriptor.
+
+The ``count`` field is the number of statistics.
+
+The ``desc_offset`` field is the offset of the Descriptors block from the start
+of the file indicated by the file descriptor.
+
+The ``data_offset`` field is the offset of the Stats Data block from the start
+of the file indicated by the file descriptor.
+
+The Descriptors block is only needed to be read once for the lifetime of the
+file descriptor. It is an array of ``struct kvm_stats_desc`` as shown in
+below code block::
+
+	#define KVM_STATS_TYPE_SHIFT		0
+	#define KVM_STATS_TYPE_MASK		(0xF << KVM_STATS_TYPE_SHIFT)
+	#define KVM_STATS_TYPE_CUMULATIVE	(0x0 << KVM_STATS_TYPE_SHIFT)
+	#define KVM_STATS_TYPE_INSTANT		(0x1 << KVM_STATS_TYPE_SHIFT)
+	#define KVM_STATS_TYPE_MAX		KVM_STATS_TYPE_INSTANT
+
+	#define KVM_STATS_UNIT_SHIFT		4
+	#define KVM_STATS_UNIT_MASK		(0xF << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_NONE		(0x0 << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_BYTES		(0x1 << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_SECONDS		(0x2 << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_CYCLES		(0x3 << KVM_STATS_UNIT_SHIFT)
+	#define KVM_STATS_UNIT_MAX		KVM_STATS_UNIT_CYCLES
+
+	#define KVM_STATS_BASE_SHIFT		8
+	#define KVM_STATS_BASE_MASK		(0xF << KVM_STATS_BASE_SHIFT)
+	#define KVM_STATS_BASE_POW10		(0x0 << KVM_STATS_BASE_SHIFT)
+	#define KVM_STATS_BASE_POW2		(0x1 << KVM_STATS_BASE_SHIFT)
+	#define KVM_STATS_BASE_MAX		KVM_STATS_BASE_POW2
+
+	struct kvm_stats_desc {
+		__u32 flags;
+		__s16 exponent;
+		__u16 size;
+		__u32 offset;
+		__u32 unused;
+		char name[0];
+	};
+
+The ``flags`` field contains the type and unit of the statistics data described
+by this descriptor. The following flags are supported:
+
+Bits 0-3 of ``flags`` encode the type:
+  * ``KVM_STATS_TYPE_CUMULATIVE``
+    The statistics data is cumulative. The value of data can only be increased.
+    Most of the counters used in KVM are of this type.
+    The corresponding ``count`` field for this type is always 1.
+  * ``KVM_STATS_TYPE_INSTANT``
+    The statistics data is instantaneous. Its value can be increased or
+    decreased. This type is usually used as a measurement of some resources,
+    like the number of dirty pages, the number of large pages, etc.
+    The corresponding ``count`` field for this type is always 1.
+
+Bits 4-7 of ``flags`` encode the unit:
+  * ``KVM_STATS_UNIT_NONE``
+    There is no unit for the value of statistics data. This usually means that
+    the value is a simple counter of an event.
+  * ``KVM_STATS_UNIT_BYTES``
+    It indicates that the statistics data is used to measure memory size, in the
+    unit of Byte, KiByte, MiByte, GiByte, etc. The unit of the data is
+    determined by the ``exponent`` field in the descriptor. The
+    ``KVM_STATS_BASE_POW2`` flag is valid in this case. The unit of the data is
+    determined by ``pow(2, exponent)``. For example, if value is 10,
+    ``exponent`` is 20, which means the unit of statistics data is MiByte, we
+    can get the statistics data in the unit of Byte by
+    ``value * pow(2, exponent) = 10 * pow(2, 20) = 10 MiByte`` which is
+    10 * 1024 * 1024 Bytes.
+  * ``KVM_STATS_UNIT_SECONDS``
+    It indicates that the statistics data is used to measure time/latency, in
+    the unit of nanosecond, microsecond, millisecond and second. The unit of the
+    data is determined by the ``exponent`` field in the descriptor. The
+    ``KVM_STATS_BASE_POW10`` flag is valid in this case. The unit of the data
+    is determined by ``pow(10, exponent)``. For example, if value is 2000000,
+    ``exponent`` is -6, which means the unit of statistics data is microsecond,
+    we can get the statistics data in the unit of second by
+    ``value * pow(10, exponent) = 2000000 * pow(10, -6) = 2 seconds``.
+  * ``KVM_STATS_UNIT_CYCLES``
+    It indicates that the statistics data is used to measure CPU clock cycles.
+    The ``KVM_STATS_BASE_POW10`` flag is valid in this case. For example, if
+    value is 200, ``exponent`` is 4, we can get the number of CPU clock cycles
+    by ``value * pow(10, exponent) = 200 * pow(10, 4) = 2000000``.
+
+Bits 8-11 of ``flags`` encode the base:

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5225

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+  * ``KVM_STATS_BASE_POW10``
+    The scale is based on power of 10. It is used for measurement of time and
+    CPU clock cycles.
+  * ``KVM_STATS_BASE_POW2``
+    The scale is based on power of 2. It is used for measurement of memory size.
+
+The ``exponent`` field is the scale of corresponding statistics data. For
+example, if the unit is ``KVM_STATS_UNIT_BYTES``, the base is
+``KVM_STATS_BASE_POW2``, the ``exponent`` is 10, then we know that the real
+unit of the statistics data is KBytes a.k.a pow(2, 10) = 1024 bytes.

Might also want to show a negative example here for exponent, like you show above for time.

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5228

Quoted 3 lines
Documentation/virt/kvm/api.rst +3 -0
+
+The ``size`` field is the number of values (u64) of this statistics data. Its
+value is usually 1 for most of simple statistics.

What does "u64" mean here?

thanks,

greg k-h

author
#22 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Hi Greg,

Quoted 20 lines · Greg KH
Quoted 18 lines · Jing Zhang
Documentation/virt/kvm/api.rst +18 -0
+The file descriptor can be used to read VM/vCPU statistics data in binary
+format. The file data is organized into three blocks as below:
++-------------+
+|   Header    |
++-------------+
+| Descriptors |
++-------------+
+| Stats Data  |
++-------------+
+
+The Header block is always at the start of the file. It is only needed to be
+read one time for the lifetime of the file descriptor.
+It is in the form of ``struct kvm_stats_header`` as below::
+
+     #define KVM_STATS_ID_MAXLEN             64
+
+     struct kvm_stats_header {
+             __u32 name_size;
Quoted 9 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5121

Quoted 5 lines
Documentation/virt/kvm/api.rst +5 -0
+             __u32 count;
+             __u32 desc_offset;
+             __u32 data_offset;
+             char id[0];
+     };

So you have no idea the size of the whole header when reading it? That feels odd, are you sure it's not needed?

We do know the size when reading it, it is sizeof(struct kvm_stats_header) + KVM_STATS_ID_MAXLEN.
I think we need to change "char id[0]" to "char id[KVM_STATS_ID_MAXLEN]" here. Since it is not like the name field in the descriptor, the max id string's length is fixed.

Quoted 6 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5126

Quoted 4 lines
Documentation/virt/kvm/api.rst +4 -0
+The ``id`` field is identification for the corresponding KVM statistics. For
+VM statistics, it is in the form of "kvm-{kvm pid}", like "kvm-12345". For
+VCPU statistics, it is in the form of "kvm-{kvm pid}/vcpu-{vcpu id}", like
+"kvm-12345/vcpu-12".

Why do you have "name_size" but not "id_size"?

Because we know that the max length of id is less than 64, that's why we have KVM_STATS_ID_MAXLEN exported to userspace.
But for KVM stats name, we are not sure about the max length in the future. The maximum length for KVM stats is defined as 48 in KVM for now, but it is possible that it might need to be increased to 64 or larger.

Quoted 3 lines

And is this a \0 terminated string? If so, please state it here.

Will do.

Quoted 3 lines

And what is the max size of this string?

And again, should it be [], not [0]?

As stated in previous comments, there are pros and cons to choose between [] and [0].

Quoted 4 lines

Will the header be padded out to any specific byte boundry (4/8/32/whatever) before the other headers?

No. For every architecture, there is only one header for VM and one header for VCPU.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+The ``name_size`` field is the size (byte) of the statistics name string

s/byte/in bytes/

Will do.

Quoted 92 lines
Quoted 92 lines
Documentation/virt/kvm/api.rst +92 -0
+(including trailing '\0') appended to the end of every statistics descriptor.
+
+The ``count`` field is the number of statistics.
+
+The ``desc_offset`` field is the offset of the Descriptors block from the start
+of the file indicated by the file descriptor.
+
+The ``data_offset`` field is the offset of the Stats Data block from the start
+of the file indicated by the file descriptor.
+
+The Descriptors block is only needed to be read once for the lifetime of the
+file descriptor. It is an array of ``struct kvm_stats_desc`` as shown in
+below code block::
+
+     #define KVM_STATS_TYPE_SHIFT            0
+     #define KVM_STATS_TYPE_MASK             (0xF << KVM_STATS_TYPE_SHIFT)
+     #define KVM_STATS_TYPE_CUMULATIVE       (0x0 << KVM_STATS_TYPE_SHIFT)
+     #define KVM_STATS_TYPE_INSTANT          (0x1 << KVM_STATS_TYPE_SHIFT)
+     #define KVM_STATS_TYPE_MAX              KVM_STATS_TYPE_INSTANT
+
+     #define KVM_STATS_UNIT_SHIFT            4
+     #define KVM_STATS_UNIT_MASK             (0xF << KVM_STATS_UNIT_SHIFT)
+     #define KVM_STATS_UNIT_NONE             (0x0 << KVM_STATS_UNIT_SHIFT)
+     #define KVM_STATS_UNIT_BYTES            (0x1 << KVM_STATS_UNIT_SHIFT)
+     #define KVM_STATS_UNIT_SECONDS          (0x2 << KVM_STATS_UNIT_SHIFT)
+     #define KVM_STATS_UNIT_CYCLES           (0x3 << KVM_STATS_UNIT_SHIFT)
+     #define KVM_STATS_UNIT_MAX              KVM_STATS_UNIT_CYCLES
+
+     #define KVM_STATS_BASE_SHIFT            8
+     #define KVM_STATS_BASE_MASK             (0xF << KVM_STATS_BASE_SHIFT)
+     #define KVM_STATS_BASE_POW10            (0x0 << KVM_STATS_BASE_SHIFT)
+     #define KVM_STATS_BASE_POW2             (0x1 << KVM_STATS_BASE_SHIFT)
+     #define KVM_STATS_BASE_MAX              KVM_STATS_BASE_POW2
+
+     struct kvm_stats_desc {
+             __u32 flags;
+             __s16 exponent;
+             __u16 size;
+             __u32 offset;
+             __u32 unused;
+             char name[0];
+     };
+
+The ``flags`` field contains the type and unit of the statistics data described
+by this descriptor. The following flags are supported:
+
+Bits 0-3 of ``flags`` encode the type:
+  * ``KVM_STATS_TYPE_CUMULATIVE``
+    The statistics data is cumulative. The value of data can only be increased.
+    Most of the counters used in KVM are of this type.
+    The corresponding ``count`` field for this type is always 1.
+  * ``KVM_STATS_TYPE_INSTANT``
+    The statistics data is instantaneous. Its value can be increased or
+    decreased. This type is usually used as a measurement of some resources,
+    like the number of dirty pages, the number of large pages, etc.
+    The corresponding ``count`` field for this type is always 1.
+
+Bits 4-7 of ``flags`` encode the unit:
+  * ``KVM_STATS_UNIT_NONE``
+    There is no unit for the value of statistics data. This usually means that
+    the value is a simple counter of an event.
+  * ``KVM_STATS_UNIT_BYTES``
+    It indicates that the statistics data is used to measure memory size, in the
+    unit of Byte, KiByte, MiByte, GiByte, etc. The unit of the data is
+    determined by the ``exponent`` field in the descriptor. The
+    ``KVM_STATS_BASE_POW2`` flag is valid in this case. The unit of the data is
+    determined by ``pow(2, exponent)``. For example, if value is 10,
+    ``exponent`` is 20, which means the unit of statistics data is MiByte, we
+    can get the statistics data in the unit of Byte by
+    ``value * pow(2, exponent) = 10 * pow(2, 20) = 10 MiByte`` which is
+    10 * 1024 * 1024 Bytes.
+  * ``KVM_STATS_UNIT_SECONDS``
+    It indicates that the statistics data is used to measure time/latency, in
+    the unit of nanosecond, microsecond, millisecond and second. The unit of the
+    data is determined by the ``exponent`` field in the descriptor. The
+    ``KVM_STATS_BASE_POW10`` flag is valid in this case. The unit of the data
+    is determined by ``pow(10, exponent)``. For example, if value is 2000000,
+    ``exponent`` is -6, which means the unit of statistics data is microsecond,
+    we can get the statistics data in the unit of second by
+    ``value * pow(10, exponent) = 2000000 * pow(10, -6) = 2 seconds``.
+  * ``KVM_STATS_UNIT_CYCLES``
+    It indicates that the statistics data is used to measure CPU clock cycles.
+    The ``KVM_STATS_BASE_POW10`` flag is valid in this case. For example, if
+    value is 200, ``exponent`` is 4, we can get the number of CPU clock cycles
+    by ``value * pow(10, exponent) = 200 * pow(10, 4) = 2000000``.
+
+Bits 8-11 of ``flags`` encode the base:
+  * ``KVM_STATS_BASE_POW10``
+    The scale is based on power of 10. It is used for measurement of time and
+    CPU clock cycles.
+  * ``KVM_STATS_BASE_POW2``
+    The scale is based on power of 2. It is used for measurement of memory size.
Quoted 10 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5225

Quoted 5 lines
Documentation/virt/kvm/api.rst +5 -0
+
+The ``exponent`` field is the scale of corresponding statistics data. For
+example, if the unit is ``KVM_STATS_UNIT_BYTES``, the base is
+``KVM_STATS_BASE_POW2``, the ``exponent`` is 10, then we know that the real
+unit of the statistics data is KBytes a.k.a pow(2, 10) = 1024 bytes.

Might also want to show a negative example here for exponent, like you show above for time.

Will do.

Quoted 5 lines

Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5228

Quoted 3 lines
Documentation/virt/kvm/api.rst +3 -0
+
+The ``size`` field is the number of values (u64) of this statistics data. Its
+value is usually 1 for most of simple statistics.

What does "u64" mean here?

It means the type of value is unsigned 64 bit. Will clarify this.

Quoted 4 lines

thanks,

greg k-h

Thanks,
Jing

#23 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

In reply to Jing Zhang
Quoted 11 lines · Jing Zhang

This new API provides a file descriptor for every VM and VCPU to read KVM statistics data in binary format.
It is meant to provide a lightweight, flexible, scalable and efficient lock-free solution for user space telemetry applications to pull the statistics data periodically for large scale systems. The pulling frequency could be as high as a few times per second.
The statistics descriptors are defined by KVM in kernel and can be by userspace to discover VM/VCPU statistics during the one-time setup stage.
The statistics data itself could be read out by userspace telemetry periodically without any extra parsing or setup effort.

Do you have a pointer to userspace code that can do such a thing that others can use? We do not like adding apis to the kernel without at least seeing the user of those apis, especially for complex things like this.

Ideally you would include some library code in the kernel tree itself that everyone can use for this for their own programs. You have provided a test which is great, but how do we know it works for "real" usages?

thanks,

greg k-h

maintainer
#24 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Quoted 11 lines · Greg KH
Quoted 2 lines

The statistics data itself could be read out by userspace telemetry periodically without any extra parsing or setup effort.

Do you have a pointer to userspace code that can do such a thing that others can use? We do not like adding apis to the kernel without at least seeing the user of those apis, especially for complex things like this.

Ideally you would include some library code in the kernel tree itself that everyone can use for this for their own programs. You have provided a test which is great, but how do we know it works for "real" usages?

I am pretty sure that Google is using this internally, but we are also going to work on QEMU and Libvirt support for this.

As for the rest, thanks for the review---I'll let Jing act on it and only add my own remarks in a couple places.

Paolo

#25 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Greg KH
Quoted 2 lines

The statistics data itself could be read out by userspace telemetry periodically without any extra parsing or setup effort.

Do you have a pointer to userspace code that can do such a thing that others can use? We do not like adding apis to the kernel without at least seeing the user of those apis, especially for complex things like this.

Quoted 7 lines
Quoted 4 lines

Ideally you would include some library code in the kernel tree itself that everyone can use for this for their own programs. You have provided a test which is great, but how do we know it works for "real" usages?

I am pretty sure that Google is using this internally, but we are also going to work on QEMU and Libvirt support for this.

We need an "external user" for something as complex as this to be able to see if it actually works or not. Otherwise we have to just guess :(

thanks,

greg k-h

author
#26 lore raw

Re: [PATCH v10 3/5] KVM: stats: Add documentation for binary statistics interface

Quoted 15 lines · Greg KH
Quoted 13 lines · Paolo Bonzini
Quoted 11 lines · Greg KH
Quoted 2 lines

The statistics data itself could be read out by userspace telemetry periodically without any extra parsing or setup effort.

Do you have a pointer to userspace code that can do such a thing that others can use? We do not like adding apis to the kernel without at least seeing the user of those apis, especially for complex things like this.

Ideally you would include some library code in the kernel tree itself that everyone can use for this for their own programs. You have provided a test which is great, but how do we know it works for "real" usages?

Quoted 5 lines
Quoted 2 lines

I am pretty sure that Google is using this internally, but we are also going to work on QEMU and Libvirt support for this.

We need an "external user" for something as complex as this to be able to see if it actually works or not. Otherwise we have to just guess :(

We have plans to add some library code in kernel selftests and also some simple tools to let everyone get a feeling of the new API.

Quoted 4 lines

thanks,

greg k-h

Thanks,
Jing

author
#27 lore raw

PATCH v10 4/5 KVM: selftests: Add selftest for KVM statistics data binary interface

In reply to Jing Zhang

Add selftest to check KVM stats descriptors validity.

---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   3 +
 .../testing/selftests/kvm/include/kvm_util.h  |   3 +
 .../selftests/kvm/kvm_binary_stats_test.c     | 225 ++++++++++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    |  12 +
 5 files changed, 244 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/kvm_binary_stats_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
index e0e14150744e..ebe81d67c738 100644
@@ -46,3 +46,4 @@
 /memslot_perf_test
 /set_memory_region_test
 /steal_time
+/kvm_binary_stats_test
tools/testing/selftests/kvm/Makefile +3 -0
index 61e2accd080d..8e4eaf8cdc06 100644
@@ -79,6 +79,7 @@ TEST_GEN_PROGS_x86_64 += memslot_modification_stress_test
 TEST_GEN_PROGS_x86_64 += memslot_perf_test
 TEST_GEN_PROGS_x86_64 += set_memory_region_test
 TEST_GEN_PROGS_x86_64 += steal_time
+TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
 
 TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
 TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list-sve
@@ -90,6 +91,7 @@ TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
 TEST_GEN_PROGS_aarch64 += kvm_page_table_test
 TEST_GEN_PROGS_aarch64 += set_memory_region_test
 TEST_GEN_PROGS_aarch64 += steal_time
+TEST_GEN_PROGS_aarch64 += kvm_binary_stats_test
 
 TEST_GEN_PROGS_s390x = s390x/memop
 TEST_GEN_PROGS_s390x += s390x/resets
@@ -99,6 +101,7 @@ TEST_GEN_PROGS_s390x += dirty_log_test
 TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
 TEST_GEN_PROGS_s390x += kvm_page_table_test
 TEST_GEN_PROGS_s390x += set_memory_region_test
+TEST_GEN_PROGS_s390x += kvm_binary_stats_test
 
 TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
 LIBKVM += $(LIBKVM_$(UNAME_M))
tools/testing/selftests/kvm/include/kvm_util.h +3 -0
index 35739567189e..558b450f3d7c 100644
@@ -391,4 +391,7 @@ uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc);
 #define GUEST_ASSERT_4(_condition, arg1, arg2, arg3, arg4) \
 	__GUEST_ASSERT((_condition), 4, (arg1), (arg2), (arg3), (arg4))
 
+int vm_get_stats_fd(struct kvm_vm *vm);
+int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid);
+
 #endif /* SELFTEST_KVM_UTIL_H */
tools/testing/selftests/kvm/kvm_binary_stats_test.c new file +224 -0

224 lines not on this page · raw message

tools/testing/selftests/kvm/lib/kvm_util.c +12 -0
index 5c70596dd1b9..83c02cb0ae1e 100644
@@ -2286,3 +2286,15 @@ unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size)
 	n = DIV_ROUND_UP(size, vm_guest_mode_params[mode].page_size);
 	return vm_adjust_num_guest_pages(mode, n);
 }
+
+int vm_get_stats_fd(struct kvm_vm *vm)
+{
+	return ioctl(vm->fd, KVM_GET_STATS_FD, NULL);
+}
+
+int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid)
+{
+	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
+
+	return ioctl(vcpu->fd, KVM_GET_STATS_FD, NULL);
+}
Signature · 1 line
2.32.0.272.g935e593368-goog
author
#28 lore raw

PATCH v10 5/5 KVM: stats: Remove code duplication for binary and debugfs stats

In reply to Jing Zhang

To remove code duplication, use the binary stats descriptors for debugfs interface for KVM stats. Then we only have one stats definitions for both binary and debugfs interface.

---
 arch/arm64/kvm/guest.c    |  16 ------
 arch/mips/kvm/mips.c      |  39 --------------
 arch/powerpc/kvm/book3s.c |  33 ------------
 arch/powerpc/kvm/booke.c  |  25 ---------
 arch/s390/kvm/kvm-s390.c  | 108 --------------------------------------
 arch/x86/kvm/x86.c        |  49 +----------------
 include/linux/kvm_host.h  |  43 +++++----------
 include/uapi/linux/kvm.h  |   4 +-
 virt/kvm/kvm_main.c       | 104 ++++++++++++++++++++++++++----------
 9 files changed, 94 insertions(+), 327 deletions(-)
arch/arm64/kvm/guest.c +0 -16
index 99842e29c2de..59486a9fef1a 100644
@@ -66,22 +66,6 @@ struct _kvm_stats_header kvm_vcpu_stats_header = {
 	}
 };
 
-struct kvm_stats_debugfs_item debugfs_entries[] = {
-	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
-	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
-	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
-	VCPU_STAT("hvc_exit_stat", hvc_exit_stat),
-	VCPU_STAT("wfe_exit_stat", wfe_exit_stat),
-	VCPU_STAT("wfi_exit_stat", wfi_exit_stat),
-	VCPU_STAT("mmio_exit_user", mmio_exit_user),
-	VCPU_STAT("mmio_exit_kernel", mmio_exit_kernel),
-	VCPU_STAT("exits", exits),
-	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
-	{ NULL }
-};
-
 static bool core_reg_offset_is_vreg(u64 off)
 {
 	return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) &&
arch/mips/kvm/mips.c +0 -39
index 67404f0947aa..606fe3b47075 100644
@@ -99,45 +99,6 @@ struct _kvm_stats_header kvm_vcpu_stats_header = {
 	}
 };
 
-struct kvm_stats_debugfs_item debugfs_entries[] = {
-	VCPU_STAT("wait", wait_exits),
-	VCPU_STAT("cache", cache_exits),
-	VCPU_STAT("signal", signal_exits),
-	VCPU_STAT("interrupt", int_exits),
-	VCPU_STAT("cop_unusable", cop_unusable_exits),
-	VCPU_STAT("tlbmod", tlbmod_exits),
-	VCPU_STAT("tlbmiss_ld", tlbmiss_ld_exits),
-	VCPU_STAT("tlbmiss_st", tlbmiss_st_exits),
-	VCPU_STAT("addrerr_st", addrerr_st_exits),
-	VCPU_STAT("addrerr_ld", addrerr_ld_exits),
-	VCPU_STAT("syscall", syscall_exits),
-	VCPU_STAT("resvd_inst", resvd_inst_exits),
-	VCPU_STAT("break_inst", break_inst_exits),
-	VCPU_STAT("trap_inst", trap_inst_exits),
-	VCPU_STAT("msa_fpe", msa_fpe_exits),
-	VCPU_STAT("fpe", fpe_exits),
-	VCPU_STAT("msa_disabled", msa_disabled_exits),
-	VCPU_STAT("flush_dcache", flush_dcache_exits),
-	VCPU_STAT("vz_gpsi", vz_gpsi_exits),
-	VCPU_STAT("vz_gsfc", vz_gsfc_exits),
-	VCPU_STAT("vz_hc", vz_hc_exits),
-	VCPU_STAT("vz_grr", vz_grr_exits),
-	VCPU_STAT("vz_gva", vz_gva_exits),
-	VCPU_STAT("vz_ghfc", vz_ghfc_exits),
-	VCPU_STAT("vz_gpa", vz_gpa_exits),
-	VCPU_STAT("vz_resvd", vz_resvd_exits),
-#ifdef CONFIG_CPU_LOONGSON64
-	VCPU_STAT("vz_cpucfg", vz_cpucfg_exits),
-#endif
-	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
-	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
-	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
-	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
-	{NULL}
-};
-
 bool kvm_trace_guest_mode_change;
 
 int kvm_guest_mode_change_trace_reg(void)
arch/powerpc/kvm/book3s.c +0 -33
index 15436484e521..0c5bc4bf488c 100644
@@ -102,39 +102,6 @@ struct _kvm_stats_header kvm_vcpu_stats_header = {
 	}
 };
 
-struct kvm_stats_debugfs_item debugfs_entries[] = {
-	VCPU_STAT("exits", sum_exits),
-	VCPU_STAT("mmio", mmio_exits),
-	VCPU_STAT("sig", signal_exits),
-	VCPU_STAT("sysc", syscall_exits),
-	VCPU_STAT("inst_emu", emulated_inst_exits),
-	VCPU_STAT("dec", dec_exits),
-	VCPU_STAT("ext_intr", ext_intr_exits),
-	VCPU_STAT("queue_intr", queue_intr),
-	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
-	VCPU_STAT("halt_wait_ns", halt_wait_ns),
-	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
-	VCPU_STAT("halt_successful_wait", halt_successful_wait),
-	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
-	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
-	VCPU_STAT("pf_storage", pf_storage),
-	VCPU_STAT("sp_storage", sp_storage),
-	VCPU_STAT("pf_instruc", pf_instruc),
-	VCPU_STAT("sp_instruc", sp_instruc),
-	VCPU_STAT("ld", ld),
-	VCPU_STAT("ld_slow", ld_slow),
-	VCPU_STAT("st", st),
-	VCPU_STAT("st_slow", st_slow),
-	VCPU_STAT("pthru_all", pthru_all),
-	VCPU_STAT("pthru_host", pthru_host),
-	VCPU_STAT("pthru_bad_aff", pthru_bad_aff),
-	VM_STAT("largepages_2M", num_2M_pages, .mode = 0444),
-	VM_STAT("largepages_1G", num_1G_pages, .mode = 0444),
-	{ NULL }
-};
-
 static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
 			unsigned long pending_now, unsigned long old_pending)
 {
arch/powerpc/kvm/booke.c +0 -25
index fbc9b7b6af57..888885b574cf 100644
@@ -93,31 +93,6 @@ struct _kvm_stats_header kvm_vcpu_stats_header = {
 	}
 };
 
-struct kvm_stats_debugfs_item debugfs_entries[] = {
-	VCPU_STAT("mmio", mmio_exits),
-	VCPU_STAT("sig", signal_exits),
-	VCPU_STAT("itlb_r", itlb_real_miss_exits),
-	VCPU_STAT("itlb_v", itlb_virt_miss_exits),
-	VCPU_STAT("dtlb_r", dtlb_real_miss_exits),
-	VCPU_STAT("dtlb_v", dtlb_virt_miss_exits),
-	VCPU_STAT("sysc", syscall_exits),
-	VCPU_STAT("isi", isi_exits),
-	VCPU_STAT("dsi", dsi_exits),
-	VCPU_STAT("inst_emu", emulated_inst_exits),
-	VCPU_STAT("dec", dec_exits),
-	VCPU_STAT("ext_intr", ext_intr_exits),
-	VCPU_STAT_GENERIC("halt_successful_poll", halt_successful_poll),
-	VCPU_STAT_GENERIC("halt_attempted_poll", halt_attempted_poll),
-	VCPU_STAT_GENERIC("halt_poll_invalid", halt_poll_invalid),
-	VCPU_STAT_GENERIC("halt_wakeup", halt_wakeup),
-	VCPU_STAT("doorbell", dbell_exits),
-	VCPU_STAT("guest doorbell", gdbell_exits),
-	VCPU_STAT_GENERIC("halt_poll_success_ns", halt_poll_success_ns),
-	VCPU_STAT_GENERIC("halt_poll_fail_ns", halt_poll_fail_ns),
-	VM_STAT_GENERIC("remote_tlb_flush", remote_tlb_flush),
-	{ NULL }
-};
-
 /* TODO: use vcpu_printf() */
 void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
 {
arch/s390/kvm/kvm-s390.c +0 -108

114 lines not on this page · raw message

arch/x86/kvm/x86.c +1 -48

61 lines not on this page · raw message

include/linux/kvm_host.h +14 -29

78 lines not on this page · raw message

include/uapi/linux/kvm.h +3 -1
index d6e97b577d01..81ad814cdf24 100644
@@ -1920,7 +1920,9 @@ struct kvm_stats_header {
 #define KVM_STATS_TYPE_MASK		(0xF << KVM_STATS_TYPE_SHIFT)
 #define KVM_STATS_TYPE_CUMULATIVE	(0x0 << KVM_STATS_TYPE_SHIFT)
 #define KVM_STATS_TYPE_INSTANT		(0x1 << KVM_STATS_TYPE_SHIFT)
-#define KVM_STATS_TYPE_MAX		KVM_STATS_TYPE_INSTANT
+/* Peak type is used as read/write instant counters */
+#define KVM_STATS_TYPE_PEAK		(0x2 << KVM_STATS_TYPE_SHIFT)
+#define KVM_STATS_TYPE_MAX		KVM_STATS_TYPE_PEAK
 
 #define KVM_STATS_UNIT_SHIFT		4
 #define KVM_STATS_UNIT_MASK		(0xF << KVM_STATS_UNIT_SHIFT)
virt/kvm/kvm_main.c +76 -28

209 lines not on this page · raw message

Signature · 1 line
2.32.0.272.g935e593368-goog