Skip to contentSkip to conversation

Add async page fault test

PATCH Superseded Gleb Natapov 3 participants 8 messages May 8, 2012 – May 9, 2012 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add async page fault test

config-x86-common.mak +3 -1
index c8fbda7..6976f78 100644
@@ -34,7 +34,7 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \
                $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat \
                $(TEST_DIR)/kvmclock_test.flat  $(TEST_DIR)/eventinj.flat \
-               $(TEST_DIR)/s3.flat $(TEST_DIR)/pmu.flat
+               $(TEST_DIR)/s3.flat $(TEST_DIR)/pmu.flat $(TEST_DIR)/asyncpf.flat
 
 ifdef API
 tests-common += api/api-sample
@@ -90,6 +90,8 @@ $(TEST_DIR)/s3.elf: $(cstart.o) $(TEST_DIR)/s3.o
 
 $(TEST_DIR)/pmu.elf: $(cstart.o) $(TEST_DIR)/pmu.o
 
+$(TEST_DIR)/asyncpf.elf: $(cstart.o) $(TEST_DIR)/asyncpf.o
+
 arch_clean:
 	$(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat $(TEST_DIR)/*.elf \
 	$(TEST_DIR)/.*.d $(TEST_DIR)/lib/.*.d $(TEST_DIR)/lib/*.o
lib/x86/processor.h +5 -0
index c7e1afb..f700e9c 100644
@@ -302,4 +302,9 @@ static inline void invlpg(void *va)
 {
 	asm volatile("invlpg (%0)" ::"r" (va) : "memory");
 }
+
+static inline void halt(void)
+{
+	asm volatile("hlt");
+}
 #endif
lib/x86/vm.c +5 -0
index 550ec9b..b030934 100644
@@ -219,6 +219,11 @@ void *vmalloc(unsigned long size)
     return mem;
 }
 
+unsigned long virt_to_phys_cr3(void *mem)
+{
+    return (get_pte(phys_to_virt(read_cr3()), mem) & PTE_ADDR) + ((ulong)mem & (PAGE_SIZE - 1));
+}
+
 void vfree(void *mem)
 {
     unsigned long size = ((unsigned long *)mem)[-1];
lib/x86/vm.h +1 -0
index 71ab4a8..ff4842f 100644
@@ -22,6 +22,7 @@ void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
 void *alloc_vpage(void);
 void *alloc_vpages(ulong nr);
+unsigned long virt_to_phys_cr3(void *mem);
 
 void install_pte(unsigned long *cr3,
                         int pte_level,
x86/asyncpf.c new file +98 -0

98 lines not on this page · raw message

--
Gleb.

#1 lore raw
Quoted 1 line · Gleb Natapov

Please describe the regression you're testing for. We could even link it to the fix with the commit hash.

lib/x86/vm.h void vfree(void *mem); · line 25

Quoted 12 lines

void vfree(void *mem)

 {
     unsigned long size = ((unsigned long *)mem)[-1];
lib/x86/vm.h +1 -0
index 71ab4a8..ff4842f 100644
@@ -22,6 +22,7 @@ void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
 void *alloc_vpage(void);
 void *alloc_vpages(ulong nr);
+unsigned long virt_to_phys_cr3(void *mem);

uint64_t.

lib/x86/vm.h line 5

Quoted 6 lines
lib/x86/vm.h +5 -0
@@ -0,0 +1,98 @@
+/*
+ * Async PF test. For the test to actually do anywathing it ineeds to be started
+ * in memory cgroup with 512M of memory and with more then 1G memory provided 
+ * to the guest.
+ */

Please include instructions or a script on how to do that.

Alterative ways of doing this:
- file-backed memory using FUSE to control paging
- add madvise(MADV_DONTNEED) support to testdev, and have the guest trigger page-in itself.

I'm not asking to change this test, just providing ideas for the future in case fine-grained control is needed. It also doesn't thrash the disk.

Quoted 13 lines
lib/x86/vm.h +13 -0
+#include "x86/msr.h"
+#include "x86/processor.h"
+#include "x86/apic-defs.h"
+#include "x86/apic.h"
+#include "x86/desc.h"
+#include "x86/isr.h"
+#include "x86/vm.h"
+
+#include "libcflat.h"
+#include <stdint.h>
+
+#define KVM_PV_REASON_PAGE_NOT_PRESENT 1
+#define KVM_PV_REASON_PAGE_READY 2

lib/x86/vm.h line 28

Quoted 10 lines
lib/x86/vm.h +10 -0
+
+#define MSR_KVM_ASYNC_PF_EN 0x4b564d02
+
+#define KVM_ASYNC_PF_ENABLED                    (1 << 0)
+#define KVM_ASYNC_PF_SEND_ALWAYS                (1 << 1)
+
+volatile uint32_t apf_reason __attribute__((aligned(64)));
+char *buf;
+volatile uint64_t  i;
+volatile unsigned long phys;

uint64_t.

lib/x86/vm.h line 35

Quoted 7 lines
lib/x86/vm.h +7 -0
+
+static void pf_isr(struct ex_regs *r)
+{
+	void* virt = (void*)((ulong)(buf+i) & ~4095ul);
+
+	switch (get_apf_reason()) {
+		case 0:

default:

lib/x86/vm.h line 42

Quoted 7 lines
lib/x86/vm.h +7 -0
+			printf("unexpected #PF at %p\n", read_cr2());
+			fail = true;
+			break;
+		case KVM_PV_REASON_PAGE_NOT_PRESENT:
+			phys = virt_to_phys_cr3(virt);
+			install_pte(phys_to_virt(read_cr3()), 1, virt, phys, 0);
+			write_cr3(read_cr3());

What's the point of these?

lib/x86/vm.h line 46

Quoted 4 lines
lib/x86/vm.h +4 -0
+			printf("Got not present #PF token %x virt addr %p phys addr %p\n", read_cr2(), virt, phys);
+			while(phys) {
+				irq_enable();
+				halt();

Racy... you need safe_halt() here.

Quoted 15 lines
lib/x86/vm.h +14 -0
+				irq_disable();
+			}
+			break;
+		case KVM_PV_REASON_PAGE_READY:
+			printf("Got present #PF token %x\n", read_cr2());
+			if ((uint32_t)read_cr2() == ~0)
+				break;
+			install_pte(phys_to_virt(read_cr3()), 1, virt, phys | PTE_PRESENT | PTE_WRITE, 0);
+			write_cr3(read_cr3());
+			phys = 0;
+			break;
+	}
+}
+
Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 6 lines · Avi Kivity
Quoted 1 line · Gleb Natapov

Please describe the regression you're testing for. We could even link it to the fix with the commit hash.

The test does not really tests for a regression, because there wasn't one. It test that prefault does not generates spurious #PFs. Will write that.

Quoted 15 lines

lib/x86/vm.h void vfree(void *mem); · line 25

Quoted 13 lines

void vfree(void *mem)

 {
     unsigned long size = ((unsigned long *)mem)[-1];
lib/x86/vm.h +1 -0
index 71ab4a8..ff4842f 100644
@@ -22,6 +22,7 @@ void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
 void *alloc_vpage(void);
 void *alloc_vpages(ulong nr);
+unsigned long virt_to_phys_cr3(void *mem);

uint64_t.

virt_to_phys() also unsigned long. And get_pte() that virt_to_phys_cr3() uses also. I guess the code is not ready for more then 2^32 memory in 32bit VM.

Quoted 10 lines

lib/x86/vm.h line 5

Quoted 7 lines
lib/x86/vm.h +5 -0
@@ -0,0 +1,98 @@
+/*
+ * Async PF test. For the test to actually do anywathing it ineeds to be started
+ * in memory cgroup with 512M of memory and with more then 1G memory provided 
+ * to the guest.
+ */

Please include instructions or a script on how to do that.

OK.

Quoted 2 lines

Alterative ways of doing this:
- file-backed memory using FUSE to control paging

Not sure how that can be done.

Quoted 2 lines

- add madvise(MADV_DONTNEED) support to testdev, and have the guest trigger page-in itself.

MADV_DONTNEED will drop page, not swap it out.

Quoted 30 lines

I'm not asking to change this test, just providing ideas for the future in case fine-grained control is needed. It also doesn't thrash the disk.

Quoted 13 lines
lib/x86/vm.h +13 -0
+#include "x86/msr.h"
+#include "x86/processor.h"
+#include "x86/apic-defs.h"
+#include "x86/apic.h"
+#include "x86/desc.h"
+#include "x86/isr.h"
+#include "x86/vm.h"
+
+#include "libcflat.h"
+#include <stdint.h>
+
+#define KVM_PV_REASON_PAGE_NOT_PRESENT 1
+#define KVM_PV_REASON_PAGE_READY 2

lib/x86/vm.h line 28

Quoted 10 lines
lib/x86/vm.h +10 -0
+
+#define MSR_KVM_ASYNC_PF_EN 0x4b564d02
+
+#define KVM_ASYNC_PF_ENABLED                    (1 << 0)
+#define KVM_ASYNC_PF_SEND_ALWAYS                (1 << 1)
+
+volatile uint32_t apf_reason __attribute__((aligned(64)));
+char *buf;
+volatile uint64_t  i;
+volatile unsigned long phys;

uint64_t.

Quoted 9 lines

lib/x86/vm.h line 35

Quoted 7 lines
lib/x86/vm.h +7 -0
+
+static void pf_isr(struct ex_regs *r)
+{
+	void* virt = (void*)((ulong)(buf+i) & ~4095ul);
+
+	switch (get_apf_reason()) {
+		case 0:

default:

I'd rather make deafult: fail the test. It shouldn't happen.

Quoted 11 lines

lib/x86/vm.h line 42

Quoted 8 lines
lib/x86/vm.h +7 -0
+			printf("unexpected #PF at %p\n", read_cr2());
+			fail = true;
+			break;
+		case KVM_PV_REASON_PAGE_NOT_PRESENT:
+			phys = virt_to_phys_cr3(virt);
+			install_pte(phys_to_virt(read_cr3()), 1, virt, phys, 0);
+			write_cr3(read_cr3());

What's the point of these?

Shouldn't we reload page tables after changing them?

Quoted 6 lines

lib/x86/vm.h line 46

Quoted 4 lines
lib/x86/vm.h +4 -0
+			printf("Got not present #PF token %x virt addr %p phys addr %p\n", read_cr2(), virt, phys);
+			while(phys) {
+				irq_enable();
+				halt();

Racy... you need safe_halt() here.

The code generated is sti; hlt; cli. By I as well may add safe_halt() to do sti; hlt explicit.

Quoted 19 lines
Quoted 16 lines
lib/x86/vm.h +14 -0
+				irq_disable();
+			}
+			break;
+		case KVM_PV_REASON_PAGE_READY:
+			printf("Got present #PF token %x\n", read_cr2());
+			if ((uint32_t)read_cr2() == ~0)
+				break;
+			install_pte(phys_to_virt(read_cr3()), 1, virt, phys | PTE_PRESENT | PTE_WRITE, 0);
+			write_cr3(read_cr3());
+			phys = 0;
+			break;
+	}
+}
+
Signature · 1 line
error compiling committee.c: too many arguments to function

--
Gleb.

#3 lore raw
Quoted 15 lines · Gleb Natapov
Quoted 15 lines

lib/x86/vm.h void vfree(void *mem); · line 25

Quoted 13 lines

void vfree(void *mem)

 {
     unsigned long size = ((unsigned long *)mem)[-1];
lib/x86/vm.h +1 -0
index 71ab4a8..ff4842f 100644
@@ -22,6 +22,7 @@ void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
 void *alloc_vpage(void);
 void *alloc_vpages(ulong nr);
+unsigned long virt_to_phys_cr3(void *mem);

uint64_t.

Quoted 3 lines

virt_to_phys() also unsigned long. And get_pte() that virt_to_phys_cr3() uses also. I guess the code is not ready for more then 2^32 memory in 32bit VM.

It's certainly not enterprise quality yet. But let's not add more problems.

Quoted 7 lines
Quoted 2 lines

Alterative ways of doing this:
- file-backed memory using FUSE to control paging

Not sure how that can be done.

Quoted 2 lines

- add madvise(MADV_DONTNEED) support to testdev, and have the guest trigger page-in itself.

MADV_DONTNEED will drop page, not swap it out.

Right, but it will be have to be reloaded from disk (it has to be file-backed for this to work). If it's dirty, sync it first.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Gleb Natapov
Quoted 15 lines

lib/x86/vm.h void vfree(void *mem); · line 25

Quoted 13 lines

void vfree(void *mem)

 {
     unsigned long size = ((unsigned long *)mem)[-1];
lib/x86/vm.h +1 -0
index 71ab4a8..ff4842f 100644
@@ -22,6 +22,7 @@ void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
 void *alloc_vpage(void);
 void *alloc_vpages(ulong nr);
+unsigned long virt_to_phys_cr3(void *mem);

uint64_t.

Quoted 6 lines
Quoted 3 lines

virt_to_phys() also unsigned long. And get_pte() that virt_to_phys_cr3() uses also. I guess the code is not ready for more then 2^32 memory in 32bit VM.

It's certainly not enterprise quality yet. But let's not add more problems.

Okay.

Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

Alterative ways of doing this:
- file-backed memory using FUSE to control paging

Not sure how that can be done.

Quoted 2 lines

- add madvise(MADV_DONTNEED) support to testdev, and have the guest trigger page-in itself.

MADV_DONTNEED will drop page, not swap it out.

Right, but it will be have to be reloaded from disk (it has to be file-backed for this to work). If it's dirty, sync it first.

Hmm, yes if it is file backed it may work. Setting up qemu to use file backed memory is one more complication while running the test though. I haven't checked by I am not sure that MADV_DONTNEED will drop page immediately though. It probably puts it on some list to be freed later. Hmm actually looking at the comments it seems like this is what happens:

/*
* Application no longer needs these pages. If the pages are dirty,
* it's OK to just throw them away. The app will be more careful about
* data it wants to keep. Be sure to free swap resources too. The
* zap_page_range call sets things up for shrink_active_list to actually
* free
* these pages later if no one else has touched them in the meantime,
* although we could add these pages to a global reuse list for
* shrink_active_list to pick up before reclaiming other pages. */

--
Gleb.

#5 lore raw
Quoted 6 lines · On Wed, 9 May 2012 11:59:17 +0300 Gleb Natapov <[email protected]> wrote:

Hmm, yes if it is file backed it may work. Setting up qemu to use file backed memory is one more complication while running the test though. I haven't checked by I am not sure that MADV_DONTNEED will drop page immediately though. It probably puts it on some list to be freed later. Hmm actually looking at the comments it seems like this is what happens:

Quoted 10 lines

/*
* Application no longer needs these pages. If the pages are dirty,
* it's OK to just throw them away. The app will be more careful about
* data it wants to keep. Be sure to free swap resources too. The
* zap_page_range call sets things up for shrink_active_list to actually
* free
* these pages later if no one else has touched them in the meantime,
* although we could add these pages to a global reuse list for
* shrink_active_list to pick up before reclaiming other pages. */

zap_page_range() actually frees these pages, no?

Virtio balloon seems to rely on this.

Thanks,
Takuya

#6 lore raw
Quoted 20 lines · Takuya Yoshikawa
Quoted 6 lines · On Wed, 9 May 2012 11:59:17 +0300 Gleb Natapov <[email protected]> wrote:

Hmm, yes if it is file backed it may work. Setting up qemu to use file backed memory is one more complication while running the test though. I haven't checked by I am not sure that MADV_DONTNEED will drop page immediately though. It probably puts it on some list to be freed later. Hmm actually looking at the comments it seems like this is what happens:

Quoted 10 lines

/*
* Application no longer needs these pages. If the pages are dirty,
* it's OK to just throw them away. The app will be more careful about
* data it wants to keep. Be sure to free swap resources too. The
* zap_page_range call sets things up for shrink_active_list to actually
* free
* these pages later if no one else has touched them in the meantime,
* although we could add these pages to a global reuse list for
* shrink_active_list to pick up before reclaiming other pages. */

Quoted 4 lines

zap_page_range() actually frees these pages, no?

Virtio balloon seems to rely on this.

The pages are removed from the user address space. But if they're not anonymous, the pages still live in the page cache.

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw
Quoted 7 lines · On Wed, 09 May 2012 16:20:23 +0300 Avi Kivity <[email protected]> wrote:
Quoted 4 lines

zap_page_range() actually frees these pages, no?

Virtio balloon seems to rely on this.

The pages are removed from the user address space. But if they're not anonymous, the pages still live in the page cache.

Ah, about non-anonymous/file-backed case, I see.

Thanks,
Takuya