public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Avi Kivity <avi@redhat.com>
To: Marcelo Tosatti <mtosatti@redhat.com>, kvm@vger.kernel.org
Subject: [PATCH kvm-unit-tests v2 14/14] Add dirty log test
Date: Wed, 15 Dec 2010 18:09:43 +0200	[thread overview]
Message-ID: <1292429383-15326-15-git-send-email-avi@redhat.com> (raw)
In-Reply-To: <1292429383-15326-1-git-send-email-avi@redhat.com>

This checks the failure that was fixed by kernel commit edde99ce0529
("KVM: Write protect memory after slot swap").  Two threads are used;
a guest thread continuously updates a shared variable, which is also
sampled by a host thread that also checks if dirty logging marked it
as dirty.

It detects about 5 million failures with the fix reverted, and 0 failures
with the fix applied.

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 api/dirty-log.cc      |   78 +++++++++++++++++++++++++++++++++++++++++++++++++
 config-x86-common.mak |    7 +++-
 2 files changed, 83 insertions(+), 2 deletions(-)
 create mode 100644 api/dirty-log.cc

diff --git a/api/dirty-log.cc b/api/dirty-log.cc
new file mode 100644
index 0000000..1e4ef9e
--- /dev/null
+++ b/api/dirty-log.cc
@@ -0,0 +1,78 @@
+#include "kvmxx.hh"
+#include "memmap.hh"
+#include "identity.hh"
+#include <boost/thread/thread.hpp>
+#include <stdlib.h>
+#include <stdio.h>
+
+namespace {
+
+void delay_loop(unsigned n)
+{
+    for (unsigned i = 0; i < n; ++i) {
+        asm volatile("pause");
+    }
+ }
+
+void write_mem(volatile bool& running, volatile int* shared_var)
+{
+    while (running) {
+        ++*shared_var;
+        delay_loop(1000);
+    }
+}
+
+void check_dirty_log(mem_slot& slot,
+                     volatile bool& running,
+                     volatile int* shared_var,
+                     int& nr_fail)
+{
+    uint64_t shared_var_gpa = reinterpret_cast<uint64_t>(shared_var);
+    slot.set_dirty_logging(true);
+    slot.update_dirty_log();
+    for (int i = 0; i < 10000000; ++i) {
+        int sample1 = *shared_var;
+        delay_loop(600);
+        int sample2 = *shared_var;
+        slot.update_dirty_log();
+        if (!slot.is_dirty(shared_var_gpa) && sample1 != sample2) {
+            ++nr_fail;
+        }
+    }
+    running = false;
+    slot.set_dirty_logging(false);
+}
+
+}
+
+using boost::ref;
+using std::tr1::bind;
+
+int main(int ac, char **av)
+{
+    kvm::system sys;
+    kvm::vm vm(sys);
+    mem_map memmap(vm);
+    void* logged_slot_virt;
+    posix_memalign(&logged_slot_virt, 4096, 4096);
+    int* shared_var = static_cast<int*>(logged_slot_virt);
+    identity::hole hole(logged_slot_virt, 4096);
+    identity::vm ident_vm(vm, memmap, hole);
+    kvm::vcpu vcpu(vm, 0);
+    bool running = true;
+    int nr_fail = 0;
+    mem_slot logged_slot(memmap,
+                         reinterpret_cast<uint64_t>(logged_slot_virt),
+                         4096, logged_slot_virt);
+    boost::thread host_poll_thread(check_dirty_log, ref(logged_slot),
+                                   ref(running),
+                                   ref(shared_var), ref(nr_fail));
+    identity::vcpu guest_write_thread(vcpu,
+                                      bind(write_mem,
+                                           ref(running),
+                                           ref(shared_var)));
+    vcpu.run();
+    host_poll_thread.join();
+    printf("Dirty bitmap failures: %d\n", nr_fail);
+    return nr_fail == 0 ? 0 : 1;
+}
diff --git a/config-x86-common.mak b/config-x86-common.mak
index ce36cde..b5c49f4 100644
--- a/config-x86-common.mak
+++ b/config-x86-common.mak
@@ -33,6 +33,7 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/kvmclock_test.flat
 
 tests-common += api/api-sample
+tests-common += api/dirty-log
 
 tests_and_config = $(TEST_DIR)/*.flat $(TEST_DIR)/unittests.cfg
 
@@ -85,10 +86,12 @@ arch_clean:
 
 api/%.o: CFLAGS += -m32
 
-api/%: LDLIBS += -lstdc++
+api/%: LDLIBS += -lstdc++ -lboost_thread-mt -lpthread
 api/%: LDFLAGS += -m32
 
 api/libapi.a: api/kvmxx.o api/identity.o api/exception.o api/memmap.o
 	$(AR) rcs $@ $^
 
-api/api-sample: api/api-sample.o api/libapi.a
\ No newline at end of file
+api/api-sample: api/api-sample.o api/libapi.a
+
+api/dirty-log: api/dirty-log.o api/libapi.a
-- 
1.7.1


  parent reply	other threads:[~2010-12-15 16:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-15 16:09 [PATCH kvm-unit-tests v2 00/14] API test framework Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 01/14] Makefile: add support for C++ Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 02/14] Improve autodepend includes Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 03/14] Add exception class for kernel errors (errno) Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 04/14] Add try_main() for running a program under an exception handler Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 05/14] Introduce a C++ wrapper for the kvm APIs Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 06/14] Add support for calling a function in guest mode Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 07/14] Add sample test using the api test harness Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 08/14] api: add support for KVM_SET_USER_MEMORY_REGION flags field Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 09/14] api: support KVM_GET_DIRTY_LOG ioctl Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 10/14] api: add memory map management Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 11/14] Build tests with debug information Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 12/14] api: Add support for creating an identity map with a hole Avi Kivity
2010-12-15 16:09 ` [PATCH kvm-unit-tests v2 13/14] Introduce libapi.a to avoid long Makefile recipes Avi Kivity
2010-12-15 16:09 ` Avi Kivity [this message]
2010-12-22 13:14 ` [PATCH kvm-unit-tests v2 00/14] API test framework Marcelo Tosatti

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1292429383-15326-15-git-send-email-avi@redhat.com \
    --to=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox