diff mbox series

[RFC,v8,3/7] KVM: Guest free page hinting functional skeleton

Message ID 20190204201854.2328-4-nitesh@redhat.com (mailing list archive)
State New, archived
Headers show
Series KVM: Guest Free Page Hinting | expand

Commit Message

Nitesh Narayan Lal Feb. 4, 2019, 8:18 p.m. UTC
This patch adds the functional skeleton for the guest implementation.
It also enables the guest to maintain the list of pages which are
freed by the guest. Once the list is full guest_free_page invokes
scan_array() which wakes up the kernel thread responsible for further
processing.

Signed-off-by: Nitesh Narayan Lal <nitesh@redhat.com>
---
 include/linux/page_hinting.h |  3 ++
 virt/kvm/page_hinting.c      | 60 +++++++++++++++++++++++++++++++++++-
 2 files changed, 62 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/include/linux/page_hinting.h b/include/linux/page_hinting.h
index 9bdcf63e1306..2d7ff59f3f6a 100644
--- a/include/linux/page_hinting.h
+++ b/include/linux/page_hinting.h
@@ -1,3 +1,5 @@ 
+#include <linux/smpboot.h>
+
 /*
  * Size of the array which is used to store the freed pages is defined by
  * MAX_FGPT_ENTRIES. If possible, we have to find a better way using which
@@ -16,6 +18,7 @@  struct hypervisor_pages {
 
 extern int guest_page_hinting_flag;
 extern struct static_key_false guest_page_hinting_key;
+extern struct smp_hotplug_thread hinting_threads;
 
 int guest_page_hinting_sysctl(struct ctl_table *table, int write,
 			      void __user *buffer, size_t *lenp, loff_t *ppos);
diff --git a/virt/kvm/page_hinting.c b/virt/kvm/page_hinting.c
index 4a34ea8db0c8..636990e7fbb3 100644
--- a/virt/kvm/page_hinting.c
+++ b/virt/kvm/page_hinting.c
@@ -1,7 +1,7 @@ 
 #include <linux/gfp.h>
 #include <linux/mm.h>
-#include <linux/kernel.h>
 #include <linux/kvm_host.h>
+#include <linux/kernel.h>
 
 /*
  * struct kvm_free_pages - Tracks the pages which are freed by the guest.
@@ -37,6 +37,7 @@  EXPORT_SYMBOL(guest_page_hinting_key);
 static DEFINE_MUTEX(hinting_mutex);
 int guest_page_hinting_flag;
 EXPORT_SYMBOL(guest_page_hinting_flag);
+static DEFINE_PER_CPU(struct task_struct *, hinting_task);
 
 int guest_page_hinting_sysctl(struct ctl_table *table, int write,
 			      void __user *buffer, size_t *lenp,
@@ -54,6 +55,63 @@  int guest_page_hinting_sysctl(struct ctl_table *table, int write,
 	return ret;
 }
 
+static void hinting_fn(unsigned int cpu)
+{
+	struct page_hinting *page_hinting_obj = this_cpu_ptr(&hinting_obj);
+
+	page_hinting_obj->kvm_pt_idx = 0;
+	put_cpu_var(hinting_obj);
+}
+
+void scan_array(void)
+{
+	struct page_hinting *page_hinting_obj = this_cpu_ptr(&hinting_obj);
+
+	if (page_hinting_obj->kvm_pt_idx == MAX_FGPT_ENTRIES)
+		wake_up_process(__this_cpu_read(hinting_task));
+}
+
+static int hinting_should_run(unsigned int cpu)
+{
+	struct page_hinting *page_hinting_obj = this_cpu_ptr(&hinting_obj);
+	int free_page_idx = page_hinting_obj->kvm_pt_idx;
+
+	if (free_page_idx == MAX_FGPT_ENTRIES)
+		return 1;
+	else
+		return 0;
+}
+
+struct smp_hotplug_thread hinting_threads = {
+	.store			= &hinting_task,
+	.thread_should_run	= hinting_should_run,
+	.thread_fn		= hinting_fn,
+	.thread_comm		= "hinting/%u",
+	.selfparking		= false,
+};
+EXPORT_SYMBOL(hinting_threads);
+
 void guest_free_page(struct page *page, int order)
 {
+	unsigned long flags;
+	struct page_hinting *page_hinting_obj = this_cpu_ptr(&hinting_obj);
+	/*
+	 * use of global variables may trigger a race condition between irq and
+	 * process context causing unwanted overwrites. This will be replaced
+	 * with a better solution to prevent such race conditions.
+	 */
+
+	local_irq_save(flags);
+	if (page_hinting_obj->kvm_pt_idx != MAX_FGPT_ENTRIES) {
+		page_hinting_obj->kvm_pt[page_hinting_obj->kvm_pt_idx].pfn =
+							page_to_pfn(page);
+		page_hinting_obj->kvm_pt[page_hinting_obj->kvm_pt_idx].zonenum =
+							page_zonenum(page);
+		page_hinting_obj->kvm_pt[page_hinting_obj->kvm_pt_idx].order =
+							order;
+		page_hinting_obj->kvm_pt_idx += 1;
+		if (page_hinting_obj->kvm_pt_idx == MAX_FGPT_ENTRIES)
+			scan_array();
+	}
+	local_irq_restore(flags);
 }