From: Madhavan T. Venkataraman <madvenka@linux.microsoft.com>
When a page gets mapped, create permissions counters for it and
initialize them based on the specified permissions.
When a page gets unmapped, update the counters appropriately.
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Mickaël Salaün <mic@digikod.net>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Wanpeng Li <wanpengli@tencent.com>
Signed-off-by: Madhavan T. Venkataraman <madvenka@linux.microsoft.com>
---
Changes since v1:
* New patch
---
include/linux/heki.h | 11 ++++++++++-
mm/vmalloc.c | 7 +++++++
virt/heki/counters.c | 20 ++++++++++++++++++++
3 files changed, 37 insertions(+), 1 deletion(-)
@@ -68,7 +68,11 @@ struct heki_hypervisor {
* pointer into this heki structure.
*
* During guest kernel boot, permissions counters for each guest page are
- * initialized based on the page's current permissions.
+ * initialized based on the page's current permissions. Beyond this point,
+ * the counters are updated whenever:
+ *
+ * - a page is mapped into the kernel address space
+ * - a page is unmapped from the kernel address space
*/
struct heki {
struct heki_hypervisor *hypervisor;
@@ -77,6 +81,7 @@ struct heki {
enum heki_cmd {
HEKI_MAP,
+ HEKI_UNMAP,
};
/*
@@ -109,6 +114,7 @@ void heki_counters_init(void);
void heki_walk(unsigned long va, unsigned long va_end, heki_func_t func,
struct heki_args *args);
void heki_map(unsigned long va, unsigned long end);
+void heki_unmap(unsigned long va, unsigned long end);
/* Arch-specific functions. */
void heki_arch_early_init(void);
@@ -125,6 +131,9 @@ static inline void heki_late_init(void)
static inline void heki_map(unsigned long va, unsigned long end)
{
}
+static inline void heki_unmap(unsigned long va, unsigned long end)
+{
+}
#endif /* CONFIG_HEKI */
@@ -40,6 +40,7 @@
#include <linux/pgtable.h>
#include <linux/hugetlb.h>
#include <linux/sched/mm.h>
+#include <linux/heki.h>
#include <asm/tlbflush.h>
#include <asm/shmparam.h>
@@ -301,6 +302,8 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end,
if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
arch_sync_kernel_mappings(start, end);
+ heki_map(start, end);
+
return err;
}
@@ -419,6 +422,8 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end)
pgtbl_mod_mask mask = 0;
BUG_ON(addr >= end);
+ heki_unmap(start, end);
+
pgd = pgd_offset_k(addr);
do {
next = pgd_addr_end(addr, end);
@@ -564,6 +569,8 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
arch_sync_kernel_mappings(start, end);
+ heki_map(start, end);
+
return 0;
}
@@ -88,6 +88,13 @@ void heki_callback(struct heki_args *args)
heki_update_counters(counters, 0, permissions, 0);
break;
+ case HEKI_UNMAP:
+ if (WARN_ON_ONCE(!counters))
+ break;
+ heki_update_counters(counters, permissions, 0,
+ permissions);
+ break;
+
default:
WARN_ON_ONCE(1);
break;
@@ -124,6 +131,19 @@ void heki_map(unsigned long va, unsigned long end)
heki_func(va, end, &args);
}
+/*
+ * Find the mappings in the given range and revert the permission counters for
+ * them.
+ */
+void heki_unmap(unsigned long va, unsigned long end)
+{
+ struct heki_args args = {
+ .cmd = HEKI_UNMAP,
+ };
+
+ heki_func(va, end, &args);
+}
+
/*
* Permissions counters are associated with each guest page using the
* Memory Table feature. Initialize the permissions counters here.