@@ -489,6 +489,28 @@ TRACE_EVENT(kvm_test_age_hva,
TP_printk("mmu notifier test age hva: %#016lx", __entry->hva)
);
+#ifdef CONFIG_KVM_PRIVATE_MEM
+TRACE_EVENT(kvm_gmem_direct_map_state_change,
+ TP_PROTO(pgoff_t start, pgoff_t end, bool state),
+ TP_ARGS(start, end, state),
+
+ TP_STRUCT__entry(
+ __field(pgoff_t, start)
+ __field(pgoff_t, end)
+ __field(bool, state)
+ ),
+
+ TP_fast_assign(
+ __entry->start = start;
+ __entry->end = end;
+ __entry->state = state;
+ ),
+
+ TP_printk("changed direct map state of guest_memfd range %lu to %lu to %s",
+ __entry->start, __entry->end, __entry->state ? "present" : "not present")
+);
+#endif
+
#endif /* _TRACE_KVM_MAIN_H */
/* This part must be outside protection */
@@ -7,6 +7,7 @@
#include <linux/set_memory.h>
#include "kvm_mm.h"
+#include "trace/events/kvm.h"
struct kvm_gmem {
struct kvm *kvm;
@@ -169,6 +170,8 @@ static __always_unused int kvm_gmem_folio_set_direct_map(struct folio *folio, pg
r = __kvm_gmem_folio_set_direct_map(folio, start, end, state);
folio_unlock(folio);
+ trace_kvm_gmem_direct_map_state_change(start, end, state);
+
unlock_xa:
xa_unlock(&gmem_priv->direct_map_state);
out:
@@ -216,6 +219,8 @@ static __always_unused int kvm_gmem_set_direct_map(struct inode *inode, pgoff_t
folio_batch_release(&fbatch);
}
+ trace_kvm_gmem_direct_map_state_change(start, end, state);
+
xa_unlock(&gmem_priv->direct_map_state);
out:
return r;
Add tracepoints to kvm_gmem_set_direct_map and kvm_gmem_folio_set_direct_map. The above operations can cause folios to be insert/removed into/from the direct map. We want to be able to make sure that only those gmem folios that we expect KVM to access are ever reinserted into the direct map, and that all folios that are temporarily reinserted are also removed again at a later point. Processing ftrace output is one way to verify this. Signed-off-by: Patrick Roy <roypat@amazon.co.uk> --- include/trace/events/kvm.h | 22 ++++++++++++++++++++++ virt/kvm/guest_memfd.c | 5 +++++ 2 files changed, 27 insertions(+)