Currently dirty_log_test hardcodes usleep 1ms in each interval, which
could be too short for guest to write and fault in enough pages, then
there is less chance to test the write protection mechanism, especially
in the case of (log_mode != LOG_MODE_DIRTY_RING).

Unit is introduced to replace the default 1ms if specified in command
line. The following test can't trigger failure on my riscv vm:

  # ./dirty_log_test -m 21 -M clear-log

By enlarging unit, it fails every time:

  # ./dirty_log_test -u 100 -m 21 -M clear-log
  Random seed: 0x6b8b4567
  Setting log mode to: 'clear-log'
  Test iterations: 32, interval: 10, unit: 100 (ms)
  Testing guest mode: PA-bits:50,  VA-bits:48,  4K pages
  guest physical test memory offset: 0x3ffffbfffc000
  Iteration  1: dirty: 262147 clean: 0      writes: 8454
  ==== Test Assertion Failure ====
    dirty_log_test.c:565: val < iteration
    pid=295 tid=295 errno=0 - Success
  sh: addr2line: not found
    Clear page 197160 value (2) >= iteration (2) (last = 18446744073709551615, 
prev_last = 18446744073709551615)

Signed-off-by: Wu Fei <[email protected]>
---
 tools/testing/selftests/kvm/dirty_log_test.c | 24 ++++++++++++++------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/kvm/dirty_log_test.c 
b/tools/testing/selftests/kvm/dirty_log_test.c
index 12446a4b6e8d..06234a77f1c5 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -34,9 +34,12 @@
 /* How many host loops to run (one KVM_GET_DIRTY_LOG for each loop) */
 #define TEST_HOST_LOOP_N               32UL

-/* Interval for each host loop (ms) */
+/* Interval for each host loop (number of units) */
 #define TEST_HOST_LOOP_INTERVAL                10UL

+/* ms per unit, one iteration takes (interval * unit) ms */
+#define TEST_HOST_LOOP_UNIT            1UL
+
 /*
  * Ensure the vCPU is able to perform a reasonable number of writes in each
  * iteration to provide a lower bound on coverage.
@@ -592,6 +595,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, 
struct kvm_vcpu **vcpu,
 struct test_params {
        unsigned long iterations;
        unsigned long interval;
+       unsigned long unit;
        u64 phys_offset;
 };

@@ -718,7 +722,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                         * and so that the test doesn't run too far beyond the
                         * specified interval.
                         */
-                       usleep(1000);
+                       usleep(1000 * p->unit);

                        sync_global_from_guest(vm, nr_writes);

@@ -807,7 +811,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 static void help(char *name)
 {
        puts("");
-       printf("usage: %s [-h] [-i iterations] [-I interval] "
+       printf("usage: %s [-h] [-i iterations] [-I interval] [-u unit] "
               "[-p offset] [-m mode]\n", name);
        puts("");
        printf(" -c: hint to dirty ring size, in number of entries\n");
@@ -815,8 +819,9 @@ static void help(char *name)
               TEST_DIRTY_RING_COUNT);
        printf(" -i: specify iteration counts (default: %"PRIu64")\n",
               TEST_HOST_LOOP_N);
-       printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n",
+       printf(" -I: specify interval in unit (default: %"PRIu64")\n",
               TEST_HOST_LOOP_INTERVAL);
+       printf(" -u: specify unit in ms (default: 1)\n");
        printf(" -p: specify guest physical test memory offset\n"
               "     Warning: a low offset can conflict with the loaded test 
code.\n");
        printf(" -M: specify the host logging mode "
@@ -832,6 +837,7 @@ int main(int argc, char *argv[])
        struct test_params p = {
                .iterations = TEST_HOST_LOOP_N,
                .interval = TEST_HOST_LOOP_INTERVAL,
+               .unit = TEST_HOST_LOOP_UNIT,
        };
        int opt, i;

@@ -840,7 +846,7 @@ int main(int argc, char *argv[])

        guest_modes_append_default();

-       while ((opt = getopt(argc, argv, "c:hi:I:p:m:M:")) != -1) {
+       while ((opt = getopt(argc, argv, "c:hi:I:u:p:m:M:")) != -1) {
                switch (opt) {
                case 'c':
                        test_dirty_ring_count = strtol(optarg, NULL, 10);
@@ -851,6 +857,9 @@ int main(int argc, char *argv[])
                case 'I':
                        p.interval = strtol(optarg, NULL, 10);
                        break;
+               case 'u':
+                       p.unit = strtol(optarg, NULL, 10);
+                       break;
                case 'p':
                        p.phys_offset = strtoull(optarg, NULL, 0);
                        break;
@@ -886,9 +895,10 @@ int main(int argc, char *argv[])

        TEST_ASSERT(p.iterations > 0, "Iterations must be greater than zero");
        TEST_ASSERT(p.interval > 0, "Interval must be greater than zero");
+       TEST_ASSERT(p.unit > 0, "Unit must be greater than zero");

-       pr_info("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n",
-               p.iterations, p.interval);
+       pr_info("Test iterations: %"PRIu64", interval: %"PRIu64", unit: 
%"PRIu64" (ms)\n",
+               p.iterations, p.interval, p.unit);

        if (host_log_mode_option == LOG_MODE_ALL) {
                /* Run each log mode */
--
2.43.0

Reply via email to