RESEND [PATCH 10/10] KVM: Log ROE violations in system log

From: Ahmed Abd El Mawgood
Date: Fri Dec 07 2018 - 11:24:00 EST


I am absolutely sorry, I had some modifications that I forgot to commit
before I send. so please use this one patch 10/10 instead of the last
one.

Signed-off-by: Ahmed Abd El Mawgood <ahmedsoliman@xxxxxxxxxxx>
---
virt/kvm/kvm_main.c | 3 ++-
virt/kvm/roe.c | 26 ++++++++++++++++++++++++++
virt/kvm/roe_generic.h | 3 ++-
3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index c3a21d3bc8..761cb7561a 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1870,13 +1870,14 @@ static u64 roe_gfn_to_hva(struct kvm_memory_slot *slot, gfn_t gfn, int offset,
addr = __gfn_to_hva_many(slot, gfn, NULL, false);
return addr;
}
+
static int __kvm_write_guest_page(struct kvm_memory_slot *memslot, gfn_t gfn,
const void *data, int offset, int len)
{
int r;
unsigned long addr;
-
addr = roe_gfn_to_hva(memslot, gfn, offset, len);
+ kvm_roe_check_and_log(memslot, gfn, data, offset, len);
if (kvm_is_error_hva(addr))
return -EFAULT;
r = __copy_to_user((void __user *)addr + offset, data, len);
diff --git a/virt/kvm/roe.c b/virt/kvm/roe.c
index 6555838f0c..01362f0fca 100644
--- a/virt/kvm/roe.c
+++ b/virt/kvm/roe.c
@@ -76,6 +76,32 @@ void kvm_roe_free(struct kvm_memory_slot *slot)
kvfree(slot->prot_list);
}

+static void kvm_warning_roe_violation(u64 addr, const void *data, int len)
+{
+ int i;
+ const char *d = data;
+ char *buf = kvmalloc(len * 3 + 1, GFP_KERNEL);
+
+ for (i = 0; i < len; i++)
+ sprintf(buf+3*i, " %02x", d[i]);
+ pr_warn("ROE violation:\n");
+ pr_warn("\tAttempt to write %d bytes at address 0x%08llx\n", len, addr);
+ pr_warn("\tData: %s\n", buf);
+ kvfree(buf);
+}
+
+void kvm_roe_check_and_log(struct kvm_memory_slot *memslot, gfn_t gfn,
+ const void *data, int offset, int len)
+{
+ if (!memslot)
+ return;
+ if (!gfn_is_full_roe(memslot, gfn))
+ return;
+ if (!kvm_roe_check_range(memslot, gfn, offset, len))
+ return;
+ kvm_warning_roe_violation((gfn << PAGE_SHIFT) + offset, data, len);
+}
+
static void kvm_roe_protect_slot(struct kvm *kvm, struct kvm_memory_slot *slot,
gfn_t gfn, u64 npages, bool partial)
{
diff --git a/virt/kvm/roe_generic.h b/virt/kvm/roe_generic.h
index f1ce4a8aec..6c5f0cf381 100644
--- a/virt/kvm/roe_generic.h
+++ b/virt/kvm/roe_generic.h
@@ -14,5 +14,6 @@ void kvm_roe_free(struct kvm_memory_slot *slot);
int kvm_roe_init(struct kvm_memory_slot *slot);
bool kvm_roe_check_range(struct kvm_memory_slot *slot, gfn_t gfn, int offset,
int len);
-
+void kvm_roe_check_and_log(struct kvm_memory_slot *memslot, gfn_t gfn,
+ const void *data, int offset, int len);
#endif
--
2.19.2