[RFC PATCH v3 4/6] kvm: gmem: add trace point for direct map state changes

Patrick Roy posted 6 patches 3 weeks, 4 days ago
[RFC PATCH v3 4/6] kvm: gmem: add trace point for direct map state changes
Posted by Patrick Roy 3 weeks, 4 days ago
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(+)

diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h
index 74e40d5d4af42..f3d852c18fa08 100644
--- a/include/trace/events/kvm.h
+++ b/include/trace/events/kvm.h
@@ -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 */
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 54387828dcc6a..a0b3b9cacd361 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -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;
-- 
2.47.0