* [RFC PATCH 0/3] KVM: Rework how halt-polling stats and per-VM debugfs use memory
@ 2026-11-16 3:27 Peng Hao
2026-11-16 3:27 ` [RFC PATCH 1/3] KVM: Add the halt-polling time to the kvm_vcpu_wakeup tracepoint Peng Hao
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Peng Hao @ 2026-11-16 3:27 UTC (permalink / raw)
To: pbonzini, seanjc; +Cc: kvm
This series replaces the halt-polling histogram Kconfig patch, as a first
step towards a general plan for KVM stats.
Take a VM with 8 vCPUs on x86. Each vCPU has 1104 bytes of stats, 768 of
which are the three halt-polling histograms, i.e. 6KiB for the VM. The VM
also gets ~95 debugfs entries (one file per stat, plus a directory and a few
files per vCPU), each costing a dentry and an inode, i.e. ~70KiB, all
duplicating the binary stats.
The plan:
1. Counters stay embedded in the stats structs; they're cheap and
monitoring wants them always on.
2. Arrays and histograms move to a separately allocated block, controlled
by kvm.extended_stats (default on). The binary stats ABI doesn't change;
the values read as zero if the block isn't allocated. (patch 3)
3. Debug-only data goes to tracepoints and is aggregated with BPF. Adding
the halt-polling time to kvm_vcpu_wakeup is enough to rebuild the three
histograms. (patch 1)
In addition, kvm.per_vm_debugfs (default on) allows skipping the per-VM
debugfs directories. (patch 2)
With patch 3, struct vcpu_vmx shrinks from 8384 to 7616 bytes and struct
vcpu_svm from 6728 to 5968 bytes.
Peng Hao (3):
KVM: Add the halt-polling time to the kvm_vcpu_wakeup tracepoint
KVM: Add a module param to disable the per-VM debugfs directories
KVM: Move the halt-polling histograms out of struct kvm_vcpu_stat
.../admin-guide/kernel-parameters.txt | 13 ++++
arch/powerpc/kvm/book3s_hv.c | 11 ++-
include/linux/kvm_host.h | 24 +++++-
include/linux/kvm_types.h | 10 ++-
include/trace/events/kvm.h | 16 ++--
virt/kvm/binary_stats.c | 29 +++++++-
virt/kvm/kvm_main.c | 73 +++++++++++++++----
7 files changed, 144 insertions(+), 32 deletions(-)
--
2.43.7
^ permalink raw reply [flat|nested] 5+ messages in thread
* [RFC PATCH 1/3] KVM: Add the halt-polling time to the kvm_vcpu_wakeup tracepoint
2026-11-16 3:27 [RFC PATCH 0/3] KVM: Rework how halt-polling stats and per-VM debugfs use memory Peng Hao
@ 2026-11-16 3:27 ` Peng Hao
2026-11-16 3:27 ` [RFC PATCH 2/3] KVM: Add a module param to disable the per-VM debugfs directories Peng Hao
2026-11-16 3:27 ` [RFC PATCH 3/3] KVM: Move the halt-polling histograms out of struct kvm_vcpu_stat Peng Hao
2 siblings, 0 replies; 5+ messages in thread
From: Peng Hao @ 2026-11-16 3:27 UTC (permalink / raw)
To: pbonzini, seanjc; +Cc: kvm
kvm_vcpu_wakeup only reports the total halt time, which includes both the
time spent halt-polling and the time spent blocking. Report the
halt-polling time as well, so that the split between polling and waiting
can be derived from the tracepoint, e.g. to build histograms equivalent to
the halt_poll_success_hist, halt_poll_fail_hist and halt_wait_hist stats
with BPF.
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
include/trace/events/kvm.h | 16 +++++++++-------
virt/kvm/kvm_main.c | 3 ++-
2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h
index 07013f88e655..d03e3954ca37 100644
--- a/include/trace/events/kvm.h
+++ b/include/trace/events/kvm.h
@@ -47,24 +47,26 @@ TRACE_EVENT(kvm_userspace_exit,
);
TRACE_EVENT(kvm_vcpu_wakeup,
- TP_PROTO(__u64 ns, bool waited, bool valid),
- TP_ARGS(ns, waited, valid),
+ TP_PROTO(__u64 ns, __u64 poll_ns, bool waited, bool valid),
+ TP_ARGS(ns, poll_ns, waited, valid),
TP_STRUCT__entry(
- __field( __u64, ns )
- __field( bool, waited )
- __field( bool, valid )
+ __field(__u64, ns)
+ __field(__u64, poll_ns)
+ __field(bool, waited)
+ __field(bool, valid)
),
TP_fast_assign(
__entry->ns = ns;
+ __entry->poll_ns = poll_ns;
__entry->waited = waited;
__entry->valid = valid;
),
- TP_printk("%s time %lld ns, polling %s",
+ TP_printk("%s time %lld ns (poll %lld ns), polling %s",
__entry->waited ? "wait" : "poll",
- __entry->ns,
+ __entry->ns, __entry->poll_ns,
__entry->valid ? "valid" : "invalid")
);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index cf4cb586f140..fff19e9239d2 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3888,7 +3888,8 @@ void kvm_vcpu_halt(struct kvm_vcpu *vcpu)
}
}
- trace_kvm_vcpu_wakeup(halt_ns, waited, vcpu_valid_wakeup(vcpu));
+ trace_kvm_vcpu_wakeup(halt_ns, ktime_to_ns(ktime_sub(poll_end, start)),
+ waited, vcpu_valid_wakeup(vcpu));
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_halt);
--
2.43.7
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [RFC PATCH 2/3] KVM: Add a module param to disable the per-VM debugfs directories
2026-11-16 3:27 [RFC PATCH 0/3] KVM: Rework how halt-polling stats and per-VM debugfs use memory Peng Hao
2026-11-16 3:27 ` [RFC PATCH 1/3] KVM: Add the halt-polling time to the kvm_vcpu_wakeup tracepoint Peng Hao
@ 2026-11-16 3:27 ` Peng Hao
2026-11-16 3:43 ` sashiko-bot
2026-11-16 3:27 ` [RFC PATCH 3/3] KVM: Move the halt-polling histograms out of struct kvm_vcpu_stat Peng Hao
2 siblings, 1 reply; 5+ messages in thread
From: Peng Hao @ 2026-11-16 3:27 UTC (permalink / raw)
To: pbonzini, seanjc; +Cc: kvm
Every VM gets a debugfs directory with one file per stat, plus a directory
and a few files per vCPU, i.e. ~95 entries for an 8-vCPU VM on x86. Each
entry costs a dentry and an inode, and the stats files duplicate the
binary stats.
Add kvm.per_vm_debugfs (default on) so that these directories can be
skipped.
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
Documentation/admin-guide/kernel-parameters.txt | 7 +++++++
virt/kvm/kvm_main.c | 12 ++++++++++--
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 42ff19defe16..c1e0f6bf4c24 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3211,6 +3211,13 @@ Kernel parameters
If the value is 0 (the default), KVM will pick a period based
on the ratio, such that a page is zapped after 1 hour on average.
+ kvm.per_vm_debugfs=
+ [KVM] Create a debugfs directory for each VM and vCPU,
+ containing per-VM stats and architecture specific
+ debug files. Global stats in debugfs and binary stats
+ are not affected.
+ Default is Y (on).
+
kvm-{amd,intel}.enable_mediated_pmu=[KVM,AMD,INTEL]
If enabled, KVM will provide a mediated virtual PMU,
instead of the default perf-based virtual PMU (if
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index fff19e9239d2..54a2924f9b92 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -104,6 +104,14 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(halt_poll_ns_shrink);
static bool __ro_after_init allow_unsafe_mappings;
module_param(allow_unsafe_mappings, bool, 0444);
+/*
+ * Create a debugfs directory for every VM and vCPU. The per-VM stats files
+ * duplicate the binary stats, and each file costs a dentry and an inode, which
+ * adds up when running many VMs.
+ */
+static bool __ro_after_init per_vm_debugfs = true;
+module_param(per_vm_debugfs, bool, 0444);
+
#ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
bool __ro_after_init gmem_in_place_conversion = false;
module_param(gmem_in_place_conversion, bool, 0444);
@@ -1024,7 +1032,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc +
kvm_vcpu_stats_header.num_desc;
- if (!debugfs_initialized())
+ if (!debugfs_initialized() || !per_vm_debugfs)
return 0;
snprintf(dir_name, sizeof(dir_name), "%d-%s", task_pid_nr(current), fdname);
@@ -4235,7 +4243,7 @@ static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
struct dentry *debugfs_dentry;
char dir_name[ITOA_MAX_LEN * 2];
- if (!debugfs_initialized())
+ if (!debugfs_initialized() || !per_vm_debugfs)
return;
snprintf(dir_name, sizeof(dir_name), "vcpu%d", vcpu->vcpu_id);
--
2.43.7
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [RFC PATCH 3/3] KVM: Move the halt-polling histograms out of struct kvm_vcpu_stat
2026-11-16 3:27 [RFC PATCH 0/3] KVM: Rework how halt-polling stats and per-VM debugfs use memory Peng Hao
2026-11-16 3:27 ` [RFC PATCH 1/3] KVM: Add the halt-polling time to the kvm_vcpu_wakeup tracepoint Peng Hao
2026-11-16 3:27 ` [RFC PATCH 2/3] KVM: Add a module param to disable the per-VM debugfs directories Peng Hao
@ 2026-11-16 3:27 ` Peng Hao
2 siblings, 0 replies; 5+ messages in thread
From: Peng Hao @ 2026-11-16 3:27 UTC (permalink / raw)
To: pbonzini, seanjc; +Cc: kvm
The halt_poll_success_hist, halt_poll_fail_hist and halt_wait_hist
histograms take 768 bytes in every vCPU, out of 1104 bytes of stats on
x86. Move them into a separately allocated struct kvm_vcpu_stat_ext, and
add kvm.extended_stats (default on) so that it can be skipped.
The descriptors don't change and the extended stats are laid out after
struct kvm_vcpu_stat in the stats data block, so the binary stats ABI is
unchanged; if the block isn't allocated the histograms read as zero.
This shrinks struct kvm_vcpu from 5776 to 5016 bytes, struct vcpu_vmx from
8384 to 7616 bytes and struct vcpu_svm from 6728 to 5968 bytes.
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
.../admin-guide/kernel-parameters.txt | 6 ++
arch/powerpc/kvm/book3s_hv.c | 11 ++--
include/linux/kvm_host.h | 24 ++++++--
include/linux/kvm_types.h | 10 +++-
virt/kvm/binary_stats.c | 29 +++++++++-
virt/kvm/kvm_main.c | 58 +++++++++++++++----
6 files changed, 116 insertions(+), 22 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index c1e0f6bf4c24..181f9a920b26 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3159,6 +3159,12 @@ Kernel parameters
kvm.enable_vmware_backdoor=[KVM] Support VMware backdoor PV interface.
Default is false (don't support).
+ kvm.extended_stats=
+ [KVM] Allocate the extended per-vCPU stats, e.g. the
+ halt-polling histograms. If disabled, the extended
+ stats are still reported but always read as zero.
+ Default is Y (on).
+
kvm.gmem_in_place_conversion=
[KVM] Controls whether KVM enables in-place conversion
support for guest_memfd and tracks the private/shared
diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
index f2d9f16c2e44..fc9fba16ce48 100644
--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -4684,16 +4684,15 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
if (do_sleep) {
vc->runner->stat.generic.halt_wait_ns +=
ktime_to_ns(cur) - ktime_to_ns(start_wait);
- KVM_STATS_LOG_HIST_UPDATE(
- vc->runner->stat.generic.halt_wait_hist,
+ KVM_VCPU_STATS_EXT_LOG_HIST_UPDATE(vc->runner, halt_wait_hist,
ktime_to_ns(cur) - ktime_to_ns(start_wait));
/* Attribute failed poll time */
if (vc->halt_poll_ns) {
vc->runner->stat.generic.halt_poll_fail_ns +=
ktime_to_ns(start_wait) -
ktime_to_ns(start_poll);
- KVM_STATS_LOG_HIST_UPDATE(
- vc->runner->stat.generic.halt_poll_fail_hist,
+ KVM_VCPU_STATS_EXT_LOG_HIST_UPDATE(vc->runner,
+ halt_poll_fail_hist,
ktime_to_ns(start_wait) -
ktime_to_ns(start_poll));
}
@@ -4703,8 +4702,8 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
vc->runner->stat.generic.halt_poll_success_ns +=
ktime_to_ns(cur) -
ktime_to_ns(start_poll);
- KVM_STATS_LOG_HIST_UPDATE(
- vc->runner->stat.generic.halt_poll_success_hist,
+ KVM_VCPU_STATS_EXT_LOG_HIST_UPDATE(vc->runner,
+ halt_poll_success_hist,
ktime_to_ns(cur) - ktime_to_ns(start_poll));
}
}
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index e7fc94e2910a..fd07a626fdaf 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -391,6 +391,7 @@ struct kvm_vcpu {
bool scheduled_out;
struct kvm_vcpu_arch arch;
struct kvm_vcpu_stat stat;
+ struct kvm_vcpu_stat_ext *stat_ext;
char stats_id[KVM_STATS_NAME_SIZE];
struct kvm_dirty_ring dirty_ring;
@@ -2029,7 +2030,15 @@ struct kvm_stat_data {
.offset = offsetof(struct kvm_vcpu_stat, stat), \
.name = #stat, \
}
-/* SCOPE: VM, VM_GENERIC, VCPU, VCPU_GENERIC */
+/* Extended vCPU stats are laid out after struct kvm_vcpu_stat. */
+#define VCPU_EXT_STATS_DESC(stat, type, unit, base, exp, sz, bsz) \
+{ \
+ STATS_DESC_COMMON(type, unit, base, exp, sz, bsz), \
+ .offset = sizeof(struct kvm_vcpu_stat) + \
+ offsetof(struct kvm_vcpu_stat_ext, stat), \
+ .name = #stat, \
+}
+/* SCOPE: VM, VM_GENERIC, VCPU, VCPU_GENERIC, VCPU_EXT */
#define STATS_DESC(SCOPE, stat, type, unit, base, exp, sz, bsz) \
SCOPE##_STATS_DESC(stat, type, unit, base, exp, sz, bsz)
@@ -2096,17 +2105,18 @@ struct kvm_stat_data {
STATS_DESC_TIME_NSEC(VCPU_GENERIC, halt_poll_success_ns), \
STATS_DESC_TIME_NSEC(VCPU_GENERIC, halt_poll_fail_ns), \
STATS_DESC_TIME_NSEC(VCPU_GENERIC, halt_wait_ns), \
- STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_poll_success_hist, \
+ STATS_DESC_LOGHIST_TIME_NSEC(VCPU_EXT, halt_poll_success_hist, \
HALT_POLL_HIST_COUNT), \
- STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_poll_fail_hist, \
+ STATS_DESC_LOGHIST_TIME_NSEC(VCPU_EXT, halt_poll_fail_hist, \
HALT_POLL_HIST_COUNT), \
- STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_wait_hist, \
+ STATS_DESC_LOGHIST_TIME_NSEC(VCPU_EXT, halt_wait_hist, \
HALT_POLL_HIST_COUNT), \
STATS_DESC_IBOOLEAN(VCPU_GENERIC, blocking)
ssize_t kvm_stats_read(char *id, const struct kvm_stats_header *header,
const struct kvm_stats_desc *desc,
void *stats, size_t size_stats,
+ void *stats_ext, size_t size_stats_ext,
char __user *user_buffer, size_t size, loff_t *offset);
/**
@@ -2148,6 +2158,12 @@ static inline void kvm_stats_log_hist_update(u64 *data, size_t size, u64 value)
#define KVM_STATS_LOG_HIST_UPDATE(array, value) \
kvm_stats_log_hist_update(array, ARRAY_SIZE(array), value)
+#define KVM_VCPU_STATS_EXT_LOG_HIST_UPDATE(vcpu, hist, value) \
+do { \
+ if ((vcpu)->stat_ext) \
+ KVM_STATS_LOG_HIST_UPDATE((vcpu)->stat_ext->hist, value); \
+} while (0)
+
extern const struct kvm_stats_header kvm_vm_stats_header;
extern const struct kvm_stats_desc kvm_vm_stats_desc[];
diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h
index a568d8e6f4e8..fd7426627928 100644
--- a/include/linux/kvm_types.h
+++ b/include/linux/kvm_types.h
@@ -133,10 +133,18 @@ struct kvm_vcpu_stat_generic {
u64 halt_poll_success_ns;
u64 halt_poll_fail_ns;
u64 halt_wait_ns;
+ u64 blocking;
+};
+
+/*
+ * vCPU stats that are too big to embed in every vCPU, e.g. histograms. They
+ * are allocated only if kvm.extended_stats is enabled, and read as zero
+ * otherwise.
+ */
+struct kvm_vcpu_stat_ext {
u64 halt_poll_success_hist[HALT_POLL_HIST_COUNT];
u64 halt_poll_fail_hist[HALT_POLL_HIST_COUNT];
u64 halt_wait_hist[HALT_POLL_HIST_COUNT];
- u64 blocking;
};
#define KVM_STATS_NAME_SIZE 48
diff --git a/virt/kvm/binary_stats.c b/virt/kvm/binary_stats.c
index 76ce697c773b..c4f8bed1f01d 100644
--- a/virt/kvm/binary_stats.c
+++ b/virt/kvm/binary_stats.c
@@ -19,6 +19,9 @@
* @desc: start address of an array of stats descriptors for a vm or a vcpu
* @stats: start address of stats data block for a vm or a vcpu
* @size_stats: the size of stats data block pointed by @stats
+ * @stats_ext: start address of the extended stats data block, which follows
+ * the stats data block, or NULL if it isn't allocated
+ * @size_stats_ext: the size of the extended stats data block
* @user_buffer: start address of userspace buffer
* @size: requested read size from userspace
* @offset: the start position from which the content will be read for the
@@ -34,6 +37,9 @@
* +-------------+
* | Stats Data |
* +-------------+
+ * | Extended |
+ * | Stats Data |
+ * +-------------+
* Although this function allows userspace to read any amount of data (as long
* as in the limit) from any position, the typical usage would follow below
* steps:
@@ -52,6 +58,7 @@
ssize_t kvm_stats_read(char *id, const struct kvm_stats_header *header,
const struct kvm_stats_desc *desc,
void *stats, size_t size_stats,
+ void *stats_ext, size_t size_stats_ext,
char __user *user_buffer, size_t size, loff_t *offset)
{
ssize_t len;
@@ -66,7 +73,8 @@ ssize_t kvm_stats_read(char *id, const struct kvm_stats_header *header,
size_header = sizeof(*header);
size_desc = header->num_desc * sizeof(*desc);
- len = KVM_STATS_NAME_SIZE + size_header + size_desc + size_stats - pos;
+ len = KVM_STATS_NAME_SIZE + size_header + size_desc + size_stats +
+ size_stats_ext - pos;
len = min(len, remain);
if (len <= 0)
return 0;
@@ -136,6 +144,25 @@ ssize_t kvm_stats_read(char *id, const struct kvm_stats_header *header,
src = stats + pos - header->data_offset;
if (copy_to_user(dest, src, copylen))
return -EFAULT;
+ remain -= copylen;
+ pos += copylen;
+ dest += copylen;
+ }
+
+ /*
+ * Copy extended kvm stats values, which read as zero if the extended
+ * stats data block isn't allocated.
+ */
+ copylen = header->data_offset + size_stats + size_stats_ext - pos;
+ copylen = min(copylen, remain);
+ if (copylen > 0) {
+ if (stats_ext) {
+ src = stats_ext + pos - header->data_offset - size_stats;
+ if (copy_to_user(dest, src, copylen))
+ return -EFAULT;
+ } else if (clear_user(dest, copylen)) {
+ return -EFAULT;
+ }
pos += copylen;
}
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 54a2924f9b92..210d1f658a21 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -112,6 +112,10 @@ module_param(allow_unsafe_mappings, bool, 0444);
static bool __ro_after_init per_vm_debugfs = true;
module_param(per_vm_debugfs, bool, 0444);
+/* Allocate the extended vCPU stats, e.g. histograms, see kvm_vcpu_stat_ext. */
+static bool __ro_after_init extended_stats = true;
+module_param(extended_stats, bool, 0444);
+
#ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
bool __ro_after_init gmem_in_place_conversion = false;
module_param(gmem_in_place_conversion, bool, 0444);
@@ -493,6 +497,7 @@ static void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
put_pid(vcpu->pid);
free_page((unsigned long)vcpu->run);
+ kfree(vcpu->stat_ext);
kmem_cache_free(kvm_vcpu_cache, vcpu);
}
@@ -3796,10 +3801,12 @@ static inline void update_halt_poll_stats(struct kvm_vcpu *vcpu, ktime_t start,
++vcpu->stat.generic.halt_poll_invalid;
stats->halt_poll_success_ns += poll_ns;
- KVM_STATS_LOG_HIST_UPDATE(stats->halt_poll_success_hist, poll_ns);
+ KVM_VCPU_STATS_EXT_LOG_HIST_UPDATE(vcpu, halt_poll_success_hist,
+ poll_ns);
} else {
stats->halt_poll_fail_ns += poll_ns;
- KVM_STATS_LOG_HIST_UPDATE(stats->halt_poll_fail_hist, poll_ns);
+ KVM_VCPU_STATS_EXT_LOG_HIST_UPDATE(vcpu, halt_poll_fail_hist,
+ poll_ns);
}
}
@@ -3859,7 +3866,7 @@ void kvm_vcpu_halt(struct kvm_vcpu *vcpu)
if (waited) {
vcpu->stat.generic.halt_wait_ns +=
ktime_to_ns(cur) - ktime_to_ns(poll_end);
- KVM_STATS_LOG_HIST_UPDATE(vcpu->stat.generic.halt_wait_hist,
+ KVM_VCPU_STATS_EXT_LOG_HIST_UPDATE(vcpu, halt_wait_hist,
ktime_to_ns(cur) - ktime_to_ns(poll_end));
}
out:
@@ -4307,11 +4314,19 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
}
vcpu->run = page_address(page);
+ if (extended_stats) {
+ vcpu->stat_ext = kzalloc_obj(*vcpu->stat_ext, GFP_KERNEL_ACCOUNT);
+ if (!vcpu->stat_ext) {
+ r = -ENOMEM;
+ goto vcpu_free_run_page;
+ }
+ }
+
kvm_vcpu_init(vcpu, kvm, id);
r = kvm_arch_vcpu_create(vcpu);
if (r)
- goto vcpu_free_run_page;
+ goto vcpu_free_stat_ext;
if (kvm->dirty_ring_size) {
r = kvm_dirty_ring_alloc(kvm, &vcpu->dirty_ring,
@@ -4366,6 +4381,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
kvm_dirty_ring_free(&vcpu->dirty_ring);
arch_vcpu_destroy:
kvm_arch_vcpu_destroy(vcpu);
+vcpu_free_stat_ext:
+ kfree(vcpu->stat_ext);
vcpu_free_run_page:
free_page((unsigned long)vcpu->run);
vcpu_free:
@@ -4393,7 +4410,8 @@ static ssize_t kvm_vcpu_stats_read(struct file *file, char __user *user_buffer,
return kvm_stats_read(vcpu->stats_id, &kvm_vcpu_stats_header,
&kvm_vcpu_stats_desc[0], &vcpu->stat,
- sizeof(vcpu->stat), user_buffer, size, offset);
+ sizeof(vcpu->stat), vcpu->stat_ext,
+ sizeof(*vcpu->stat_ext), user_buffer, size, offset);
}
static int kvm_vcpu_stats_release(struct inode *inode, struct file *file)
@@ -5216,7 +5234,8 @@ static ssize_t kvm_vm_stats_read(struct file *file, char __user *user_buffer,
return kvm_stats_read(kvm->stats_id, &kvm_vm_stats_header,
&kvm_vm_stats_desc[0], &kvm->stat,
- sizeof(kvm->stat), user_buffer, size, offset);
+ sizeof(kvm->stat), NULL, 0, user_buffer, size,
+ offset);
}
static int kvm_vm_stats_release(struct inode *inode, struct file *file)
@@ -6259,15 +6278,30 @@ static int kvm_clear_stat_per_vm(struct kvm *kvm, size_t offset)
return 0;
}
+static u64 *kvm_vcpu_stat_ptr(struct kvm_vcpu *vcpu, size_t offset)
+{
+ if (offset < sizeof(vcpu->stat))
+ return (void *)&vcpu->stat + offset;
+
+ if (!vcpu->stat_ext)
+ return NULL;
+
+ return (void *)vcpu->stat_ext + offset - sizeof(vcpu->stat);
+}
+
static int kvm_get_stat_per_vcpu(struct kvm *kvm, size_t offset, u64 *val)
{
unsigned long i;
struct kvm_vcpu *vcpu;
+ u64 *stat;
*val = 0;
- kvm_for_each_vcpu(i, vcpu, kvm)
- *val += *(u64 *)((void *)(&vcpu->stat) + offset);
+ kvm_for_each_vcpu(i, vcpu, kvm) {
+ stat = kvm_vcpu_stat_ptr(vcpu, offset);
+ if (stat)
+ *val += *stat;
+ }
return 0;
}
@@ -6276,9 +6310,13 @@ static int kvm_clear_stat_per_vcpu(struct kvm *kvm, size_t offset)
{
unsigned long i;
struct kvm_vcpu *vcpu;
+ u64 *stat;
- kvm_for_each_vcpu(i, vcpu, kvm)
- *(u64 *)((void *)(&vcpu->stat) + offset) = 0;
+ kvm_for_each_vcpu(i, vcpu, kvm) {
+ stat = kvm_vcpu_stat_ptr(vcpu, offset);
+ if (stat)
+ *stat = 0;
+ }
return 0;
}
--
2.43.7
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [RFC PATCH 2/3] KVM: Add a module param to disable the per-VM debugfs directories
2026-11-16 3:27 ` [RFC PATCH 2/3] KVM: Add a module param to disable the per-VM debugfs directories Peng Hao
@ 2026-11-16 3:43 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-16 3:43 UTC (permalink / raw)
To: Peng Hao; +Cc: kvm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: debugfs leak and UAF in
kvm_nested_s2_ptdump_create_debugfs()
--
commit 6b8cad730d46b448b05e32a4f22f6d9fe9cf399b
Author: Peng Hao <flyingpenghao@gmail.com>
KVM: Add a module param to disable the per-VM debugfs directories
This commit adds the kvm.per_vm_debugfs module parameter to allow skipping the
creation of per-VM and per-vCPU debugfs directories and files.
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index fff19e9239d2b..54a2924f9b928 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
[ ... ]
> @@ -1024,7 +1032,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
> int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc +
> kvm_vcpu_stats_header.num_desc;
>
> - if (!debugfs_initialized())
> + if (!debugfs_initialized() || !per_vm_debugfs)
> return 0;
[Severity: Medium]
On arm64 with CONFIG_PTDUMP_STAGE2_DEBUGFS=y, does returning early here when
per_vm_debugfs is false cause kvm_nested_s2_ptdump_create_debugfs() to leak
nested_mmu* files in the root of debugfs and leave dangling pointers?
kvm_create_vm() initializes kvm->debugfs_dentry to ERR_PTR(-ENOENT), so
callers passing it as a parent to debugfs_create_file() or
debugfs_create_dir() fail as expected. However, kvm->arch.debugfs_nv_dentry
is zero-allocated (NULL) and is only populated when kvm_create_vm_debugfs()
reaches kvm_arch_create_vm_debugfs() -> kvm_s2_ptdump_create_debugfs().
When per_vm_debugfs is false while debugfs_initialized() is true,
kvm->arch.debugfs_nv_dentry remains NULL. Initializing a nested vCPU in
kvm_vcpu_init_nested() then calls kvm_nested_s2_ptdump_create_debugfs(),
passing NULL as the parent dentry:
arch/arm64/kvm/ptdump.c:kvm_nested_s2_ptdump_create_debugfs() {
...
debugfs_create_file(file_name, 0400,
mmu->arch->debugfs_nv_dentry, mmu,
&kvm_ptdump_guest_fops);
}
debugfs interprets a NULL parent as the root of the debugfs mount and creates
/sys/kernel/debug/nested_mmu* with inode->i_private pointing to mmu. When
the VM is destroyed, these root files are never removed while
kvm_destroy_nested() frees the nested_mmus arrays and kvm_destroy_vm() frees
struct kvm, so opening /sys/kernel/debug/nested_mmu* afterwards triggers a
use-after-free in kvm_ptdump_guest_open().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116032748.54582-1-flyingpeng@tencent.com?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-16 3:43 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 3:27 [RFC PATCH 0/3] KVM: Rework how halt-polling stats and per-VM debugfs use memory Peng Hao
2026-11-16 3:27 ` [RFC PATCH 1/3] KVM: Add the halt-polling time to the kvm_vcpu_wakeup tracepoint Peng Hao
2026-11-16 3:27 ` [RFC PATCH 2/3] KVM: Add a module param to disable the per-VM debugfs directories Peng Hao
2026-11-16 3:43 ` sashiko-bot
2026-11-16 3:27 ` [RFC PATCH 3/3] KVM: Move the halt-polling histograms out of struct kvm_vcpu_stat Peng Hao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox