Skip to contentSkip to conversation

KVM: Add asynchronous page fault for PV guest.

PATCH Superseded Gleb Natapov 7 participants 55 messages Nov 1, 2009 – Nov 8, 2009

Subsystems (12)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover KVM: Add asynchronous page fault for PV guest.

KVM virtualize guest memory by means of shadow pages or HW assistance like NPT/EPT. Not all memory used by a guest is mapped into the guest address space or even present in a host memory at any given time. When vcpu tries to access memory page that is not mapped into guest address space KVM is notified about it. KVM maps the page into guest address space and resumes vcpu execution. If the page is swapped out from host memory vcpu execution is suspended till page is not swapped into the memory again. This is inefficient since vcpu can do other work (run other task or serve interrupts) while page gets swapped in.

To overcome this inefficient this patch series implements "asynchronous page fault" for paravirtualized KVM guests. If a page that vcpu is trying to access is swapped out KVM sends async PF to the vcpu and continues vcpu execution. Requested page is swapped in by another thread in parallel. When vcpu gets async PF it puts the task that faulted to sleep until "wake up" interrupt is delivered. When page is brought to host memory KVM sends "wake up" interrupt and the guest task resumes execution.

Gleb Natapov (11):
Add shared memory hypercall to PV Linux guest.
Add "handle page fault" PV helper.
Handle asynchronous page fault in a PV guest.
Export __get_user_pages_fast.
Add get_user_pages() variant that fails if major fault is required. Inject asynchronous page fault into a guest if page is swapped out. Retry fault before vmentry
Add "wait for page" hypercall.
Maintain preemptability count even for !CONFIG_PREEMPT kernels Handle async PF in non preemptable context.
Send async PF when guest is not in userspace too.

 arch/x86/include/asm/kvm_host.h       |   29 ++++-
 arch/x86/include/asm/kvm_para.h       |   14 ++
 arch/x86/include/asm/paravirt.h       |    7 +
 arch/x86/include/asm/paravirt_types.h |    4 +
 arch/x86/kernel/kvm.c                 |  210 ++++++++++++++++++++++++
 arch/x86/kernel/paravirt.c            |    8 +
 arch/x86/kernel/paravirt_patch_32.c   |    8 +
 arch/x86/kernel/paravirt_patch_64.c   |    7 +
 arch/x86/kernel/setup.c               |    1 +
 arch/x86/kernel/smpboot.c             |    3 +
 arch/x86/kvm/mmu.c                    |  284 ++++++++++++++++++++++++++++++++-
 arch/x86/kvm/mmutrace.h               |   79 +++++++++
 arch/x86/kvm/paging_tmpl.h            |   44 +++++-
 arch/x86/kvm/x86.c                    |   97 +++++++++++-
 arch/x86/mm/fault.c                   |    3 +
 arch/x86/mm/gup.c                     |    2 +
 fs/ncpfs/mmap.c                       |    2 +
 include/linux/hardirq.h               |    6 +-
 include/linux/kvm.h                   |    1 +
 include/linux/kvm_para.h              |    5 +
 include/linux/mm.h                    |    5 +
 include/linux/preempt.h               |   22 ++-
 kernel/sched.c                        |    6 -
 lib/kernel_lock.c                     |    1 +
 mm/filemap.c                          |    3 +
 mm/memory.c                           |   31 ++++-
 mm/shmem.c                            |    8 +-
 27 files changed, 855 insertions(+), 35 deletions(-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

PATCH 1/11 Add shared memory hypercall to PV Linux guest.

Add hypercall that allows guest and host to setup per cpu shared memory.

---
 arch/x86/include/asm/kvm_host.h |    3 +
 arch/x86/include/asm/kvm_para.h |   11 +++++
 arch/x86/kernel/kvm.c           |   82 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/setup.c         |    1 +
 arch/x86/kernel/smpboot.c       |    3 +
 arch/x86/kvm/x86.c              |   70 +++++++++++++++++++++++++++++++++
 include/linux/kvm.h             |    1 +
 include/linux/kvm_para.h        |    4 ++
 8 files changed, 175 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
index 26a74b7..2d1f526 100644
@@ -374,6 +374,9 @@ struct kvm_vcpu_arch {
 	/* used for guest single stepping over the given code position */
 	u16 singlestep_cs;
 	unsigned long singlestep_rip;
+
+	struct kvm_vcpu_pv_shm *pv_shm;
+	struct page *pv_shm_page;
 };
 
 struct kvm_mem_alias {
arch/x86/include/asm/kvm_para.h +11 -0
index c584076..90708b7 100644
@@ -15,6 +15,7 @@
 #define KVM_FEATURE_CLOCKSOURCE		0
 #define KVM_FEATURE_NOP_IO_DELAY	1
 #define KVM_FEATURE_MMU_OP		2
+#define KVM_FEATURE_ASYNC_PF		3
 
 #define MSR_KVM_WALL_CLOCK  0x11
 #define MSR_KVM_SYSTEM_TIME 0x12
@@ -47,6 +48,16 @@ struct kvm_mmu_op_release_pt {
 	__u64 pt_phys;
 };
 
+#define KVM_PV_SHM_VERSION 1
+
+#define KVM_PV_SHM_FEATURES_ASYNC_PF		(1 << 0)
+
+struct kvm_vcpu_pv_shm {
+	__u64 features;
+	__u64 reason;
+	__u64 param;
+};
+
 #ifdef __KERNEL__
 #include <asm/processor.h>
 
arch/x86/kernel/kvm.c +82 -0

111 lines not on this page · raw message

arch/x86/kernel/setup.c +1 -0
index e09f0e2..1c2f8dd 100644
@@ -1007,6 +1007,7 @@ void __init setup_arch(char **cmdline_p)
 	probe_nr_irqs_gsi();
 
 	kvm_guest_init();
+	kvm_guest_cpu_init();
 
 	e820_reserve_resources();
 	e820_mark_nosave_regions(max_low_pfn);
arch/x86/kernel/smpboot.c +3 -0
index 565ebc6..5599098 100644
@@ -65,6 +65,7 @@
 #include <asm/setup.h>
 #include <asm/uv/uv.h>
 #include <linux/mc146818rtc.h>
+#include <linux/kvm_para.h>
 
 #include <asm/smpboot_hooks.h>
 
@@ -321,6 +322,8 @@ notrace static void __cpuinit start_secondary(void *unused)
 	ipi_call_unlock();
 	per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
 
+	kvm_guest_cpu_init();
+
 	/* enable local interrupts */
 	local_irq_enable();
 
arch/x86/kvm/x86.c +70 -0

100 lines not on this page · raw message

include/linux/kvm.h +1 -0
index 6ed1a12..2fec4a2 100644
@@ -440,6 +440,7 @@ struct kvm_ioeventfd {
 #define KVM_CAP_XEN_HVM 38
 #endif
 #define KVM_CAP_ADJUST_CLOCK 39
+#define KVM_CAP_ASYNC_PF 40
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
include/linux/kvm_para.h +4 -0
index d731092..1c37495 100644
@@ -14,9 +14,11 @@
 #define KVM_EFAULT		EFAULT
 #define KVM_E2BIG		E2BIG
 #define KVM_EPERM		EPERM
+#define KVM_EINVAL		EINVAL
 
 #define KVM_HC_VAPIC_POLL_IRQ		1
 #define KVM_HC_MMU_OP			2
+#define KVM_HC_SETUP_SHM		3
 
 /*
  * hypercalls use architecture specific
@@ -26,8 +28,10 @@
 #ifdef __KERNEL__
 #ifdef CONFIG_KVM_GUEST
 void __init kvm_guest_init(void);
+void __cpuinit kvm_guest_cpu_init(void);
 #else
 #define kvm_guest_init() do { } while (0)
+#define kvm_guest_cpu_init() do { } while (0)
 #endif
 
 static inline int kvm_para_has_feature(unsigned int feature)
Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#9 lore raw

PATCH 2/11 Add "handle page fault" PV helper.

In reply to Gleb Natapov

Allow paravirtualized guest to do special handling for some page faults.

---
 arch/x86/include/asm/paravirt.h       |    7 +++++++
 arch/x86/include/asm/paravirt_types.h |    4 ++++
 arch/x86/kernel/paravirt.c            |    8 ++++++++
 arch/x86/kernel/paravirt_patch_32.c   |    8 ++++++++
 arch/x86/kernel/paravirt_patch_64.c   |    7 +++++++
 arch/x86/mm/fault.c                   |    3 +++
 6 files changed, 37 insertions(+), 0 deletions(-)
arch/x86/include/asm/paravirt.h +7 -0
index efb3899..5203da1 100644
@@ -6,6 +6,7 @@
 #ifdef CONFIG_PARAVIRT
 #include <asm/pgtable_types.h>
 #include <asm/asm.h>
+#include <asm/ptrace.h>
 
 #include <asm/paravirt_types.h>
 
@@ -710,6 +711,12 @@ static inline void arch_end_context_switch(struct task_struct *next)
 	PVOP_VCALL1(pv_cpu_ops.end_context_switch, next);
 }
 
+static inline int arch_handle_page_fault(struct pt_regs *regs,
+					 unsigned long error_code)
+{
+	return PVOP_CALL2(int, pv_cpu_ops.handle_pf, regs, error_code);
+}
+
 #define  __HAVE_ARCH_ENTER_LAZY_MMU_MODE
 static inline void arch_enter_lazy_mmu_mode(void)
 {
arch/x86/include/asm/paravirt_types.h +4 -0
index 9357473..bcc39b3 100644
@@ -186,6 +186,7 @@ struct pv_cpu_ops {
 
 	void (*start_context_switch)(struct task_struct *prev);
 	void (*end_context_switch)(struct task_struct *next);
+	int (*handle_pf)(struct pt_regs *regs, unsigned long error_code);
 };
 
 struct pv_irq_ops {
@@ -385,6 +386,7 @@ extern struct pv_lock_ops pv_lock_ops;
 unsigned paravirt_patch_nop(void);
 unsigned paravirt_patch_ident_32(void *insnbuf, unsigned len);
 unsigned paravirt_patch_ident_64(void *insnbuf, unsigned len);
+unsigned paravirt_patch_ret_0(void *insnbuf, unsigned len);
 unsigned paravirt_patch_ignore(unsigned len);
 unsigned paravirt_patch_call(void *insnbuf,
 			     const void *target, u16 tgt_clobbers,
@@ -676,8 +678,10 @@ void paravirt_leave_lazy_mmu(void);
 void _paravirt_nop(void);
 u32 _paravirt_ident_32(u32);
 u64 _paravirt_ident_64(u64);
+unsigned long _paravirt_ret_0(void);
 
 #define paravirt_nop	((void *)_paravirt_nop)
+#define paravirt_ret_0  ((void *)_paravirt_ret_0)
 
 /* These all sit in the .parainstructions section to tell us what to patch. */
 struct paravirt_patch_site {
arch/x86/kernel/paravirt.c +8 -0
index 1b1739d..7d8f37b 100644
@@ -54,6 +54,11 @@ u64 _paravirt_ident_64(u64 x)
 	return x;
 }
 
+unsigned long _paravirt_ret_0(void)
+{
+	return 0;
+}
+
 void __init default_banner(void)
 {
 	printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
@@ -154,6 +159,8 @@ unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
 		ret = paravirt_patch_ident_32(insnbuf, len);
 	else if (opfunc == _paravirt_ident_64)
 		ret = paravirt_patch_ident_64(insnbuf, len);
+	else if (opfunc == _paravirt_ret_0)
+		ret = paravirt_patch_ret_0(insnbuf, len);
 
 	else if (type == PARAVIRT_PATCH(pv_cpu_ops.iret) ||
 		 type == PARAVIRT_PATCH(pv_cpu_ops.irq_enable_sysexit) ||
@@ -380,6 +387,7 @@ struct pv_cpu_ops pv_cpu_ops = {
 
 	.start_context_switch = paravirt_nop,
 	.end_context_switch = paravirt_nop,
+	.handle_pf = paravirt_ret_0,
 };
 
 struct pv_apic_ops pv_apic_ops = {
arch/x86/kernel/paravirt_patch_32.c +8 -0
index d9f32e6..de006b1 100644
@@ -12,6 +12,8 @@ DEF_NATIVE(pv_mmu_ops, read_cr3, "mov %cr3, %eax");
 DEF_NATIVE(pv_cpu_ops, clts, "clts");
 DEF_NATIVE(pv_cpu_ops, read_tsc, "rdtsc");
 
+DEF_NATIVE(, mov0, "xor %eax, %eax");
+
 unsigned paravirt_patch_ident_32(void *insnbuf, unsigned len)
 {
 	/* arg in %eax, return in %eax */
@@ -24,6 +26,12 @@ unsigned paravirt_patch_ident_64(void *insnbuf, unsigned len)
 	return 0;
 }
 
+unsigned paravirt_patch_ret_0(void *insnbuf, unsigned len)
+{
+	return paravirt_patch_insns(insnbuf, len,
+				    start__mov0, end__mov0);
+}
+
 unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
 		      unsigned long addr, unsigned len)
 {
arch/x86/kernel/paravirt_patch_64.c +7 -0
index 3f08f34..d685e7d 100644
@@ -21,6 +21,7 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs");
 
 DEF_NATIVE(, mov32, "mov %edi, %eax");
 DEF_NATIVE(, mov64, "mov %rdi, %rax");
+DEF_NATIVE(, mov0, "xor %rax, %rax");
 
 unsigned paravirt_patch_ident_32(void *insnbuf, unsigned len)
 {
@@ -34,6 +35,12 @@ unsigned paravirt_patch_ident_64(void *insnbuf, unsigned len)
 				    start__mov64, end__mov64);
 }
 
+unsigned paravirt_patch_ret_0(void *insnbuf, unsigned len)
+{
+	return paravirt_patch_insns(insnbuf, len,
+				    start__mov0, end__mov0);
+}
+
 unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
 		      unsigned long addr, unsigned len)
 {
arch/x86/mm/fault.c +3 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	int write;
 	int fault;
 
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+
 	tsk = current;
 	mm = tsk->mm;
 
Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#31 lore raw

PATCH 3/11 Handle asynchronous page fault in a PV guest.

In reply to Gleb Natapov

Asynchronous page fault notifies vcpu that page it is trying to access is swapped out by a host. In response guest puts a task that caused the fault to sleep until page is swapped in again. When missing page is brought back into the memory guest is notified and task resumes execution.

---
 arch/x86/include/asm/kvm_para.h |    3 +
 arch/x86/kernel/kvm.c           |  120 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 120 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_para.h +3 -0
index 90708b7..61e2aa3 100644
@@ -52,6 +52,9 @@ struct kvm_mmu_op_release_pt {
 
 #define KVM_PV_SHM_FEATURES_ASYNC_PF		(1 << 0)
 
+#define KVM_PV_REASON_PAGE_NP 1
+#define KVM_PV_REASON_PAGE_READY 2
+
 struct kvm_vcpu_pv_shm {
 	__u64 features;
 	__u64 reason;
arch/x86/kernel/kvm.c +117 -3

155 lines not on this page · raw message

Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#38 lore raw

PATCH 4/11 Export __get_user_pages_fast.

In reply to Gleb Natapov
---
 arch/x86/mm/gup.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/mm/gup.c +2 -0
index 71da1bc..cea0dfe 100644
@@ -8,6 +8,7 @@
 #include <linux/mm.h>
 #include <linux/vmstat.h>
 #include <linux/highmem.h>
+#include <linux/module.h>
 
 #include <asm/pgtable.h>
 
@@ -274,6 +275,7 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
 
 	return nr;
 }
+EXPORT_SYMBOL_GPL(__get_user_pages_fast);
 
 /**
  * get_user_pages_fast() - pin user pages in memory
Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#40 lore raw

PATCH 5/11 Add get_user_pages() variant that fails if major fault is required.

In reply to Gleb Natapov

This patch add get_user_pages() variant that only succeeds if getting a reference to a page doesn't require major fault.

---
 fs/ncpfs/mmap.c    |    2 ++
 include/linux/mm.h |    5 +++++
 mm/filemap.c       |    3 +++
 mm/memory.c        |   31 ++++++++++++++++++++++++++++---
 mm/shmem.c         |    8 +++++++-
 5 files changed, 45 insertions(+), 4 deletions(-)
fs/ncpfs/mmap.c +2 -0
index 15458de..338527e 100644
@@ -39,6 +39,8 @@ static int ncp_file_mmap_fault(struct vm_area_struct *area,
 	int bufsize;
 	int pos; /* XXX: loff_t ? */
 
+	if (vmf->flags & FAULT_FLAG_MINOR)
+		return VM_FAULT_MAJOR | VM_FAULT_ERROR;
 	/*
 	 * ncpfs has nothing against high pages as long
 	 * as recvmsg and memset works on it
include/linux/mm.h +5 -0
index 24c3956..2304181 100644
@@ -136,6 +136,7 @@ extern pgprot_t protection_map[16];
 #define FAULT_FLAG_WRITE	0x01	/* Fault was a write access */
 #define FAULT_FLAG_NONLINEAR	0x02	/* Fault was via a nonlinear mapping */
 #define FAULT_FLAG_MKWRITE	0x04	/* Fault was mkwrite of existing pte */
+#define FAULT_FLAG_MINOR	0x08	/* Do only minor fault */
 
 /*
  * This interface is used by x86 PAT code to identify a pfn mapping that is
@@ -821,6 +822,9 @@ extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *
 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
 			unsigned long start, int nr_pages, int write, int force,
 			struct page **pages, struct vm_area_struct **vmas);
+int get_user_pages_noio(struct task_struct *tsk, struct mm_struct *mm,
+			unsigned long start, int nr_pages, int write, int force,
+			struct page **pages, struct vm_area_struct **vmas);
 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
 			struct page **pages);
 struct page *get_dump_page(unsigned long addr);
@@ -1239,6 +1243,7 @@ struct page *follow_page(struct vm_area_struct *, unsigned long address,
 #define FOLL_GET	0x04	/* do get_page on page */
 #define FOLL_DUMP	0x08	/* give error on hole if it would be zero */
 #define FOLL_FORCE	0x10	/* get_user_pages read/write w/o permission */
+#define FOLL_MINOR	0x20	/* do only minor page faults */
 
 typedef int (*pte_fn_t)(pte_t *pte, pgtable_t token, unsigned long addr,
 			void *data);
mm/filemap.c +3 -0
index ef169f3..6ef29e0 100644
@@ -1530,6 +1530,9 @@ int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 			goto no_cached_page;
 		}
 	} else {
+		if (vmf->flags & FAULT_FLAG_MINOR)
+			return VM_FAULT_MAJOR | VM_FAULT_ERROR;
+
 		/* No page in the page cache at all */
 		do_sync_mmap_readahead(vma, ra, file, offset);
 		count_vm_event(PGMAJFAULT);
mm/memory.c +28 -3
index 7e91b5f..b621526 100644
@@ -1318,10 +1318,13 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
 			cond_resched();
 			while (!(page = follow_page(vma, start, foll_flags))) {
 				int ret;
+				unsigned int fault_fl =
+					((foll_flags & FOLL_WRITE) ?
+					FAULT_FLAG_WRITE : 0) |
+					((foll_flags & FOLL_MINOR) ?
+					FAULT_FLAG_MINOR : 0);
 
-				ret = handle_mm_fault(mm, vma, start,
-					(foll_flags & FOLL_WRITE) ?
-					FAULT_FLAG_WRITE : 0);
+				ret = handle_mm_fault(mm, vma, start, fault_fl);
 
 				if (ret & VM_FAULT_ERROR) {
 					if (ret & VM_FAULT_OOM)
@@ -1329,6 +1332,8 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
 					if (ret &
 					    (VM_FAULT_HWPOISON|VM_FAULT_SIGBUS))
 						return i ? i : -EFAULT;
+					else if (ret & VM_FAULT_MAJOR)
+						return i ? i : -EFAULT;
 					BUG();
 				}
 				if (ret & VM_FAULT_MAJOR)
@@ -1439,6 +1444,23 @@ int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
 }
 EXPORT_SYMBOL(get_user_pages);
 
+int get_user_pages_noio(struct task_struct *tsk, struct mm_struct *mm,
+		unsigned long start, int nr_pages, int write, int force,
+		struct page **pages, struct vm_area_struct **vmas)
+{
+	int flags = FOLL_TOUCH | FOLL_MINOR;
+
+	if (pages)
+		flags |= FOLL_GET;
+	if (write)
+		flags |= FOLL_WRITE;
+	if (force)
+		flags |= FOLL_FORCE;
+
+	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
+}
+EXPORT_SYMBOL(get_user_pages_noio);
+
 /**
  * get_dump_page() - pin user page in memory while writing it to core dump
  * @addr: user address
@@ -2518,6 +2540,9 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
 	page = lookup_swap_cache(entry);
 	if (!page) {
+		if (flags & FAULT_FLAG_MINOR)
+			return VM_FAULT_MAJOR | VM_FAULT_ERROR;
+
 		grab_swap_token(mm); /* Contend for token _before_ read-in */
 		page = swapin_readahead(entry,
 					GFP_HIGHUSER_MOVABLE, vma, address);
mm/shmem.c +7 -1
index 356dd99..6a9d3c0 100644
@@ -1218,6 +1218,7 @@ static int shmem_getpage(struct inode *inode, unsigned long idx,
 	swp_entry_t swap;
 	gfp_t gfp;
 	int error;
+	int flags = type ? *type : 0;
 
 	if (idx >= SHMEM_MAX_INDEX)
 		return -EFBIG;
@@ -1266,6 +1267,11 @@ repeat:
 		swappage = lookup_swap_cache(swap);
 		if (!swappage) {
 			shmem_swp_unmap(entry);
+			if (flags & FAULT_FLAG_MINOR) {
+				spin_unlock(&info->lock);
+				*type = VM_FAULT_MAJOR | VM_FAULT_ERROR;
+				goto failed;
+			}
 			/* here we actually do the io */
 			if (type && !(*type & VM_FAULT_MAJOR)) {
 				__count_vm_event(PGMAJFAULT);
@@ -1474,7 +1480,7 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 {
 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
 	int error;
-	int ret;
+	int ret = (int)vmf->flags;
 
 	if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
 		return VM_FAULT_SIGBUS;
Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#42 lore raw

PATCH 6/11 Inject asynchronous page fault into a guest if page is swapped out.

In reply to Gleb Natapov

If guest access swapped out memory do not swap it in from vcpu thread context. Setup slow work to do swapping and send async page fault to a guest.

Allow async page fault injection only when guest is in user mode since otherwise guest may be in non-sleepable context and will not be able to reschedule.

---
 arch/x86/include/asm/kvm_host.h |   20 +++
 arch/x86/kvm/mmu.c              |  243 ++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/mmutrace.h         |   60 ++++++++++
 arch/x86/kvm/paging_tmpl.h      |   16 +++-
 arch/x86/kvm/x86.c              |   22 +++-
 5 files changed, 352 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_host.h +20 -0
index 2d1f526..e3cdbfe 100644
@@ -15,6 +15,7 @@
 #include <linux/mm.h>
 #include <linux/mmu_notifier.h>
 #include <linux/tracepoint.h>
+#include <linux/slow-work.h>
 
 #include <linux/kvm.h>
 #include <linux/kvm_para.h>
@@ -235,6 +236,18 @@ struct kvm_pv_mmu_op_buffer {
 	char buf[512] __aligned(sizeof(long));
 };
 
+struct kvm_mmu_async_pf {
+	struct slow_work work;
+	struct list_head link;
+	struct kvm_vcpu *vcpu;
+	struct mm_struct *mm;
+	gva_t gva;
+	unsigned long addr;
+	u64 token;
+	struct page *page;
+	atomic_t used;
+};
+
 struct kvm_pio_request {
 	unsigned long count;
 	int cur_count;
@@ -318,6 +331,11 @@ struct kvm_vcpu_arch {
 		unsigned long mmu_seq;
 	} update_pte;
 
+	struct list_head mmu_async_pf_done;
+	spinlock_t mmu_async_pf_lock;
+	struct kvm_mmu_async_pf *mmu_async_pf_work;
+	u32 async_pf_id;
+
 	struct i387_fxsave_struct host_fx_image;
 	struct i387_fxsave_struct guest_fx_image;
 
@@ -654,6 +672,8 @@ void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
 int kvm_mmu_load(struct kvm_vcpu *vcpu);
 void kvm_mmu_unload(struct kvm_vcpu *vcpu);
 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
+void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
+void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
 
 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
 
arch/x86/kvm/mmu.c +240 -3

285 lines not on this page · raw message

arch/x86/kvm/mmutrace.h +60 -0

66 lines not on this page · raw message

arch/x86/kvm/paging_tmpl.h +15 -1
index 72558f8..9fe2ecd 100644
@@ -419,7 +419,21 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
 
 	mmu_seq = vcpu->kvm->mmu_notifier_seq;
 	smp_rmb();
-	pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
+
+	if (can_do_async_pf(vcpu)) {
+		r = gfn_to_pfn_async(vcpu->kvm, walker.gfn, &pfn);
+		trace_kvm_mmu_try_async_get_page(r, pfn);
+	} else {
+do_sync:
+		r = 1;
+		pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
+	}
+
+	if (!r) {
+		if (!setup_async_pf(vcpu, addr, walker.gfn))
+			goto do_sync;
+		return 0;
+	}
 
 	/* mmio */
 	if (is_error_pfn(pfn)) {
arch/x86/kvm/x86.c +17 -5
index c177933..e6bd3ad 100644
@@ -3330,6 +3330,10 @@ int kvm_arch_init(void *opaque)
 		goto out;
 	}
 
+	r = slow_work_register_user();
+	if (r)
+		goto out;
+
 	r = kvm_mmu_module_init();
 	if (r)
 		goto out;
@@ -3352,6 +3356,7 @@ out:
 
 void kvm_arch_exit(void)
 {
+	slow_work_unregister_user();
 	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
 		cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
 					    CPUFREQ_TRANSITION_NOTIFIER);
@@ -3837,6 +3842,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		}
 	}
 
+	kvm_check_async_pf_completion(vcpu);
+
 	preempt_disable();
 
 	kvm_x86_ops->prepare_guest_switch(vcpu);
@@ -3927,7 +3934,6 @@ out:
 	return r;
 }
 
-
 static int __vcpu_run(struct kvm_vcpu *vcpu)
 {
 	int r;
@@ -5026,6 +5032,9 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
 	}
 	vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
 
+	INIT_LIST_HEAD(&vcpu->arch.mmu_async_pf_done);
+	spin_lock_init(&vcpu->arch.mmu_async_pf_lock);
+
 	return 0;
 
 fail_mmu_destroy:
@@ -5078,8 +5087,10 @@ static void kvm_free_vcpus(struct kvm *kvm)
 	/*
 	 * Unpin any mmu pages first.
 	 */
-	kvm_for_each_vcpu(i, vcpu, kvm)
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		kvm_clear_async_pf_completion_queue(vcpu);
 		kvm_unload_vcpu_mmu(vcpu);
+	}
 	kvm_for_each_vcpu(i, vcpu, kvm)
 		kvm_arch_vcpu_free(vcpu);
 
@@ -5178,10 +5189,11 @@ void kvm_arch_flush_shadow(struct kvm *kvm)
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 {
 	return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
+		|| !list_empty_careful(&vcpu->arch.mmu_async_pf_done)
 		|| vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
-		|| vcpu->arch.nmi_pending ||
-		(kvm_arch_interrupt_allowed(vcpu) &&
-		 kvm_cpu_has_interrupt(vcpu));
+		|| vcpu->arch.nmi_pending
+		|| (kvm_arch_interrupt_allowed(vcpu) &&
+		    kvm_cpu_has_interrupt(vcpu));
 }
 
 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#45 lore raw

PATCH 7/11 Retry fault before vmentry

In reply to Gleb Natapov

When page is swapped in it is mapped into guest memory only after guest tries to access it again and generate another fault. To save this fault we can map it immediately since we know that guest is going to access the page.

---
 arch/x86/include/asm/kvm_host.h |    4 +++-
 arch/x86/kvm/mmu.c              |   18 ++++++++++++------
 arch/x86/kvm/paging_tmpl.h      |   32 ++++++++++++++++++++++++++++----
 3 files changed, 43 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -1
index e3cdbfe..6c781ea 100644
@@ -241,6 +241,8 @@ struct kvm_mmu_async_pf {
 	struct list_head link;
 	struct kvm_vcpu *vcpu;
 	struct mm_struct *mm;
+	gpa_t cr3;
+	u32 error_code;
 	gva_t gva;
 	unsigned long addr;
 	u64 token;
@@ -267,7 +269,7 @@ struct kvm_pio_request {
  */
 struct kvm_mmu {
 	void (*new_cr3)(struct kvm_vcpu *vcpu);
-	int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
+	int (*page_fault)(struct kvm_vcpu *vcpu, gpa_t cr3, gva_t gva, u32 err);
 	void (*free)(struct kvm_vcpu *vcpu);
 	gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
 	void (*prefetch_page)(struct kvm_vcpu *vcpu,
arch/x86/kvm/mmu.c +12 -6
index 31e837b..abe1ce9 100644
@@ -2171,7 +2171,7 @@ static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
 	return vaddr;
 }
 
-static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
+static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gpa_t cr3, gva_t gva,
 				u32 error_code)
 {
 	gfn_t gfn;
@@ -2322,6 +2322,8 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)
 			spin_lock(&vcpu->arch.mmu_async_pf_lock);
 			list_del(&work->link);
 			spin_unlock(&vcpu->arch.mmu_async_pf_lock);
+			vcpu->arch.mmu.page_fault(vcpu, (gpa_t)-1, work->gva,
+						  work->error_code);
 			put_page(work->page);
 			async_pf_work_free(work);
 		}
@@ -2338,6 +2340,7 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)
 	list_del(&work->link);
 	spin_unlock(&vcpu->arch.mmu_async_pf_lock);
 
+	vcpu->arch.mmu.page_fault(vcpu, (gpa_t)-1, work->gva, work->error_code);
 	vcpu->arch.pv_shm->reason = KVM_PV_REASON_PAGE_READY;
 	vcpu->arch.pv_shm->param = work->token;
 	kvm_inject_page_fault(vcpu, work->gva, 0);
@@ -2363,7 +2366,8 @@ static bool can_do_async_pf(struct kvm_vcpu *vcpu)
 	return !!(kvm_seg.selector & 3);
 }
 
-static int setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
+static int setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr3, gva_t gva,
+			  gfn_t gfn, u32 error_code)
 {
 	struct kvm_mmu_async_pf *work;
 
@@ -2378,6 +2382,8 @@ static int setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
 	atomic_set(&work->used, 1);
 	work->page = NULL;
 	work->vcpu = vcpu;
+	work->cr3 = cr3;
+	work->error_code = error_code;
 	work->gva = gva;
 	work->addr = gfn_to_hva(vcpu->kvm, gfn);
 	work->token = (vcpu->arch.async_pf_id++ << 12) | vcpu->vcpu_id;
@@ -2403,7 +2409,7 @@ retry_sync:
 	return 0;
 }
 
-static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
+static int tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t cr3, gva_t gpa,
 				u32 error_code)
 {
 	pfn_t pfn;
@@ -2426,7 +2432,7 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
 	mmu_seq = vcpu->kvm->mmu_notifier_seq;
 	smp_rmb();
 
-	if (can_do_async_pf(vcpu)) {
+	if (cr3 != (gpa_t)-1 && can_do_async_pf(vcpu)) {
 		r = gfn_to_pfn_async(vcpu->kvm, gfn, &pfn);
 		trace_kvm_mmu_try_async_get_page(r, pfn);
 	} else {
@@ -2436,7 +2442,7 @@ do_sync:
 	}
 
 	if (!r) {
-		if (!setup_async_pf(vcpu, gpa, gfn))
+		if (!setup_async_pf(vcpu, cr3, gpa, gfn, error_code))
 			goto do_sync;
 		return 0;
 	}
@@ -3006,7 +3012,7 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
 	int r;
 	enum emulation_result er;
 
-	r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
+	r = vcpu->arch.mmu.page_fault(vcpu, vcpu->arch.cr3, cr2, error_code);
 	if (r < 0)
 		goto out;
 
arch/x86/kvm/paging_tmpl.h +28 -4
index 9fe2ecd..b1fe61f 100644
@@ -375,7 +375,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  *  Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  *           a negative value on error.
  */
-static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
+static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t cr3, gva_t addr,
 			       u32 error_code)
 {
 	int write_fault = error_code & PFERR_WRITE_MASK;
@@ -388,6 +388,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
 	pfn_t pfn;
 	int level = PT_PAGE_TABLE_LEVEL;
 	unsigned long mmu_seq;
+	gpa_t curr_cr3 = vcpu->arch.cr3;
 
 	pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
 	kvm_mmu_audit(vcpu, "pre page fault");
@@ -396,6 +397,13 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
 	if (r)
 		return r;
 
+	if (curr_cr3 != cr3) {
+		vcpu->arch.cr3 = cr3;
+		paging_new_cr3(vcpu);
+		if (kvm_mmu_reload(vcpu))
+			goto switch_cr3;
+	}
+
 	/*
 	 * Look up the guest pte for the faulting address.
 	 */
@@ -406,6 +414,8 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
 	 * The page is not mapped by the guest.  Let the guest handle it.
 	 */
 	if (!r) {
+		if (curr_cr3 != vcpu->arch.cr3)
+			goto switch_cr3;
 		pgprintk("%s: guest page fault\n", __func__);
 		inject_page_fault(vcpu, addr, walker.error_code);
 		vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
@@ -420,7 +430,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
 	mmu_seq = vcpu->kvm->mmu_notifier_seq;
 	smp_rmb();
 
-	if (can_do_async_pf(vcpu)) {
+	if (cr3 != (gpa_t)-1 && can_do_async_pf(vcpu)) {
 		r = gfn_to_pfn_async(vcpu->kvm, walker.gfn, &pfn);
 		trace_kvm_mmu_try_async_get_page(r, pfn);
 	} else {
@@ -430,13 +440,17 @@ do_sync:
 	}
 
 	if (!r) {
-		if (!setup_async_pf(vcpu, addr, walker.gfn))
+		if (!setup_async_pf(vcpu, cr3, addr, walker.gfn, error_code))
 			goto do_sync;
+		if (curr_cr3 != vcpu->arch.cr3)
+			goto switch_cr3;
 		return 0;
 	}
 
 	/* mmio */
 	if (is_error_pfn(pfn)) {
+		if (curr_cr3 != vcpu->arch.cr3)
+			goto switch_cr3;
 		pgprintk("gfn %lx is mmio\n", walker.gfn);
 		kvm_release_pfn_clean(pfn);
 		return 1;
@@ -458,12 +472,22 @@ do_sync:
 	kvm_mmu_audit(vcpu, "post page fault (fixed)");
 	spin_unlock(&vcpu->kvm->mmu_lock);
 
+	if (curr_cr3 != vcpu->arch.cr3)
+		goto switch_cr3;
+
 	return write_pt;
 
 out_unlock:
 	spin_unlock(&vcpu->kvm->mmu_lock);
 	kvm_release_pfn_clean(pfn);
-	return 0;
+switch_cr3:
+	if (curr_cr3 != vcpu->arch.cr3) {
+		vcpu->arch.cr3 = curr_cr3;
+		paging_new_cr3(vcpu);
+		kvm_mmu_reload(vcpu);
+	}
+
+	return write_pt;
 }
 
 static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#47 lore raw

PATCH 8/11 Add "wait for page" hypercall.

In reply to Gleb Natapov

We want to be able to inject async pagefault into guest event if a guest is not executing userspace code. But in this case guest may receive async page fault in non-sleepable context. In this case it will be able to make "wait for page" hypercall vcpu will be put to sleep until page is swapped in and guest can continue without reschedule.

---
 arch/x86/include/asm/kvm_host.h |    2 ++
 arch/x86/kvm/mmu.c              |   35 ++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/mmutrace.h         |   19 +++++++++++++++++++
 arch/x86/kvm/x86.c              |    5 +++++
 include/linux/kvm_para.h        |    1 +
 5 files changed, 61 insertions(+), 1 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
index 6c781ea..d404b14 100644
@@ -456,6 +456,7 @@ struct kvm_vm_stat {
 
 struct kvm_vcpu_stat {
 	u32 pf_fixed;
+	u32 pf_async_wait;
 	u32 pf_guest;
 	u32 tlb_flush;
 	u32 invlpg;
@@ -676,6 +677,7 @@ void kvm_mmu_unload(struct kvm_vcpu *vcpu);
 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
 void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
 void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
+int kvm_pv_wait_for_async_pf(struct kvm_vcpu *vcpu);
 
 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
 
arch/x86/kvm/mmu.c +34 -1
index abe1ce9..3d33994 100644
@@ -2281,7 +2281,7 @@ static void async_pf_execute(struct slow_work *work)
 					apf->gva);
 
 	if (waitqueue_active(q))
-		wake_up_interruptible(q);
+		wake_up(q);
 
 	mmdrop(apf->mm);
 }
@@ -2351,6 +2351,39 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)
 	async_pf_work_free(work);
 }
 
+static bool kvm_asyc_pf_is_done(struct kvm_vcpu *vcpu)
+{
+	struct kvm_mmu_async_pf *p, *node;
+	bool found = false;
+
+	spin_lock(&vcpu->arch.mmu_async_pf_lock);
+	list_for_each_entry_safe(p, node, &vcpu->arch.mmu_async_pf_done, link) {
+		if (p->guest_task != vcpu->arch.pv_shm->current_task)
+			continue;
+		list_del(&p->link);
+		found = true;
+		break;
+	}
+	spin_unlock(&vcpu->arch.mmu_async_pf_lock);
+	if (found) {
+		vcpu->arch.mmu.page_fault(vcpu, (gpa_t)-1, p->gva,
+					  p->error_code);
+		put_page(p->page);
+		async_pf_work_free(p);
+		trace_kvm_mmu_async_pf_wait(vcpu->arch.pv_shm->current_task, 0);
+	}
+	return found;
+}
+
+int kvm_pv_wait_for_async_pf(struct kvm_vcpu *vcpu)
+{
+	++vcpu->stat.pf_async_wait;
+	trace_kvm_mmu_async_pf_wait(vcpu->arch.pv_shm->current_task, 1);
+	wait_event(vcpu->wq, kvm_asyc_pf_is_done(vcpu));
+
+	return 0;
+}
+
 static bool can_do_async_pf(struct kvm_vcpu *vcpu)
 {
 	struct kvm_segment kvm_seg;
arch/x86/kvm/mmutrace.h +19 -0
index d6dd63c..a74f718 100644
@@ -274,6 +274,25 @@ TRACE_EVENT(
 		  __entry->gva, __entry->address, page_to_pfn(__entry->page))
 );
 
+TRACE_EVENT(
+	kvm_mmu_async_pf_wait,
+	TP_PROTO(u64 task, bool wait),
+	TP_ARGS(task, wait),
+
+	TP_STRUCT__entry(
+		__field(u64, task)
+		__field(bool, wait)
+		),
+
+	TP_fast_assign(
+		__entry->task = task;
+		__entry->wait = wait;
+		),
+
+	TP_printk("task %#llx %s", __entry->task, __entry->wait ?
+		  "waits for PF" : "end wait for PF")
+);
+
 #endif /* _TRACE_KVMMMU_H */
 
 /* This part must be outside protection */
arch/x86/kvm/x86.c +5 -0
index e6bd3ad..9208796 100644
@@ -109,6 +109,7 @@ static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
 
 struct kvm_stats_debugfs_item debugfs_entries[] = {
 	{ "pf_fixed", VCPU_STAT(pf_fixed) },
+	{ "pf_async_wait", VCPU_STAT(pf_async_wait) },
 	{ "pf_guest", VCPU_STAT(pf_guest) },
 	{ "tlb_flush", VCPU_STAT(tlb_flush) },
 	{ "invlpg", VCPU_STAT(invlpg) },
@@ -3484,6 +3485,10 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
 	case KVM_HC_SETUP_SHM:
 		r = kvm_pv_setup_shm(vcpu, a0, a1, a2, &ret);
 		break;
+	case KVM_HC_WAIT_FOR_ASYNC_PF:
+		r = kvm_pv_wait_for_async_pf(vcpu);
+		ret = 0;
+		break;
 	default:
 		ret = -KVM_ENOSYS;
 		break;
include/linux/kvm_para.h +1 -0
index 1c37495..50296a6 100644
@@ -19,6 +19,7 @@
 #define KVM_HC_VAPIC_POLL_IRQ		1
 #define KVM_HC_MMU_OP			2
 #define KVM_HC_SETUP_SHM		3
+#define KVM_HC_WAIT_FOR_ASYNC_PF	4
 
 /*
  * hypercalls use architecture specific
Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#51 lore raw

PATCH 9/11 Maintain preemptability count even for !CONFIG_PREEMPT kernels

In reply to Gleb Natapov

Do not preempt kernel. Just maintain counter to know if task can sleep.

---
 include/linux/hardirq.h |    6 ++----
 include/linux/preempt.h |   22 ++++++++++++++++------
 kernel/sched.c          |    6 ------
 lib/kernel_lock.c       |    1 +
 4 files changed, 19 insertions(+), 16 deletions(-)
include/linux/hardirq.h +2 -4
index 6d527ee..a6b6040 100644
@@ -92,12 +92,11 @@
  */
 #define in_nmi()	(preempt_count() & NMI_MASK)
 
+#define PREEMPT_CHECK_OFFSET 1
 #if defined(CONFIG_PREEMPT)
 # define PREEMPT_INATOMIC_BASE kernel_locked()
-# define PREEMPT_CHECK_OFFSET 1
 #else
 # define PREEMPT_INATOMIC_BASE 0
-# define PREEMPT_CHECK_OFFSET 0
 #endif
 
 /*
@@ -116,12 +115,11 @@
 #define in_atomic_preempt_off() \
 		((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_CHECK_OFFSET)
 
+#define IRQ_EXIT_OFFSET (HARDIRQ_OFFSET-1)
 #ifdef CONFIG_PREEMPT
 # define preemptible()	(preempt_count() == 0 && !irqs_disabled())
-# define IRQ_EXIT_OFFSET (HARDIRQ_OFFSET-1)
 #else
 # define preemptible()	0
-# define IRQ_EXIT_OFFSET HARDIRQ_OFFSET
 #endif
 
 #if defined(CONFIG_SMP) || defined(CONFIG_GENERIC_HARDIRQS)
include/linux/preempt.h +16 -6
index 72b1a10..7d039ca 100644
@@ -82,14 +82,24 @@ do { \
 
 #else
 
-#define preempt_disable()		do { } while (0)
-#define preempt_enable_no_resched()	do { } while (0)
-#define preempt_enable()		do { } while (0)
+#define preempt_disable() \
+do { \
+	inc_preempt_count(); \
+	barrier(); \
+} while (0)
+
+#define preempt_enable() \
+do { \
+	barrier(); \
+	dec_preempt_count(); \
+} while (0)
+
+#define preempt_enable_no_resched()	preempt_enable()
 #define preempt_check_resched()		do { } while (0)
 
-#define preempt_disable_notrace()		do { } while (0)
-#define preempt_enable_no_resched_notrace()	do { } while (0)
-#define preempt_enable_notrace()		do { } while (0)
+#define preempt_disable_notrace()		preempt_disable()
+#define preempt_enable_no_resched_notrace()	preempt_enable()
+#define preempt_enable_notrace()		preempt_enable()
 
 #endif
 
kernel/sched.c +0 -6
index e886895..adb0415 100644
@@ -2561,10 +2561,8 @@ void sched_fork(struct task_struct *p, int clone_flags)
 #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
 	p->oncpu = 0;
 #endif
-#ifdef CONFIG_PREEMPT
 	/* Want to start with kernel preemption disabled. */
 	task_thread_info(p)->preempt_count = 1;
-#endif
 	plist_node_init(&p->pushable_tasks, MAX_PRIO);
 
 	put_cpu();
@@ -6944,11 +6942,7 @@ void __cpuinit init_idle(struct task_struct *idle, int cpu)
 	spin_unlock_irqrestore(&rq->lock, flags);
 
 	/* Set the preempt count _outside_ the spinlocks! */
-#if defined(CONFIG_PREEMPT)
 	task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
-#else
-	task_thread_info(idle)->preempt_count = 0;
-#endif
 	/*
 	 * The idle tasks have their own, simple scheduling class:
 	 */
lib/kernel_lock.c +1 -0
index 39f1029..6e2659d 100644
@@ -93,6 +93,7 @@ static inline void __lock_kernel(void)
  */
 static inline void __lock_kernel(void)
 {
+	preempt_disable();
 	_raw_spin_lock(&kernel_flag);
 }
 #endif
Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#54 lore raw

PATCH 11/11 Send async PF when guest is not in userspace too.

In reply to Gleb Natapov
---
 arch/x86/kvm/mmu.c |   16 ++++++----------
 1 files changed, 6 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu.c +6 -10
index 3d33994..21ec65a 100644
@@ -2358,7 +2358,7 @@ static bool kvm_asyc_pf_is_done(struct kvm_vcpu *vcpu)
 
 	spin_lock(&vcpu->arch.mmu_async_pf_lock);
 	list_for_each_entry_safe(p, node, &vcpu->arch.mmu_async_pf_done, link) {
-		if (p->guest_task != vcpu->arch.pv_shm->current_task)
+		if (p->token != vcpu->arch.pv_shm->param)
 			continue;
 		list_del(&p->link);
 		found = true;
@@ -2370,7 +2370,7 @@ static bool kvm_asyc_pf_is_done(struct kvm_vcpu *vcpu)
 					  p->error_code);
 		put_page(p->page);
 		async_pf_work_free(p);
-		trace_kvm_mmu_async_pf_wait(vcpu->arch.pv_shm->current_task, 0);
+		trace_kvm_mmu_async_pf_wait(vcpu->arch.pv_shm->param, 0);
 	}
 	return found;
 }
@@ -2378,7 +2378,7 @@ static bool kvm_asyc_pf_is_done(struct kvm_vcpu *vcpu)
 int kvm_pv_wait_for_async_pf(struct kvm_vcpu *vcpu)
 {
 	++vcpu->stat.pf_async_wait;
-	trace_kvm_mmu_async_pf_wait(vcpu->arch.pv_shm->current_task, 1);
+	trace_kvm_mmu_async_pf_wait(vcpu->arch.pv_shm->param, 1);
 	wait_event(vcpu->wq, kvm_asyc_pf_is_done(vcpu));
 
 	return 0;
@@ -2386,17 +2386,13 @@ int kvm_pv_wait_for_async_pf(struct kvm_vcpu *vcpu)
 
 static bool can_do_async_pf(struct kvm_vcpu *vcpu)
 {
-	struct kvm_segment kvm_seg;
-
 	if (!vcpu->arch.pv_shm ||
 	    !(vcpu->arch.pv_shm->features & KVM_PV_SHM_FEATURES_ASYNC_PF) ||
-	    kvm_event_needs_reinjection(vcpu))
+	    kvm_event_needs_reinjection(vcpu) ||
+	    !kvm_x86_ops->interrupt_allowed(vcpu))
 		return false;
 
-	kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
-
-	/* is userspace code? TODO check VM86 mode */
-	return !!(kvm_seg.selector & 3);
+	return true;
 }
 
 static int setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr3, gva_t gva,
Signature · 7 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#53 lore raw

PATCH 10/11 Handle async PF in non preemptable context.

In reply to Gleb Natapov

If async page fault is received by idle task or when preemp_count is not zero guest cannot reschedule, so make "wait for page" hypercall and comtinue only after a page is ready.

---
 arch/x86/kernel/kvm.c |   16 +++++++++++++++-
 1 files changed, 15 insertions(+), 1 deletions(-)
arch/x86/kernel/kvm.c +15 -1
index 79d291f..1bd8b8d 100644
@@ -162,10 +162,24 @@ int kvm_handle_pf(struct pt_regs *regs, unsigned long error_code)
 	switch (reason) {
 	default:
 		return 0;
-	case KVM_PV_REASON_PAGE_NP:
+	case KVM_PV_REASON_PAGE_NP: {
+		int cpu, idle;
+		cpu = get_cpu();
+		idle = idle_cpu(cpu);
+		put_cpu();
+
+		/*
+		 * We cannot reschedule. Wait for page to be ready.
+		 */
+		if (idle || preempt_count()) {
+			kvm_hypercall0(KVM_HC_WAIT_FOR_ASYNC_PF);
+			break;
+		}
+
 		/* real page is missing. */
 		apf_task_wait(current, token);
 		break;
+	}
 	case KVM_PV_REASON_PAGE_READY:
 		apf_task_wake(token);
 		break;
Signature · 1 line
1.6.3.3
#2 lore raw

Re: [PATCH 01/11] Add shared memory hypercall to PV Linux guest.

In reply to Gleb Natapov
Quoted 2 lines · Gleb Natapov

Add hypercall that allows guest and host to setup per cpu shared memory.

While it is pretty obvious that we should implement the asynchronous pagefaults for KVM, so a swap-in of a page the host swapped out does not stall the entire virtual CPU, I believe that adding extra data accesses at context switch time may not be the best tradeoff.

It may be better to simply tell the guest what address is faulting (or give the guest some other random unique number as a token). Then, once the host brings that page into memory, we can send a signal to the guest with that same token.

The problem of finding the task(s) associated with that token can be left to the guest. A little more complexity on the guest side, but probably worth it if we can avoid adding cost to the context switch path.

Quoted 11 lines
arch/x86/kernel/kvm.c +11 -0
+static void kvm_end_context_switch(struct task_struct *next)
+{
+	struct kvm_vcpu_pv_shm *pv_shm =
+		per_cpu(kvm_vcpu_pv_shm, smp_processor_id());
+
+	if (!pv_shm)
+		return;
+
+	pv_shm->current_task = (u64)next;
+}
+
Signature · 1 line
All rights reversed.
#3 lore raw

Re: [PATCH 01/11] Add shared memory hypercall to PV Linux guest.

Quoted 17 lines · Rik van Riel
Quoted 2 lines · Gleb Natapov

Add hypercall that allows guest and host to setup per cpu shared memory.

While it is pretty obvious that we should implement the asynchronous pagefaults for KVM, so a swap-in of a page the host swapped out does not stall the entire virtual CPU, I believe that adding extra data accesses at context switch time may not be the best tradeoff.

It may be better to simply tell the guest what address is faulting (or give the guest some other random unique number as a token). Then, once the host brings that page into memory, we can send a signal to the guest with that same token.

Quoted 6 lines

The problem of finding the task(s) associated with that token can be left to the guest. A little more complexity on the guest side, but probably worth it if we can avoid adding cost to the context switch path.

This is precisely what this series implements. The function below is leftover from previous implementation, not used by the rest of the patch and removed by a later patch. Just a left over from rebase. Sorry about that. Will be fixed for future submissions.

Quoted 16 lines
Quoted 11 lines
arch/x86/kernel/kvm.c +11 -0
+static void kvm_end_context_switch(struct task_struct *next)
+{
+	struct kvm_vcpu_pv_shm *pv_shm =
+		per_cpu(kvm_vcpu_pv_shm, smp_processor_id());
+
+	if (!pv_shm)
+		return;
+
+	pv_shm->current_task = (u64)next;
+}
+
Signature · 1 line
All rights reversed.

--
Gleb.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#10 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

In reply to Gleb Natapov

arch/x86/mm/fault.c do_page_fault(struct pt_regs *regs, unsigned long error_code) · line 957

Quoted 11 lines · Gleb Natapov
arch/x86/mm/fault.c +3 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	int write;
 	int fault;
 
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one. Please consolidate them into a single callback site, this is a hotpath on x86.

And please always Cc: the x86 maintainers to patches that touch x86 code.

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#39 lore raw

Re: [PATCH 04/11] Export __get_user_pages_fast.

In reply to Gleb Natapov
Quoted 19 lines · Gleb Natapov
---
 arch/x86/mm/gup.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/mm/gup.c +1 -0
index 71da1bc..cea0dfe 100644
@@ -8,6 +8,7 @@
 #include <linux/mm.h>
 #include <linux/vmstat.h>
 #include <linux/highmem.h>
+#include <linux/module.h>
 
 #include <asm/pgtable.h>
 

arch/x86/mm/gup.c int __get_user_pages_fast(unsigned long start, int nr_pages, int write, · line 278

Quoted 4 lines
arch/x86/mm/gup.c +1 -0
@@ -274,6 +275,7 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
 
 	return nr;
 }
+EXPORT_SYMBOL_GPL(__get_user_pages_fast);

Lack of explanation in the changelog and lack of Cc:s. I fixed the latter.

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#52 lore raw

Re: [PATCH 09/11] Maintain preemptability count even for !CONFIG_PREEMPT kernels

In reply to Gleb Natapov
Quoted 9 lines · Gleb Natapov

Do not preempt kernel. Just maintain counter to know if task can sleep.

---
 include/linux/hardirq.h |    6 ++----
 include/linux/preempt.h |   22 ++++++++++++++++------
 kernel/sched.c          |    6 ------
 lib/kernel_lock.c       |    1 +
 4 files changed, 19 insertions(+), 16 deletions(-)

Lack of explanation in the changelog and lack of Cc:s.

This whole patch-set should be Cc:-ed to a lot more people, and the acks of various maintainers are needed before it can be applied.

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

Re: [PATCH 01/11] Add shared memory hypercall to PV Linux guest.

In reply to Gleb Natapov
Quoted 4 lines · Gleb Natapov

Add hypercall that allows guest and host to setup per cpu shared memory.

Better to set this up as an MSR (with bit zero enabling, bits 1-5 features, and 64-byte alignment). This allows auto-reset on INIT and live migration using the existing MSR save/restore infrastructure.

Quoted 10 lines
  arch/x86/include/asm/kvm_host.h |    3 +
  arch/x86/include/asm/kvm_para.h |   11 +++++
  arch/x86/kernel/kvm.c           |   82 +++++++++++++++++++++++++++++++++++++++
  arch/x86/kernel/setup.c         |    1 +
  arch/x86/kernel/smpboot.c       |    3 +
  arch/x86/kvm/x86.c              |   70 +++++++++++++++++++++++++++++++++
  include/linux/kvm.h             |    1 +
  include/linux/kvm_para.h        |    4 ++
  8 files changed, 175 insertions(+), 0 deletions(-)

Please separate into guest and host patches.

Quoted 2 lines

+#define KVM_PV_SHM_VERSION 1

versions = bad, feature bits = good

arch/x86/include/asm/kvm_para.h struct kvm_mmu_op_release_pt { · line 60

Quoted 10 lines
arch/x86/include/asm/kvm_para.h +9 -0
+
+#define KVM_PV_SHM_FEATURES_ASYNC_PF		(1<<  0)
+
+struct kvm_vcpu_pv_shm {
+	__u64 features;
+	__u64 reason;
+	__u64 param;
+};
+

Some documentation for this?

Also, the name should reflect the pv pagefault use. For other uses we can register other areas.

arch/x86/kernel/kvm.c struct kvm_para_state { · line 45

Quoted 8 lines
  #define MMU_QUEUE_SIZE 1024
arch/x86/kernel/kvm.c +1 -0
@@ -37,6 +41,7 @@ struct kvm_para_state {
  };
 
  static DEFINE_PER_CPU(struct kvm_para_state, para_state);
+static DEFINE_PER_CPU(struct kvm_vcpu_pv_shm *, kvm_vcpu_pv_shm);
 

Easier to put the entire structure here, not a pointer.

arch/x86/kernel/kvm.c static void __init paravirt_ops_setup(void) · line 271

Quoted 13 lines
arch/x86/kernel/kvm.c +12 -0
+
+static int kvm_pv_reboot_notify(struct notifier_block *nb,
+				unsigned long code, void *unused)
+{
+	if (code == SYS_RESTART)
+		on_each_cpu(kvm_pv_unregister_shm, NULL, 1);
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block kvm_pv_reboot_nb = {
+        .notifier_call = kvm_pv_reboot_notify,
+};

Is this called on kexec, or do we need another hook?

Quoted 4 lines
arch/x86/kvm/x86.c +3 -0
+static int kvm_pv_setup_shm(struct kvm_vcpu *vcpu, unsigned long gpa,
+			    unsigned long size, unsigned long version,
+			    unsigned long *ret)

arch/x86/kvm/x86.c int kvm_emulate_halt(struct kvm_vcpu *vcpu) · line 3419

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+{
+	addr = gfn_to_hva(vcpu->kvm, gfn);
+	if (kvm_is_error_hva(addr))
+		return -EFAULT;
+
+	/* pin page with pv shared memory */
+	down_read(&mm->mmap_sem);
+	r = get_user_pages(current, mm, addr, 1, 1, 0,&vcpu->arch.pv_shm_page,
+			   NULL);
+	up_read(&mm->mmap_sem);

This fails if the memory area straddles a page boundary. Aligning would solve this. I prefer using put_user() though than a permanent get_user_pages().

Signature · 1 line
error compiling committee.c: too many arguments to function
#32 lore raw

Re: [PATCH 03/11] Handle asynchronous page fault in a PV guest.

In reply to Gleb Natapov
Quoted 10 lines · Gleb Natapov

Asynchronous page fault notifies vcpu that page it is trying to access is swapped out by a host. In response guest puts a task that caused the fault to sleep until page is swapped in again. When missing page is brought back into the memory guest is notified and task resumes execution.

arch/x86/include/asm/kvm_para.h +0 -0
index 90708b7..61e2aa3 100644

arch/x86/include/asm/kvm_para.h struct kvm_mmu_op_release_pt { · line 57

Quoted 6 lines
arch/x86/include/asm/kvm_para.h +2 -0
@@ -52,6 +52,9 @@ struct kvm_mmu_op_release_pt {
 
  #define KVM_PV_SHM_FEATURES_ASYNC_PF		(1<<  0)
 
+#define KVM_PV_REASON_PAGE_NP 1
+#define KVM_PV_REASON_PAGE_READY 2
 

_NOT_PRESENT would improve readability.

Quoted 17 lines
arch/x86/include/asm/kvm_para.h +15 -0
+static void apf_task_wait(struct task_struct *tsk, u64 token)
  {
+	u64 key = hash_64(token, KVM_TASK_SLEEP_HASHBITS);
+	struct kvm_task_sleep_head *b =&async_pf_sleepers[key];
+	struct kvm_task_sleep_node n, *e;
+	DEFINE_WAIT(wait);
+
+	spin_lock(&b->lock);
+	e = _find_apf_task(b, token);
+	if (e) {
+		/* dummy entry exist ->  wake up was delivered ahead of PF */
+		hlist_del(&e->link);
+		kfree(e);
+		spin_unlock(&b->lock);
+		return;
+	}

arch/x86/include/asm/kvm_para.h struct kvm_mmu_op_release_pt {

Quoted 10 lines
arch/x86/include/asm/kvm_para.h +10 -0
+
+	n.token = token;
+	init_waitqueue_head(&n.wq);
+	hlist_add_head(&n.link,&b->list);
+	spin_unlock(&b->lock);
+
+	for (;;) {
+		prepare_to_wait(&n.wq,&wait, TASK_UNINTERRUPTIBLE);
+		if (hlist_unhashed(&n.link))
+			break;

Don't you need locking here? At least for the implied memory barriers.

Quoted 11 lines
arch/x86/include/asm/kvm_para.h +10 -0
+		schedule();
+	}
+	finish_wait(&n.wq,&wait);
+
+	return;
+}
+
+static void apf_task_wake(u64 token)
+{
+	u64 key = hash_64(token, KVM_TASK_SLEEP_HASHBITS);

arch/x86/include/asm/kvm_para.h

Quoted 10 lines
arch/x86/include/asm/kvm_para.h +10 -0
+	struct kvm_task_sleep_head *b =&async_pf_sleepers[key];
+	struct kvm_task_sleep_node *n;
+
+	spin_lock(&b->lock);
+	n = _find_apf_task(b, token);
+	if (!n) {
+		/* PF was not yet handled. Add dummy entry for the token */
+		n = kmalloc(sizeof(*n), GFP_ATOMIC);
+		if (!n) {
+			printk(KERN_EMERG"async PF can't allocate memory\n");

Worrying. We could have an emergency pool of one node per cpu, and disable apf if we use it until it's returned. But that's a lot of complexity for an edge case, so a simpler solution would be welcome.

Quoted 17 lines
arch/x86/include/asm/kvm_para.h +11 -1
+int kvm_handle_pf(struct pt_regs *regs, unsigned long error_code)
+{
+	u64 reason, token;
  	struct kvm_vcpu_pv_shm *pv_shm =
  		per_cpu(kvm_vcpu_pv_shm, smp_processor_id());
 
  	if (!pv_shm)
-		return;
+		return 0;
+
+	reason = pv_shm->reason;
+	pv_shm->reason = 0;
+
+	token = pv_shm->param;
+
+	switch (reason) {

arch/x86/include/asm/kvm_para.h

Quoted 10 lines
arch/x86/include/asm/kvm_para.h +10 -0
+	default:
+		return 0;
+	case KVM_PV_REASON_PAGE_NP:
+		/* real page is missing. */
+		apf_task_wait(current, token);
+		break;
+	case KVM_PV_REASON_PAGE_READY:
+		apf_task_wake(token);
+		break;
+	}

Ah, reason is not a bitmask but an enumerator. __u32 is more friendly to i386 in that case.

Much of the code here is arch independent and would work well on non-x86 kvm ports. But we can always lay the burden of moving it on the non-x86 maintainers.

Signature · 1 line
error compiling committee.c: too many arguments to function
#43 lore raw

Re: [PATCH 06/11] Inject asynchronous page fault into a guest if page is swapped out.

In reply to Gleb Natapov
Quoted 8 lines · Gleb Natapov

If guest access swapped out memory do not swap it in from vcpu thread context. Setup slow work to do swapping and send async page fault to a guest.

Allow async page fault injection only when guest is in user mode since otherwise guest may be in non-sleepable context and will not be able to reschedule.

That loses us page cache accesses, which may be the majority of accesses in some workloads.

If we allow the guest to ignore a fault, and ensure that a second access to an apf page from the same vcpu doesn't trigger another apf, we can simply ignore the apf in a guest when we can't schedule.

Probably best done with an enable bit for kernel-mode apfs.

Quoted 8 lines
---
  arch/x86/include/asm/kvm_host.h |   20 +++
  arch/x86/kvm/mmu.c              |  243 ++++++++++++++++++++++++++++++++++++++-
  arch/x86/kvm/mmutrace.h         |   60 ++++++++++
  arch/x86/kvm/paging_tmpl.h      |   16 +++-
  arch/x86/kvm/x86.c              |   22 +++-

Much of the code is generic, please move it to virt/kvm.

arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, · line 2264

Quoted 4 lines
arch/x86/kvm/mmu.c +3 -0
+static void async_pf_execute(struct slow_work *work)
+{
+	struct page *page[1];

No need to make it an array, just pass its address.

Quoted 8 lines
arch/x86/kvm/mmu.c +7 -0
+	struct kvm_mmu_async_pf *apf =
+		container_of(work, struct kvm_mmu_async_pf, work);
+	wait_queue_head_t *q =&apf->vcpu->wq;
+
+	might_sleep();
+
+	down_read(&apf->mm->mmap_sem);

arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, · line 2281

Quoted 10 lines
arch/x86/kvm/mmu.c +10 -0
+	get_user_pages(current, apf->mm, apf->addr, 1, 1, 0, page, NULL);
+	up_read(&apf->mm->mmap_sem);
+
+	spin_lock(&apf->vcpu->arch.mmu_async_pf_lock);
+	list_add_tail(&apf->link,&apf->vcpu->arch.mmu_async_pf_done);
+	apf->page = page[0];
+	spin_unlock(&apf->vcpu->arch.mmu_async_pf_lock);
+
+	trace_kvm_mmu_async_pf_executed(apf->addr, apf->page, apf->token,
+					apf->gva);

_completed, but maybe better placed in vcpu context.

Quoted 5 lines
arch/x86/kvm/mmu.c +4 -0
+
+static bool can_do_async_pf(struct kvm_vcpu *vcpu)
+{
+	struct kvm_segment kvm_seg;

arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,

Quoted 10 lines
arch/x86/kvm/mmu.c +10 -0
+
+	if (!vcpu->arch.pv_shm ||
+	    !(vcpu->arch.pv_shm->features&  KVM_PV_SHM_FEATURES_ASYNC_PF) ||
+	    kvm_event_needs_reinjection(vcpu))
+		return false;
+
+	kvm_get_segment(vcpu,&kvm_seg, VCPU_SREG_CS);
+
+	/* is userspace code? TODO check VM86 mode */
+	return !!(kvm_seg.selector&  3);

There's a ->get_cpl() which is slightly faster. Note vm86 is perfectly fine for async pf.

Quoted 9 lines
arch/x86/kvm/mmu.c +8 -0
+static int setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
+{
+	struct kvm_mmu_async_pf *work;
+
+	/* setup slow work */
+
+	/* do alloc atomic since if we are going to sleep anyway we
+	   may as well sleep faulting in page */

arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,

Quoted 10 lines
arch/x86/kvm/mmu.c +10 -0
+	work = kmem_cache_zalloc(mmu_async_pf_cache, GFP_ATOMIC);
+	if (!work)
+		return 0;
+
+	atomic_set(&work->used, 1);
+	work->page = NULL;
+	work->vcpu = vcpu;
+	work->gva = gva;
+	work->addr = gfn_to_hva(vcpu->kvm, gfn);
+	work->token = (vcpu->arch.async_pf_id++<<  12) | vcpu->vcpu_id;

The shift truncates async_pf_id.

Signature · 7 lines
error compiling committee.c: too many arguments to function

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#46 lore raw

Re: [PATCH 07/11] Retry fault before vmentry

In reply to Gleb Natapov
Quoted 30 lines · Gleb Natapov

When page is swapped in it is mapped into guest memory only after guest tries to access it again and generate another fault. To save this fault we can map it immediately since we know that guest is going to access the page.

arch/x86/kvm/paging_tmpl.h +2 -1
index 9fe2ecd..b1fe61f 100644
@@ -375,7 +375,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
   *  Returns: 1 if we need to emulate the instruction, 0 otherwise, or
   *           a negative value on error.
   */
-static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
+static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t cr3, gva_t addr,
  			       u32 error_code)
  {
  	int write_fault = error_code&  PFERR_WRITE_MASK;
@@ -388,6 +388,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  	pfn_t pfn;
  	int level = PT_PAGE_TABLE_LEVEL;
  	unsigned long mmu_seq;
+	gpa_t curr_cr3 = vcpu->arch.cr3;
 
  	pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  	kvm_mmu_audit(vcpu, "pre page fault");
@@ -396,6 +397,13 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  	if (r)

arch/x86/kvm/paging_tmpl.h static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, · line 407

Quoted 10 lines
arch/x86/kvm/paging_tmpl.h +7 -0
  		return r;
 
+	if (curr_cr3 != cr3) {
+		vcpu->arch.cr3 = cr3;
+		paging_new_cr3(vcpu);
+		if (kvm_mmu_reload(vcpu))
+			goto switch_cr3;
+	}
+
 

This is a little frightening. I can't put my finger on anything though. But playing with cr3 under the guest's feet worries me.

Signature · 7 lines
error compiling committee.c: too many arguments to function

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#48 lore raw

Re: [PATCH 08/11] Add "wait for page" hypercall.

In reply to Gleb Natapov
Quoted 6 lines · Gleb Natapov

We want to be able to inject async pagefault into guest event if a guest is not executing userspace code. But in this case guest may receive async page fault in non-sleepable context. In this case it will be able to make "wait for page" hypercall vcpu will be put to sleep until page is swapped in and guest can continue without reschedule.

What's wrong with just 'hlt' and checking in the guest?

Signature · 1 line
error compiling committee.c: too many arguments to function
#49 lore raw

Re: [PATCH 08/11] Add "wait for page" hypercall.

Quoted 9 lines · Avi Kivity
Quoted 5 lines · Gleb Natapov

We want to be able to inject async pagefault into guest event if a guest is not executing userspace code. But in this case guest may receive async page fault in non-sleepable context. In this case it will be able to make "wait for page" hypercall vcpu will be put to sleep until page is swapped in and guest can continue without reschedule.

What's wrong with just 'hlt' and checking in the guest?

Halting here will leave vcpu with interrupt disabled and this will prevent "wake up" signal delivery. Enabling interrupts is also not an options since we can't be sure that vcpu can process interrupt at this point. And we can't allow NMI delivery for the same reason.

--
Gleb.

#50 lore raw

Re: [PATCH 08/11] Add "wait for page" hypercall.

Quoted 10 lines · Gleb Natapov
Quoted 8 lines · Avi Kivity
Quoted 6 lines · Gleb Natapov

We want to be able to inject async pagefault into guest event if a guest is not executing userspace code. But in this case guest may receive async page fault in non-sleepable context. In this case it will be able to make "wait for page" hypercall vcpu will be put to sleep until page is swapped in and guest can continue without reschedule.

Quoted 5 lines
Quoted 3 lines

What's wrong with just 'hlt' and checking in the guest?

Halting here will leave vcpu with interrupt disabled and this will prevent "wake up" signal delivery.

Page faults can be delivered with interrupts disabled.

Quoted 3 lines

Enabling interrupts is also not an options
since we can't be sure that vcpu can process interrupt at this point.

That's too bad, allowing interrupts in this context can help maintain responsiveness.

Signature · 7 lines
error compiling committee.c: too many arguments to function

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#44 lore raw

Re: [PATCH 06/11] Inject asynchronous page fault into a guest if page is swapped out.

In reply to Avi Kivity
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Gleb Natapov

If guest access swapped out memory do not swap it in from vcpu thread context. Setup slow work to do swapping and send async page fault to a guest.

Quoted 7 lines
Quoted 3 lines

Allow async page fault injection only when guest is in user mode since otherwise guest may be in non-sleepable context and will not be able to reschedule.

That loses us page cache accesses, which may be the majority of accesses in some workloads.

This is addressed later in the patch series.

Quoted 14 lines

If we allow the guest to ignore a fault, and ensure that a second access to an apf page from the same vcpu doesn't trigger another apf, we can simply ignore the apf in a guest when we can't schedule.

Probably best done with an enable bit for kernel-mode apfs.

Quoted 7 lines
---
 arch/x86/include/asm/kvm_host.h |   20 +++
 arch/x86/kvm/mmu.c              |  243 ++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/mmutrace.h         |   60 ++++++++++
 arch/x86/kvm/paging_tmpl.h      |   16 +++-
 arch/x86/kvm/x86.c              |   22 +++-
Quoted 2 lines

Much of the code is generic, please move it to virt/kvm.

OK, Will move generic part to virt.

Quoted 6 lines

arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, · line 2264

Quoted 3 lines
arch/x86/kvm/mmu.c +3 -0
+static void async_pf_execute(struct slow_work *work)
+{
+	struct page *page[1];

No need to make it an array, just pass its address.

OK

Quoted 29 lines
Quoted 7 lines
arch/x86/kvm/mmu.c +7 -0
+	struct kvm_mmu_async_pf *apf =
+		container_of(work, struct kvm_mmu_async_pf, work);
+	wait_queue_head_t *q =&apf->vcpu->wq;
+
+	might_sleep();
+
+	down_read(&apf->mm->mmap_sem);

arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, · line 2281

Quoted 10 lines
arch/x86/kvm/mmu.c +10 -0
+	get_user_pages(current, apf->mm, apf->addr, 1, 1, 0, page, NULL);
+	up_read(&apf->mm->mmap_sem);
+
+	spin_lock(&apf->vcpu->arch.mmu_async_pf_lock);
+	list_add_tail(&apf->link,&apf->vcpu->arch.mmu_async_pf_done);
+	apf->page = page[0];
+	spin_unlock(&apf->vcpu->arch.mmu_async_pf_lock);
+
+	trace_kvm_mmu_async_pf_executed(apf->addr, apf->page, apf->token,
+					apf->gva);

_completed, but maybe better placed in vcpu context.

Quoted 9 lines
arch/x86/kvm/mmu.c +9 -0
+
+static bool can_do_async_pf(struct kvm_vcpu *vcpu)
+{
+	struct kvm_segment kvm_seg;
+
+	if (!vcpu->arch.pv_shm ||
+	    !(vcpu->arch.pv_shm->features&  KVM_PV_SHM_FEATURES_ASYNC_PF) ||
+	    kvm_event_needs_reinjection(vcpu))
+		return false;
Quoted 9 lines

arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,

Quoted 5 lines
arch/x86/kvm/mmu.c +5 -0
+
+	kvm_get_segment(vcpu,&kvm_seg, VCPU_SREG_CS);
+
+	/* is userspace code? TODO check VM86 mode */
+	return !!(kvm_seg.selector&  3);

There's a ->get_cpl() which is slightly faster. Note vm86 is perfectly fine for async pf.

OK. But the code is removed by following patches anyway.

Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/mmu.c +12 -0
+static int setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
+{
+	struct kvm_mmu_async_pf *work;
+
+	/* setup slow work */
+
+	/* do alloc atomic since if we are going to sleep anyway we
+	   may as well sleep faulting in page */
+	work = kmem_cache_zalloc(mmu_async_pf_cache, GFP_ATOMIC);
+	if (!work)
+		return 0;
+
Quoted 9 lines

arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,

Quoted 6 lines
arch/x86/kvm/mmu.c +6 -0
+	atomic_set(&work->used, 1);
+	work->page = NULL;
+	work->vcpu = vcpu;
+	work->gva = gva;
+	work->addr = gfn_to_hva(vcpu->kvm, gfn);
+	work->token = (vcpu->arch.async_pf_id++<<  12) | vcpu->vcpu_id;

The shift truncates async_pf_id.

Will fix.

--
Gleb.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#33 lore raw

Re: [PATCH 03/11] Handle asynchronous page fault in a PV guest.

In reply to Avi Kivity
Quoted 39 lines · Avi Kivity
Quoted 10 lines · Gleb Natapov

Asynchronous page fault notifies vcpu that page it is trying to access is swapped out by a host. In response guest puts a task that caused the fault to sleep until page is swapped in again. When missing page is brought back into the memory guest is notified and task resumes execution.

arch/x86/include/asm/kvm_para.h +0 -0
index 90708b7..61e2aa3 100644

arch/x86/include/asm/kvm_para.h struct kvm_mmu_op_release_pt { · line 56

Quoted 5 lines
arch/x86/include/asm/kvm_para.h +2 -0
@@ -52,6 +52,9 @@ struct kvm_mmu_op_release_pt {
 
 #define KVM_PV_SHM_FEATURES_ASYNC_PF		(1<<  0)
 
+#define KVM_PV_REASON_PAGE_NP 1
+#define KVM_PV_REASON_PAGE_READY 2

_NOT_PRESENT would improve readability.

Quoted 20 lines
arch/x86/include/asm/kvm_para.h +19 -0
+static void apf_task_wait(struct task_struct *tsk, u64 token)
 {
+	u64 key = hash_64(token, KVM_TASK_SLEEP_HASHBITS);
+	struct kvm_task_sleep_head *b =&async_pf_sleepers[key];
+	struct kvm_task_sleep_node n, *e;
+	DEFINE_WAIT(wait);
+
+	spin_lock(&b->lock);
+	e = _find_apf_task(b, token);
+	if (e) {
+		/* dummy entry exist ->  wake up was delivered ahead of PF */
+		hlist_del(&e->link);
+		kfree(e);
+		spin_unlock(&b->lock);
+		return;
+	}
+
+	n.token = token;
+	init_waitqueue_head(&n.wq);
+	hlist_add_head(&n.link,&b->list);
Quoted 9 lines

arch/x86/include/asm/kvm_para.h

Quoted 6 lines
arch/x86/include/asm/kvm_para.h +6 -0
+	spin_unlock(&b->lock);
+
+	for (;;) {
+		prepare_to_wait(&n.wq,&wait, TASK_UNINTERRUPTIBLE);
+		if (hlist_unhashed(&n.link))
+			break;

Don't you need locking here? At least for the implied memory barriers.

May be memory barrier will be enough. Will look at it.

Quoted 16 lines
Quoted 16 lines
arch/x86/include/asm/kvm_para.h +16 -0
+		schedule();
+	}
+	finish_wait(&n.wq,&wait);
+
+	return;
+}
+
+static void apf_task_wake(u64 token)
+{
+	u64 key = hash_64(token, KVM_TASK_SLEEP_HASHBITS);
+	struct kvm_task_sleep_head *b =&async_pf_sleepers[key];
+	struct kvm_task_sleep_node *n;
+
+	spin_lock(&b->lock);
+	n = _find_apf_task(b, token);
+	if (!n) {
Quoted 9 lines

arch/x86/include/asm/kvm_para.h

Quoted 4 lines
arch/x86/include/asm/kvm_para.h +4 -0
+		/* PF was not yet handled. Add dummy entry for the token */
+		n = kmalloc(sizeof(*n), GFP_ATOMIC);
+		if (!n) {
+			printk(KERN_EMERG"async PF can't allocate memory\n");

Worrying. We could have an emergency pool of one node per cpu, and disable apf if we use it until it's returned. But that's a lot of complexity for an edge case, so a simpler solution would be welcome.

Currently this code can't trigger since "wake up" is always sent on the same vcpu as "not present", but I don't want this implementation detail to be part of guest/host interface. Idea you've described can be easy to implement. Will look into it.

Quoted 21 lines
Quoted 21 lines
arch/x86/include/asm/kvm_para.h +16 -1
+int kvm_handle_pf(struct pt_regs *regs, unsigned long error_code)
+{
+	u64 reason, token;
 	struct kvm_vcpu_pv_shm *pv_shm =
 		per_cpu(kvm_vcpu_pv_shm, smp_processor_id());
 
 	if (!pv_shm)
-		return;
+		return 0;
+
+	reason = pv_shm->reason;
+	pv_shm->reason = 0;
+
+	token = pv_shm->param;
+
+	switch (reason) {
+	default:
+		return 0;
+	case KVM_PV_REASON_PAGE_NP:
+		/* real page is missing. */
+		apf_task_wait(current, token);
Quoted 9 lines

arch/x86/include/asm/kvm_para.h

Quoted 5 lines
arch/x86/include/asm/kvm_para.h +5 -0
+		break;
+	case KVM_PV_REASON_PAGE_READY:
+		apf_task_wake(token);
+		break;
+	}

Ah, reason is not a bitmask but an enumerator. __u32 is more friendly to i386 in that case.

OK. Will need padding for 64 bit host case.

Quoted 6 lines

Much of the code here is arch independent and would work well on non-x86 kvm ports. But we can always lay the burden of moving it on the non-x86 maintainers.

Signature · 1 line
error compiling committee.c: too many arguments to function

--
Gleb.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#11 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

In reply to Ingo Molnar
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Gleb Natapov
arch/x86/mm/fault.c +0 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	int write;
 	int fault;
 
Quoted 9 lines

arch/x86/mm/fault.c do_page_fault(struct pt_regs *regs, unsigned long error_code) · line 957

Quoted 3 lines
arch/x86/mm/fault.c +3 -0
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one. Please consolidate them into a single callback site, this is a hotpath on x86.

This call is patched out by paravirt patching mechanism so overhead should be zero for non paravirt cases. What do you want to achieve by consolidate them into single callback? I mean the code will still exist and will have to be executed on every #PF. Is the goal to move them out of line?

Quoted 3 lines

And please always Cc: the x86 maintainers to patches that touch x86 code.

Will do.

--
Gleb.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#12 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 21 lines · Gleb Natapov
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Gleb Natapov
arch/x86/mm/fault.c +0 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	int write;
 	int fault;
 
Quoted 9 lines

arch/x86/mm/fault.c do_page_fault(struct pt_regs *regs, unsigned long error_code) · line 957

Quoted 3 lines
arch/x86/mm/fault.c +3 -0
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one. Please consolidate them into a single callback site, this is a hotpath on x86.

Quoted 2 lines

This call is patched out by paravirt patching mechanism so overhead should be zero for non paravirt cases. [...]

arch_handle_page_fault() isnt upstream yet - precisely what is the instruction sequence injected into do_page_fault() in the patched-out case?

Quoted 2 lines

[...] What do you want to achieve by consolidate them into single callback? [...]

Less bloat in a hotpath and a shared callback infrastructure.

Quoted 2 lines

[...] I mean the code will still exist and will have to be executed on every #PF. Is the goal to move them out of line?

The goal is to have a single callback site for all the users - which call-site is patched out ideally - on non-paravirt too if needed. Most of these callbacks/notifier-chains have are inactive most of the time.

I.e. a very low overhead 'conditional callback' facility, and a single one - not just lots of them sprinkled around the code.

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw

Re: [PATCH 01/11] Add shared memory hypercall to PV Linux guest.

In reply to Avi Kivity
Quoted 10 lines · Avi Kivity
Quoted 3 lines · Gleb Natapov

Add hypercall that allows guest and host to setup per cpu shared memory.

Better to set this up as an MSR (with bit zero enabling, bits 1-5 features, and 64-byte alignment). This allows auto-reset on INIT and live migration using the existing MSR save/restore infrastructure.

Hmm. Will do.

Quoted 10 lines
Quoted 9 lines
 arch/x86/include/asm/kvm_host.h |    3 +
 arch/x86/include/asm/kvm_para.h |   11 +++++
 arch/x86/kernel/kvm.c           |   82 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/setup.c         |    1 +
 arch/x86/kernel/smpboot.c       |    3 +
 arch/x86/kvm/x86.c              |   70 +++++++++++++++++++++++++++++++++
 include/linux/kvm.h             |    1 +
 include/linux/kvm_para.h        |    4 ++
 8 files changed, 175 insertions(+), 0 deletions(-)
Quoted 2 lines

Please separate into guest and host patches.

OK.

Quoted 4 lines
Quoted 1 line

+#define KVM_PV_SHM_VERSION 1

versions = bad, feature bits = good

I have both! Do you want me to drop version?

Quoted 12 lines

arch/x86/include/asm/kvm_para.h struct kvm_mmu_op_release_pt { · line 60

Quoted 9 lines
arch/x86/include/asm/kvm_para.h +9 -0
+
+#define KVM_PV_SHM_FEATURES_ASYNC_PF		(1<<  0)
+
+struct kvm_vcpu_pv_shm {
+	__u64 features;
+	__u64 reason;
+	__u64 param;
+};
+

Some documentation for this?

Quoted 3 lines

Also, the name should reflect the pv pagefault use. For other uses we can register other areas.

I wanted it to be generic, but I am fine with making it apf specific. It will allow to make it smaller too.

Quoted 9 lines

arch/x86/kernel/kvm.c struct kvm_para_state { · line 44

Quoted 7 lines

#define MMU_QUEUE_SIZE 1024

arch/x86/kernel/kvm.c +1 -0
@@ -37,6 +41,7 @@ struct kvm_para_state {
 };
 
 static DEFINE_PER_CPU(struct kvm_para_state, para_state);
+static DEFINE_PER_CPU(struct kvm_vcpu_pv_shm *, kvm_vcpu_pv_shm);

Easier to put the entire structure here, not a pointer.

OK.

Quoted 16 lines

arch/x86/kernel/kvm.c static void __init paravirt_ops_setup(void) · line 271

Quoted 13 lines
arch/x86/kernel/kvm.c +12 -0
+
+static int kvm_pv_reboot_notify(struct notifier_block *nb,
+				unsigned long code, void *unused)
+{
+	if (code == SYS_RESTART)
+		on_each_cpu(kvm_pv_unregister_shm, NULL, 1);
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block kvm_pv_reboot_nb = {
+        .notifier_call = kvm_pv_reboot_notify,
+};

Is this called on kexec, or do we need another hook?

This was added specifically for kexec to work. It was called in my test,

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/x86.c +7 -0
+static int kvm_pv_setup_shm(struct kvm_vcpu *vcpu, unsigned long gpa,
+			    unsigned long size, unsigned long version,
+			    unsigned long *ret)
+{
+	addr = gfn_to_hva(vcpu->kvm, gfn);
+	if (kvm_is_error_hva(addr))
+		return -EFAULT;
Quoted 8 lines

arch/x86/kvm/x86.c int kvm_emulate_halt(struct kvm_vcpu *vcpu) · line 3419

Quoted 6 lines
arch/x86/kvm/x86.c +6 -0
+
+	/* pin page with pv shared memory */
+	down_read(&mm->mmap_sem);
+	r = get_user_pages(current, mm, addr, 1, 1, 0,&vcpu->arch.pv_shm_page,
+			   NULL);
+	up_read(&mm->mmap_sem);

This fails if the memory area straddles a page boundary. Aligning

Good point.

Quoted 3 lines

would solve this. I prefer using put_user() though than a permanent get_user_pages().

I want to prevent it from been swapped out.

--
Gleb.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#13 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

In reply to Ingo Molnar
Quoted 24 lines · Ingo Molnar
Quoted 21 lines · Gleb Natapov
Quoted 20 lines · Ingo Molnar

arch/x86/mm/fault.c do_page_fault(struct pt_regs *regs, unsigned long error_code) · line 957

Quoted 11 lines · Gleb Natapov
arch/x86/mm/fault.c +3 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	int write;
 	int fault;
 
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one. Please consolidate them into a single callback site, this is a hotpath on x86.

Quoted 7 lines
Quoted 2 lines

This call is patched out by paravirt patching mechanism so overhead should be zero for non paravirt cases. [...]

arch_handle_page_fault() isnt upstream yet - precisely what is the instruction sequence injected into do_page_fault() in the patched-out case?

It is introduced by the same patch. The instruction inserted is:
xor %rax, %rax

Quoted 8 lines
Quoted 2 lines

[...] What do you want to achieve by consolidate them into single callback? [...]

Less bloat in a hotpath and a shared callback infrastructure.

Quoted 2 lines

[...] I mean the code will still exist and will have to be executed on every #PF. Is the goal to move them out of line?

Quoted 8 lines

The goal is to have a single callback site for all the users - which call-site is patched out ideally - on non-paravirt too if needed. Most of these callbacks/notifier-chains have are inactive most of the time.

I.e. a very low overhead 'conditional callback' facility, and a single one - not just lots of them sprinkled around the code.

Ingo

--
Gleb.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#14 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 28 lines · Gleb Natapov
Quoted 27 lines · Ingo Molnar
Quoted 23 lines · Gleb Natapov
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Gleb Natapov
arch/x86/mm/fault.c +0 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	int write;
 	int fault;
 
Quoted 9 lines

arch/x86/mm/fault.c do_page_fault(struct pt_regs *regs, unsigned long error_code) · line 957

Quoted 3 lines
arch/x86/mm/fault.c +3 -0
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one. Please consolidate them into a single callback site, this is a hotpath on x86.

This call is patched out by paravirt patching mechanism so overhead should be zero for non paravirt cases. [...]

Quoted 6 lines
Quoted 3 lines

arch_handle_page_fault() isnt upstream yet - precisely what is the instruction sequence injected into do_page_fault() in the patched-out case?

It is introduced by the same patch. The instruction inserted is:
xor %rax, %rax

ok.

My observations still stand:

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

[...] What do you want to achieve by consolidate them into single callback? [...]

Less bloat in a hotpath and a shared callback infrastructure.

Quoted 2 lines

[...] I mean the code will still exist and will have to be executed on every #PF. Is the goal to move them out of line?

Quoted 6 lines
Quoted 6 lines

The goal is to have a single callback site for all the users - which call-site is patched out ideally - on non-paravirt too if needed. Most of these callbacks/notifier-chains have are inactive most of the time.

I.e. a very low overhead 'conditional callback' facility, and a single one - not just lots of them sprinkled around the code.

looks like a golden opportunity to get this right.

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#15 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 50 lines · Ingo Molnar
Quoted 28 lines · Gleb Natapov
Quoted 27 lines · Ingo Molnar
Quoted 23 lines · Gleb Natapov
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Gleb Natapov
arch/x86/mm/fault.c +0 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	int write;
 	int fault;
 
Quoted 9 lines

arch/x86/mm/fault.c do_page_fault(struct pt_regs *regs, unsigned long error_code) · line 957

Quoted 3 lines
arch/x86/mm/fault.c +3 -0
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one. Please consolidate them into a single callback site, this is a hotpath on x86.

This call is patched out by paravirt patching mechanism so overhead should be zero for non paravirt cases. [...]

Quoted 6 lines
Quoted 3 lines

arch_handle_page_fault() isnt upstream yet - precisely what is the instruction sequence injected into do_page_fault() in the patched-out case?

It is introduced by the same patch. The instruction inserted is:
xor %rax, %rax

ok.

My observations still stand:

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

[...] What do you want to achieve by consolidate them into single callback? [...]

Less bloat in a hotpath and a shared callback infrastructure.

Quoted 2 lines

[...] I mean the code will still exist and will have to be executed on every #PF. Is the goal to move them out of line?

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

The goal is to have a single callback site for all the users - which call-site is patched out ideally - on non-paravirt too if needed. Most of these callbacks/notifier-chains have are inactive most of the time.

I.e. a very low overhead 'conditional callback' facility, and a single one - not just lots of them sprinkled around the code.

looks like a golden opportunity to get this right.

I'll look into it. Expect questions from me ;)

--
Gleb.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#16 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

In reply to Ingo Molnar
Quoted 50 lines · Ingo Molnar
Quoted 28 lines · Gleb Natapov
Quoted 27 lines · Ingo Molnar
Quoted 23 lines · Gleb Natapov
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Gleb Natapov
arch/x86/mm/fault.c +0 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	int write;
 	int fault;
 
Quoted 9 lines

arch/x86/mm/fault.c do_page_fault(struct pt_regs *regs, unsigned long error_code) · line 957

Quoted 3 lines
arch/x86/mm/fault.c +3 -0
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one. Please consolidate them into a single callback site, this is a hotpath on x86.

This call is patched out by paravirt patching mechanism so overhead should be zero for non paravirt cases. [...]

Quoted 6 lines
Quoted 3 lines

arch_handle_page_fault() isnt upstream yet - precisely what is the instruction sequence injected into do_page_fault() in the patched-out case?

It is introduced by the same patch. The instruction inserted is:
xor %rax, %rax

ok.

My observations still stand:

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

[...] What do you want to achieve by consolidate them into single callback? [...]

Less bloat in a hotpath and a shared callback infrastructure.

Quoted 2 lines

[...] I mean the code will still exist and will have to be executed on every #PF. Is the goal to move them out of line?

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

The goal is to have a single callback site for all the users - which call-site is patched out ideally - on non-paravirt too if needed. Most of these callbacks/notifier-chains have are inactive most of the time.

I.e. a very low overhead 'conditional callback' facility, and a single one - not just lots of them sprinkled around the code.

looks like a golden opportunity to get this right.

Three existing callbacks are: kmemcheck, mmiotrace, notifier. Two of them kmemcheck, mmiotrace are enabled only for debugging, should not be performance concern. And notifier call sites (two of them) are deliberately, as explained by comment, not at the function entry, so can't be unified with others. (And kmemcheck also has two different call site BTW)

--
Gleb.

#25 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

In reply to Ingo Molnar
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Gleb Natapov
arch/x86/mm/fault.c +0 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
  	int write;
  	int fault;
 
Quoted 7 lines

arch/x86/mm/fault.c do_page_fault(struct pt_regs *regs, unsigned long error_code) · line 957

Quoted 3 lines
arch/x86/mm/fault.c +3 -0
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one.

There's another alternative - add our own exception vector for async page faults. Not sure if that is warranted though, especially if we already have other callbacks in do_page_fault() and we can consolidate them.

Signature · 7 lines
All rights reversed.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#26 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

In reply to Rik van Riel
Quoted 10 lines · Rik van Riel
Quoted 3 lines

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one.

There's another alternative - add our own exception vector for async page faults. Not sure if that is warranted though, especially if we already have other callbacks in do_page_fault() and we can consolidate them.

We can't add an exception vector since all the existing ones are either taken or reserved. We can try to use an interrupt vector as an exception, but that becomes messy, and I'm not sure hardware will allow us to inject an interrupt when interrupts are disabled.

Signature · 7 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#27 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 6 lines · Avi Kivity
Quoted 5 lines · Rik van Riel
Quoted 3 lines

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one.

Quoted 8 lines
Quoted 5 lines

There's another alternative - add our own exception vector for async page faults. Not sure if that is warranted though, especially if we already have other callbacks in do_page_fault() and we can consolidate them.

We can't add an exception vector since all the existing ones are either taken or reserved.

I believe some are reserved for operating system use.

That means the guest can pick one and tell the host to use that one to notify it.

Signature · 7 lines
All rights reversed.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#28 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 5 lines · Rik van Riel
Quoted 2 lines

We can't add an exception vector since all the existing ones are either taken or reserved.

I believe some are reserved for operating system use.

Table 6-1 says:

9 | | Coprocessor Segment Overrun (reserved) | Fault | No |
Floating-point instruction.2

  15 |  — |  (Intel reserved. Do not use.) |   | No |
  20-31 |  — | Intel reserved. Do not use.  |

32-255 | — | User Defined (Non-reserved) Interrupts | Interrupt
| | External interrupt or INT n instruction.

So we can only use 32-255, but these are not fault-like exceptions that can be delivered with interrupts disabled.

Signature · 7 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw

Re: [PATCH 01/11] Add shared memory hypercall to PV Linux guest.

In reply to Gleb Natapov
Quoted 7 lines · Gleb Natapov
Quoted 5 lines
Quoted 2 lines

+#define KVM_PV_SHM_VERSION 1

versions = bad, feature bits = good

I have both! Do you want me to drop version?

Yes. Once a kernel is released you can't realistically change the version.

Quoted 9 lines
Quoted 6 lines

Some documentation for this?

Also, the name should reflect the pv pagefault use. For other uses we can register other areas.

I wanted it to be generic, but I am fine with making it apf specific. It will allow to make it smaller too.

Maybe we can squeeze it into the page-fault error code?

Quoted 6 lines
Quoted 4 lines

would solve this. I prefer using put_user() though than a permanent get_user_pages().

I want to prevent it from been swapped out.

Since you don't prevent the page fault handler or code from being swapped out, you don't get anything out of it.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#7 lore raw

Re: [PATCH 01/11] Add shared memory hypercall to PV Linux guest.

Quoted 8 lines · Avi Kivity
Quoted 4 lines · Gleb Natapov
Quoted 3 lines
Quoted 1 line

+#define KVM_PV_SHM_VERSION 1

versions = bad, feature bits = good

I have both! Do you want me to drop version?

Yes. Once a kernel is released you can't realistically change the version.

Why not? If version doesn't match apf will not be used.

Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

Some documentation for this?

Also, the name should reflect the pv pagefault use. For other uses we can register other areas.

I wanted it to be generic, but I am fine with making it apf specific. It will allow to make it smaller too.

Maybe we can squeeze it into the page-fault error code?

apf has to pass two things into a guest kernel:
- event type (page not present/wake up)
- unique token
Error code has 32 bits and at least 1 of them should indicate that this is apf another one should indicate event type so this leaves us 30 bits for a token. 12 bits of a token is used to store vcpu id this leaves 18 bits for unique per vcpu id. Yes this may be enough. I don't think it is realistic to have more then 200000 outstanding apfs per vcpu. Alternately we can use CR2 to pass a token.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

would solve this. I prefer using put_user() though than a permanent get_user_pages().

I want to prevent it from been swapped out.

Since you don't prevent the page fault handler or code from being swapped out, you don't get anything out of it.

Performance. Currently it is accessed on each page fault and to access it gup+kmap should be done each and every time.

--
Gleb.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw

Re: [PATCH 01/11] Add shared memory hypercall to PV Linux guest.

Quoted 8 lines · Gleb Natapov
Quoted 6 lines
Quoted 3 lines

I have both! Do you want me to drop version?

Yes. Once a kernel is released you can't realistically change the version.

Why not? If version doesn't match apf will not be used.

Then you cause a large performance regression (assuming apf is any good). So there will be a lot of pressure to modify things incrementally via feature bits.

Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
Quoted 7 lines

Some documentation for this?

Also, the name should reflect the pv pagefault use. For other uses we can register other areas.

I wanted it to be generic, but I am fine with making it apf specific. It will allow to make it smaller too.

Maybe we can squeeze it into the page-fault error code?

Quoted 10 lines

apf has to pass two things into a guest kernel:
- event type (page not present/wake up)
- unique token
Error code has 32 bits and at least 1 of them should indicate that this is apf another one should indicate event type so this leaves us 30 bits for a token. 12 bits of a token is used to store vcpu id this leaves 18 bits for unique per vcpu id. Yes this may be enough. I don't think it is realistic to have more then 200000 outstanding apfs per vcpu. Alternately we can use CR2 to pass a token.

Or a combination of pfec and cr2, yes.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

would solve this. I prefer using put_user() though than a permanent get_user_pages().

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

I want to prevent it from been swapped out.

Since you don't prevent the page fault handler or code from being swapped out, you don't get anything out of it.

Performance. Currently it is accessed on each page fault and to access it gup+kmap should be done each and every time.

put_user() is just as fast as a kmap, and don't prevent page migration or defragmentation.

Note we still have to mark_page_dirty() unless we want to chase live migration bugs.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#34 lore raw

Re: [PATCH 03/11] Handle asynchronous page fault in a PV guest.

In reply to Gleb Natapov
Quoted 4 lines · Gleb Natapov

Asynchronous page fault notifies vcpu that page it is trying to access is swapped out by a host. In response guest puts a task that caused the fault to sleep until page is swapped in again. When missing page is brought back into the memory guest is notified and task resumes execution.

Can't you apply this to non-paravirt guests, and continue to deliver interrupts while waiting for the swapin?

It should allow the guest to schedule a different task.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#35 lore raw

Re: [PATCH 03/11] Handle asynchronous page fault in a PV guest.

Quoted 10 lines · Marcelo Tosatti
Quoted 4 lines · Gleb Natapov

Asynchronous page fault notifies vcpu that page it is trying to access is swapped out by a host. In response guest puts a task that caused the fault to sleep until page is swapped in again. When missing page is brought back into the memory guest is notified and task resumes execution.

Can't you apply this to non-paravirt guests, and continue to deliver interrupts while waiting for the swapin?

It should allow the guest to schedule a different task.

But how can I make the guest to not run the task that caused the fault?

--
Gleb.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#36 lore raw

Re: [PATCH 03/11] Handle asynchronous page fault in a PV guest.

Quoted 7 lines · Gleb Natapov
Quoted 6 lines · Marcelo Tosatti
Quoted 4 lines · Gleb Natapov

Asynchronous page fault notifies vcpu that page it is trying to access is swapped out by a host. In response guest puts a task that caused the fault to sleep until page is swapped in again. When missing page is brought back into the memory guest is notified and task resumes execution.

Quoted 5 lines
Quoted 4 lines

Can't you apply this to non-paravirt guests, and continue to deliver interrupts while waiting for the swapin?

It should allow the guest to schedule a different task.

But how can I make the guest to not run the task that caused the fault?

Any attempt to access the swapped out data will cause a #PF vmexit, since the translation is marked as not present. If there's swapin in progress, you wait for that swapin, otherwise start swapin and wait.

Its not as efficient as paravirt because you have to wait for a timer interrupt and the guest scheduler to decide to taskswitch, but OTOH its transparent.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#37 lore raw

Re: [PATCH 03/11] Handle asynchronous page fault in a PV guest.

Quoted 8 lines · Marcelo Tosatti

Any attempt to access the swapped out data will cause a #PF vmexit, since the translation is marked as not present. If there's swapin in progress, you wait for that swapin, otherwise start swapin and wait.

Its not as efficient as paravirt because you have to wait for a timer interrupt and the guest scheduler to decide to taskswitch, but OTOH its transparent.

With a dyntick guest the timer interrupt will come at the end of the time slice, likely after the page has been swapped in. That leaves smp reschedule interrupts and non-dyntick guests.

An advantage is that there is one code path for apf and non-apf. Another is that interrupts are processed, improving timekeeping and maybe responsiveness.

Signature · 1 line
error compiling committee.c: too many arguments to function
#29 lore raw

RE: [PATCH 02/11] Add "handle page fault" PV helper.

In reply to Avi Kivity

PkZyb206IEF2aSBLaXZpdHkNCj5TZW50OiAyMDA5xOoxMdTCM8jVIDEyOjU3DQo+DQo+T24gMTEv MDMvMjAwOSAwMTozNSBBTSwgUmlrIHZhbiBSaWVsIHdyb3RlOg0KPj4+IFdlIGNhbid0IGFkZCBh biBleGNlcHRpb24gdmVjdG9yIHNpbmNlIGFsbCB0aGUgZXhpc3RpbmcgDQo+b25lcyBhcmUgZWl0 aGVyDQo+Pj4gdGFrZW4gb3IgcmVzZXJ2ZWQuDQo+Pg0KPj4NCj4+IEkgYmVsaWV2ZSBzb21lIGFy ZSByZXNlcnZlZCBmb3Igb3BlcmF0aW5nIHN5c3RlbSB1c2UuDQo+DQo+VGFibGUgNi0xIHNheXM6 DQo+DQo+ICAgOSB8ICB8IENvcHJvY2Vzc29yIFNlZ21lbnQgT3ZlcnJ1biAocmVzZXJ2ZWQpICB8 ICBGYXVsdCB8ICBObyAgfCANCj5GbG9hdGluZy1wb2ludCBpbnN0cnVjdGlvbi4yDQo+ICAgMTUg fCAgoaogfCAgKEludGVsIHJlc2VydmVkLiBEbyBub3QgdXNlLikgfCAgIHwgTm8gfA0KPiAgIDIw LTMxIHwgIKGqIHwgSW50ZWwgcmVzZXJ2ZWQuIERvIG5vdCB1c2UuICB8DQo+ICAgMzItMjU1IHwg IKGqICB8IFVzZXIgRGVmaW5lZCAoTm9uLXJlc2VydmVkKSBJbnRlcnJ1cHRzIHwgIEludGVycnVw dCAgDQo+fCAgIHwgRXh0ZXJuYWwgaW50ZXJydXB0IG9yIElOVCBuIGluc3RydWN0aW9uLg0KPg0K PlNvIHdlIGNhbiBvbmx5IHVzZSAzMi0yNTUsIGJ1dCB0aGVzZSBhcmUgbm90IGZhdWx0LWxpa2Ug DQo+ZXhjZXB0aW9ucyB0aGF0IA0KPmNhbiBiZSBkZWxpdmVyZWQgd2l0aCBpbnRlcnJ1cHRzIGRp c2FibGVkLg0KPg0KDQp3b3VsZCB5b3UgcmVhbGx5IHdhbnQgdG8gaW5qZWN0IGEgZmF1bHQtbGlr ZSBleGNlcHRpb24gaGVyZT8gRmF1bHQNCmlzIGFyY2hpdHVyYWxseSBzeW5jaHJvbm91cyBldmVu dCB3aGlsZSBoZXJlIGFwZiBpcyBtb3JlIGxpa2UgYW4gDQphc3luY2hyb25vdXMgaW50ZXJydXB0 IGFzIGl0J3Mgbm90IGNhdXNlZCBieSBndWVzdCBpdHNlbGYuIElmIA0KZ3Vlc3QgaXMgd2l0aCBp bnRlcnJ1cHQgZGlzYWJsZWQsIHByZWVtcHRpb24gd29uJ3QgaGFwcGVuIGFuZCANCmFwZiBwYXRo IGp1c3QgZW5kcyB1cCAid2FpdCBmb3IgcGFnZSIgaHlwZXJjYWxsIHRvIHdhc3RlIGN5Y2xlcy4N Cg0KVGhhbmtzLA0KS2V2aW4=

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#30 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 28 lines · Kevin
Quoted 18 lines

From: Avi Kivity
Sent: 2009年11月3日 12:57

Quoted 2 lines · Rik van Riel
Quoted 2 lines

We can't add an exception vector since all the existing

ones are either

Quoted 5 lines
Quoted 2 lines

taken or reserved.

I believe some are reserved for operating system use.

Table 6-1 says:

9 | | Coprocessor Segment Overrun (reserved) | Fault | No |
Floating-point instruction.2

Quoted 10 lines
  15 |  — |  (Intel reserved. Do not use.) |   | No |
  20-31 |  — | Intel reserved. Do not use.  |

32-255 | — | User Defined (Non-reserved) Interrupts | Interrupt
| | External interrupt or INT n instruction.

So we can only use 32-255, but these are not fault-like exceptions that
can be delivered with interrupts disabled.

Quoted 6 lines

would you really want to inject a fault-like exception here? Fault is architurally synchronous event while here apf is more like an asynchronous interrupt as it's not caused by guest itself. If guest is with interrupt disabled, preemption won't happen and apf path just ends up "wait for page" hypercall to waste cycles.

An async page fault is, despite its name, synchronous, since it is associated with an instruction. It must either be delivered immediately or not at all.

It's true that in kernel mode you can't do much with an apf if interrupts are disabled, but you still want to receive apfs for user mode with interrupts disabled (for example due to interrupt shadow).

Signature · 7 lines
error compiling committee.c: too many arguments to function

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#17 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

In reply to Gleb Natapov
Quoted 58 lines · Gleb Natapov
Quoted 57 lines · Ingo Molnar
Quoted 28 lines · Gleb Natapov
Quoted 27 lines · Ingo Molnar
Quoted 23 lines · Gleb Natapov
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Gleb Natapov
arch/x86/mm/fault.c +0 -0
index f4cee90..14707dc 100644
@@ -952,6 +952,9 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	int write;
 	int fault;
 
Quoted 9 lines

arch/x86/mm/fault.c do_page_fault(struct pt_regs *regs, unsigned long error_code) · line 957

Quoted 3 lines
arch/x86/mm/fault.c +3 -0
+	if (arch_handle_page_fault(regs, error_code))
+		return;
+

This patch is not acceptable unless it's done cleaner. Currently we already have 3 callbacks in do_page_fault() (kmemcheck, mmiotrace, notifier), and this adds a fourth one. Please consolidate them into a single callback site, this is a hotpath on x86.

This call is patched out by paravirt patching mechanism so overhead should be zero for non paravirt cases. [...]

Quoted 6 lines
Quoted 3 lines

arch_handle_page_fault() isnt upstream yet - precisely what is the instruction sequence injected into do_page_fault() in the patched-out case?

It is introduced by the same patch. The instruction inserted is:
xor %rax, %rax

ok.

My observations still stand:

Quoted 14 lines
Quoted 14 lines
Quoted 2 lines

[...] What do you want to achieve by consolidate them into single callback? [...]

Less bloat in a hotpath and a shared callback infrastructure.

Quoted 2 lines

[...] I mean the code will still exist and will have to be executed on every #PF. Is the goal to move them out of line?

The goal is to have a single callback site for all the users - which call-site is patched out ideally - on non-paravirt too if needed. Most of these callbacks/notifier-chains have are inactive most of the time.

I.e. a very low overhead 'conditional callback' facility, and a single one - not just lots of them sprinkled around the code.

Quoted 8 lines
Quoted 2 lines

looks like a golden opportunity to get this right.

Three existing callbacks are: kmemcheck, mmiotrace, notifier. Two of them kmemcheck, mmiotrace are enabled only for debugging, should not be performance concern. And notifier call sites (two of them) are deliberately, as explained by comment, not at the function entry, so can't be unified with others. (And kmemcheck also has two different call site BTW)

We want mmiotrace to be generic distro capable so the overhead when the hook is not used is of concern.

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#18 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 11 lines · Ingo Molnar
Quoted 7 lines

Three existing callbacks are: kmemcheck, mmiotrace, notifier. Two of them kmemcheck, mmiotrace are enabled only for debugging, should not be performance concern. And notifier call sites (two of them) are deliberately, as explained by comment, not at the function entry, so can't be unified with others. (And kmemcheck also has two different call site BTW)

We want mmiotrace to be generic distro capable so the overhead when the hook is not used is of concern.

Maybe we should generalize paravirt-ops patching in case if (x) f() is deemed too expensive.

Signature · 7 lines
error compiling committee.c: too many arguments to function

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#19 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 8 lines · Avi Kivity
Quoted 7 lines · Ingo Molnar
Quoted 6 lines

Three existing callbacks are: kmemcheck, mmiotrace, notifier. Two of them kmemcheck, mmiotrace are enabled only for debugging, should not be performance concern. And notifier call sites (two of them) are deliberately, as explained by comment, not at the function entry, so can't be unified with others. (And kmemcheck also has two different call site BTW)

Quoted 5 lines
Quoted 2 lines

We want mmiotrace to be generic distro capable so the overhead when the hook is not used is of concern.

Maybe we should generalize paravirt-ops patching in case if (x) f() is deemed too expensive.

Yes, that's a nice idea. We have quite a number of 'conditional callbacks' in various critical paths that could be made lighter via such a technique.

It would also free new callbacks from the 'it increases overhead even if unused' criticism and made it easier to add them.

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#20 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 10 lines · Ingo Molnar
Quoted 3 lines

Maybe we should generalize paravirt-ops patching in case if (x) f() is deemed too expensive.

Yes, that's a nice idea. We have quite a number of 'conditional callbacks' in various critical paths that could be made lighter via such a technique.

It would also free new callbacks from the 'it increases overhead even if unused' criticism and made it easier to add them.

We can take the "immediate values" infrastructure as a first step. Has that been merged?

Signature · 7 lines
error compiling committee.c: too many arguments to function

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#21 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 7 lines · Avi Kivity
Quoted 6 lines · Ingo Molnar
Quoted 2 lines

Maybe we should generalize paravirt-ops patching in case if (x) f() is deemed too expensive.

Yes, that's a nice idea. We have quite a number of 'conditional callbacks' in various critical paths that could be made lighter via such a technique.

Quoted 5 lines
Quoted 2 lines

It would also free new callbacks from the 'it increases overhead even if unused' criticism and made it easier to add them.

We can take the "immediate values" infrastructure as a first step. Has that been merged?

No, there were doubts about whether patching in live instructions like that is safe on all CPU types.

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#22 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 6 lines · Ingo Molnar
Quoted 3 lines

We can take the "immediate values" infrastructure as a first step. Has that been merged?

No, there were doubts about whether patching in live instructions like that is safe on all CPU types.

Ah, I remember. Doesn't the trick of going through a breakpoint instruction work?

Signature · 7 lines
error compiling committee.c: too many arguments to function

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#23 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

In reply to Ingo Molnar
Quoted 17 lines · Ingo Molnar
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Ingo Molnar
Quoted 6 lines

Three existing callbacks are: kmemcheck, mmiotrace, notifier. Two of them kmemcheck, mmiotrace are enabled only for debugging, should not be performance concern. And notifier call sites (two of them) are deliberately, as explained by comment, not at the function entry, so can't be unified with others. (And kmemcheck also has two different call site BTW)

Quoted 5 lines
Quoted 2 lines

We want mmiotrace to be generic distro capable so the overhead when the hook is not used is of concern.

Maybe we should generalize paravirt-ops patching in case if (x) f() is deemed too expensive.

Quoted 7 lines

Yes, that's a nice idea. We have quite a number of 'conditional callbacks' in various critical paths that could be made lighter via such a technique.

It would also free new callbacks from the 'it increases overhead even if unused' criticism and made it easier to add them.

There are a number of other things were we permanently bind to a single instance of something, too. Optimizing those away would be nice. Consider memcpy(), where we may want to have different implementations for different processors.

-hpa

Signature · 8 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#24 lore raw

Re: [PATCH 02/11] Add "handle page fault" PV helper.

Quoted 22 lines · H. Peter Anvin
Quoted 21 lines · Ingo Molnar
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Ingo Molnar
Quoted 6 lines

Three existing callbacks are: kmemcheck, mmiotrace, notifier. Two of them kmemcheck, mmiotrace are enabled only for debugging, should not be performance concern. And notifier call sites (two of them) are deliberately, as explained by comment, not at the function entry, so can't be unified with others. (And kmemcheck also has two different call site BTW)

Quoted 5 lines
Quoted 2 lines

We want mmiotrace to be generic distro capable so the overhead when the hook is not used is of concern.

Maybe we should generalize paravirt-ops patching in case if (x) f() is deemed too expensive.

Yes, that's a nice idea. We have quite a number of 'conditional callbacks' in various critical paths that could be made lighter via such a technique.

Quoted 7 lines
Quoted 2 lines

It would also free new callbacks from the 'it increases overhead even if unused' criticism and made it easier to add them.

There are a number of other things were we permanently bind to a single instance of something, too. Optimizing those away would be nice. Consider memcpy(), where we may want to have different implementations for different processors.

yeah.

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>