trace_hyp_printk() for pKVM/nVHE hypervisor
v2 cover trace_hyp_printk() for pKVM/nVHE hypervisor
Hi all,
This series adds a hypervisor event "hyp_printk" which enables developers to log pretty much anything into the hypervisor tracing buffer, just like the kernel function trace_printk().
This enables rich logging from the hypervisor, while leaving all the string parsing burden to the kernel. This has been the main way of debugging pKVM in Android.
Even though not strictly related to trace_hyp_printk, I have added the following two patches:
* KVM: arm64: Allow early calls to pKVM host_share/unshare_hyp
This one mainly intends to support one of the new features I have posted here [1], which allows to enable tracing as early as possible. I have added it here to limit cross-posting.
* KVM: arm64: Move kvm_define_hypevents.h to arch/arm64/kvm/
This one is just a cleanup.
[1] https://lore.kernel.org/all/[email protected]/
Changelog:
v2:
- Collect Fuad's tags
- Rebase on 7.2-rc2
- Warn when trace_hyp_printk callsites are exhausted (Fuad)
v1 (https://lore.kernel.org/all/[email protected]/)
Vincent Donnefort (4):
KVM: arm64: Allow early calls to pKVM host_share/unshare_hyp
KVM: arm64: Move kvm_define_hypevents.h to arch/arm64/kvm/ tracing/remotes: Add REMOTE_EVENT_CUSTOM_PRINTK() helper
KVM: arm64: Add hyp_printk event to nVHE/pKVM hyp
arch/arm64/include/asm/kvm_asm.h | 4 +-
arch/arm64/include/asm/kvm_hypevents.h | 14 ++++
arch/arm64/include/asm/kvm_hyptrace.h | 8 +++
arch/arm64/kernel/image-vars.h | 1 +
arch/arm64/kernel/vmlinux.lds.S | 4 ++
.../define_hypevents.h} | 0
.../kvm/hyp/include/nvhe/define_events.h | 2 -
arch/arm64/kvm/hyp/include/nvhe/trace.h | 65 +++++++++++++++++++
arch/arm64/kvm/hyp/nvhe/events.c | 6 ++
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 2 +-
arch/arm64/kvm/hyp_trace.c | 63 +++++++++++++++++-
include/trace/define_remote_events.h | 19 +++++-
12 files changed, 179 insertions(+), 9 deletions(-)
rename arch/arm64/{include/asm/kvm_define_hypevents.h => kvm/define_hypevents.h} (100%)base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH v2 1/4 KVM: arm64: Allow early calls to pKVM host_share/unshare_hyp
The hypervisor tracing for pKVM relies on the __pkvm_host_share_hyp and __pkvm_host_unshare_hyp HVCs. In order to start tracing as early as possible, allow those two HVCs before the host is deprivileged.
arch/arm64/include/asm/kvm_asm.h +2 -2
@@ -89,12 +89,12 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_vmcr_aprs, __KVM_HOST_SMCCC_FUNC___vgic_v5_save_apr, __KVM_HOST_SMCCC_FUNC___vgic_v5_restore_vmcr_apr, + __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, + __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp,MARKER (__KVM_HOST_SMCCC_FUNC_PKVM_ONLY),/* Hypercalls that are available only when pKVM has finalised. */ - __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, - __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_donate_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest,
arch/arm64/kvm/hyp/nvhe/hyp-main.c +1 -1
@@ -740,9 +740,9 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__vgic_v3_restore_vmcr_aprs),HANDLE_FUNC (__vgic_v5_save_apr),HANDLE_FUNC (__vgic_v5_restore_vmcr_apr), -HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_host_unshare_hyp), +HANDLE_FUNC (__pkvm_host_donate_guest),HANDLE_FUNC (__pkvm_host_share_guest), HANDLE_FUNC(__pkvm_host_unshare_guest),
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH v2 2/4 KVM: arm64: Move kvm_define_hypevents.h to arch/arm64/kvm/
In reply to Vincent Donnefortkvm_define_hypevents.h is used to define the kernel-side structures for hypervisor events. It doesn't need to be used anywhere else than in hyp_trace.c.
Rename and move it to arch/arm64/kvm/
arch/arm64/kvm/define_hypevents.h renamed from arch/arm64/include/asm/kvm_define_hypevents.h +0 -0
arch/arm64/kvm/hyp_trace.c +1 -1
@@ -391,7 +391,7 @@ static struct trace_remote_callbacks trace_remote_callbacks = {static const char *__hyp_enter_exit_reason_str (u8 reason); -#include <asm/kvm_define_hypevents.h> +#include "define_hypevents.h" static const char *__hyp_enter_exit_reason_str (u8 reason) {
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH v2 3/4 tracing/remotes: Add REMOTE_EVENT_CUSTOM_PRINTK() helper
In reply to Vincent DonnefortThe current REMOTE_EVENT() takes as a __printk argument a string format and a list of arguments, such as RE_STRUCT("foo=%d bar=%d", foo, bar). Add a REMOTE_EVENT_CUSTOM_PRINTK() where the __printk argument can be a function. This intends to support the creation of a "printk" event for the arm64 nVHE/pKVM hypervisor with a dynamic prototype and by extension a dynamic print format.
include/trace/define_remote_events.h +16 -3
@@ -35,17 +35,26 @@ do { \ #define RE_PRINTK(__args...) __args -#define REMOTE_EVENT (__name, __id, __struct, __printk) \ - REMOTE_EVENT_FORMAT(__name, __struct); \ +#define REMOTE_EVENT_PRINT_FUNC (__name, __printk) \ static void remote_event_print_##__name(void *evt, struct trace_seq *seq) \ { \ struct remote_event_format_##__name __maybe_unused *__entry = evt; \ trace_seq_puts(seq, #__name); \ - remote_printk(__printk); \ + __printk; \ } + +#define REMOTE_EVENT (__name, __id, __struct, __printk) \ + REMOTE_EVENT_FORMAT(__name, __struct); \ + REMOTE_EVENT_PRINT_FUNC(__name, remote_printk(__printk)) + +#define REMOTE_EVENT_CUSTOM_PRINTK (__name, __id, __struct, __printk) \ + REMOTE_EVENT_FORMAT(__name, __struct); \ + REMOTE_EVENT_PRINT_FUNC(__name, __printk) +#include REMOTE_EVENT_INCLUDE (REMOTE_EVENT_INCLUDE_FILE )#undef REMOTE_EVENT +#undef REMOTE_EVENT_CUSTOM_PRINTK#undef RE_PRINTK#undef re_field#define re_field (__type, __field) \
@@ -70,4 +79,8 @@ do { \ .print_fmt = remote_event_print_fmt_##__name, \ .print = remote_event_print_##__name, \ } + +#define REMOTE_EVENT_CUSTOM_PRINTK (__name, __id, __struct, __printk) \ + REMOTE_EVENT(__name, __id, RE_STRUCT(__struct), "Unknown") +#include REMOTE_EVENT_INCLUDE (REMOTE_EVENT_INCLUDE_FILE )
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Re: [PATCH v2 3/4] tracing/remotes: Add REMOTE_EVENT_CUSTOM_PRINTK() helper
Quoted 10 lines · On Wed, 8 Jul 2026 08:54:34 +0100 Vincent Donnefort <[email protected]>…
The current
REMOTE_EVENT()takes as a__printkargument a string format and a list of arguments, such asRE_STRUCT("foo=%d bar=%d", foo, bar). Add aREMOTE_EVENT_CUSTOM_PRINTK()where the__printkargument can be a function. This intends to support the creation of a "printk" event for the arm64 nVHE/pKVM hypervisor with a dynamic prototype and by extension a dynamic print format.Reviewed-by Fuad Tabba <[email protected]>Tested-by Fuad Tabba <[email protected]>Signed-off-by Vincent Donnefort <[email protected]>
I'm assuming this goes in via the arm64 or KVM trees?
-- Steve
PATCH v2 4/4 KVM: arm64: Add hyp_printk event to nVHE/pKVM hyp
In reply to Vincent DonnefortCreate an event to allow developers to log pretty much anything into the hypervisor tracing buffer with trace_hyp_printk(), just like the kernel tracing has the function trace_printk().
trace_hyp_printk("foobar");
trace_hyp_printk("foo=%d", foo);
trace_hyp_printk("foo=%d bar=0x%016llx", foo, bar);To ensure writing into the tracing buffer is fast, store the string format into a kernel-accessible ELF section. The hypervisor only has to write into the event the string ID, which is the delta between the hyp_string_fmt and the start of the ELF section.
The string format is parsed by the kernel. It is therefore not possible to dereference hypervisor pointers and the format specifier %s is not supported. Also the %p specifiers will fail to find the hypervisor function.
To not waste tracing buffer data, use a dynamic size. Each argument is 8 bytes and the in-kernel printing function can simply know how many of them there are by looking at the event length.
arch/arm64/include/asm/kvm_hypevents.h +14 -0
@@ -57,4 +57,18 @@ HYP_EVENT(selftest, ),RE_PRINTK ("id=%llu" , __entry->id) ); + +/* +* trace_hyp_printk() has too many specificities to be declared with HYP_EVENT(). +* However, we can use a REMOTE_EVENT macro to automatically do the declaration +* for the kernel side. +*/ +REMOTE_EVENT_CUSTOM_PRINTK (hyp_printk, +0 ,/* id will be overwritten during hyp event init */ +RE_STRUCT ( + re_field(u16, fmt_id) + re_field(u64, args[]) + ), +__hyp_trace_printk (evt, seq) +);#endif
arch/arm64/include/asm/kvm_hyptrace.h +8 -0
@@ -23,4 +23,12 @@ extern struct remote_event __hyp_events_end[];extern struct hyp_event_id __hyp_event_ids_start[];extern struct hyp_event_id __hyp_event_ids_end[]; +#define HYP_STRING_FMT_MAX_SIZE 128 + +struct hyp_string_fmt { +const char fmt[HYP_STRING_FMT_MAX_SIZE ]; +}; + +extern struct hyp_string_fmt __hyp_string_fmts_start[]; +extern struct hyp_string_fmt __hyp_string_fmts_end[];#endif
arch/arm64/kernel/image-vars.h +1 -0
@@ -141,6 +141,7 @@ KVM_NVHE_ALIAS(__hyp_rodata_end);#ifdef CONFIG_NVHE_EL2_TRACING KVM_NVHE_ALIAS (__hyp_event_ids_start);KVM_NVHE_ALIAS (__hyp_event_ids_end); +KVM_NVHE_ALIAS (__hyp_string_fmts_start);#endif /* pKVM static key */
arch/arm64/kernel/vmlinux.lds.S +4 -0
@@ -324,6 +324,10 @@ SECTIONS __hyp_events_start = .; *(SORT(_hyp_events.*)) __hyp_events_end = .; + + __hyp_string_fmts_start = .; + *(_hyp_string_fmts) + __hyp_string_fmts_end = .; } #endif /*
arch/arm64/kvm/hyp/include/nvhe/define_events.h +0 -2
@@ -10,5 +10,3 @@#define HYP_EVENT_MULTI_READ #include <asm/kvm_hypevents.h> #undef HYP_EVENT_MULTI_READ - -#undef HYP_EVENT
arch/arm64/kvm/hyp/include/nvhe/trace.h +65 -0
77 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/events.c +6 -0
@@ -9,6 +9,12 @@#include <nvhe/define_events.h> +/* +* The hyp_printk event is not declared with HYP_EVENT in kvm_hypevents.h, +* so we manually add the boilerplate here. +*/ +HYP_EVENT (hyp_printk,0 ,0 ,0 ,0 ); +int __tracing_enable_event (unsigned short id,bool enable) {struct hyp_event_id *event_id = &__hyp_event_ids_start[id];
arch/arm64/kvm/hyp_trace.c +61 -0
79 lines not on this page · raw message
Signature · 1 line
2.55.0.795.g602f6c329a-goog