From nobody Wed Aug 26 14:37:50 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3F88F40927C; Wed, 8 Jul 2026 15:22:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783524170; cv=none; b=hYBaFWrzmTyGRC27pXCdc/kNXp3uf63czxhWGOIlF/tc2MxcqnvoVJSncqQFWArkITvpmZzxEit/bJKuDYnwQ3S7Rj8Ny2gpC/04ud/gAwiefhKJ56IlZNqcf6BB0AQtvIOf6Hf9taq6oDRmkPO4gV8E9hEXnZrkD+2J1gJC53o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783524170; c=relaxed/simple; bh=0L5YaCv2QfsD5henuVNR5RUPbcyRWh6keGowsx1OoQ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Aa6Vd3BwsO37jMgcvtGsFC0OW/I2ZhRPicaSNyOscxK2Cc9Yiq6kqVoVBmyou7PRm2OVgt6cGDAFrbaexsJIJl9i5Gz+R2ThhQIBJxYPcInk0QhudD13UJAOibzSNsVY6YtwIRXT6HjBvY+oKVXVyxvHbixLUrYFu7TspsrSm5k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=GsfMciL0; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="GsfMciL0" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 39F101CDD; Wed, 8 Jul 2026 08:22:43 -0700 (PDT) Received: from LeoBrasDK.cambridge.arm.com (LeoBrasDK.cambridge.arm.com [10.2.212.21]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 139593F66F; Wed, 8 Jul 2026 08:22:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1783524167; bh=0L5YaCv2QfsD5henuVNR5RUPbcyRWh6keGowsx1OoQ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GsfMciL0pvMWsMjYvoQ7mx3J9RGFf1CjdhIopt5M/X+JS9uSWBav9lJN5kSJLWHhi JeTszs6zVskMLMKWATzpxsBYCMz5S1BvBYd1dU57TZAKf/5IjXv5WniAXQ07FOPUlY 3FbB1wbM1duaijNmBztoOrTTO5s2u0n482BfzmQw= From: Leonardo Bras To: Paolo Bonzini , Shuah Khan , Sean Christopherson , David Matlack , Leonardo Bras , Ackerley Tng , Marc Zyngier , Oliver Upton , Wu Fei , Claudio Imbrenda , Steffen Eiden Cc: kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3 3/3] KVM: selftests: dirty_log_perf_test: Add dirty-ring support Date: Wed, 8 Jul 2026 16:22:28 +0100 Message-ID: <20260708152232.2568907-4-leo.bras@arm.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260708152232.2568907-1-leo.bras@arm.com> References: <20260708152232.2568907-1-leo.bras@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=11164; i=leo.bras@arm.com; h=from:subject; bh=0L5YaCv2QfsD5henuVNR5RUPbcyRWh6keGowsx1OoQ0=; b=owGbwMvMwCX2pizjszvTwvWMp9WSGLL8spU+v/B+1LVH0qlmtcQE2WWTDG4W7LdbPG1GVvdeo WsCXokHO0pZGMS4GGTFFFlkH81fxfN9SsaRKz8WwMxhZQIZwsDFKQAT+RXNyLBht9qtX/b7Td/L Wyw503M5ziYjNXNJBltgXK7TnPPXGysZGZ7Ot/hSuukpzyG3CBXO3v/MrG8KjtwXOfHv4v5nRz3 N+rkB X-Developer-Key: i=leo.bras@arm.com; a=openpgp; fpr=36E6C95AE0F111CC5B6F4D2E688C33F8A0C5B0C5 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" dirty_log_test supports both dirty-bitmap and dirty-ring as dirty-page tracking mechanisms, while dirty_log_perf_test only supports dirty-bitmap. Add support to dirty-ring on dirty_log_perf_test so it can be used to compare performance between changes in the mechanism. Signed-off-by: Leonardo Bras --- .../selftests/kvm/dirty_log_perf_test.c | 107 ++++++++++++++++-- 1 file changed, 98 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/test= ing/selftests/kvm/dirty_log_perf_test.c index 69b38791440e..181f521c9be9 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -6,61 +6,122 @@ * * Copyright (C) 2018, Red Hat, Inc. * Copyright (C) 2020, Google, Inc. */ =20 #include #include #include #include #include +#include =20 #include "kvm_util.h" #include "test_util.h" #include "memstress.h" #include "guest_modes.h" #include "ucall_common.h" =20 /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each l= oop)*/ #define TEST_HOST_LOOP_N 2UL =20 static int nr_vcpus =3D 1; static u64 guest_percpu_mem_size =3D DEFAULT_PER_VCPU_MEM_SIZE; static bool run_vcpus_while_disabling_dirty_logging; =20 /* Host variables */ static u64 dirty_log_manual_caps; +static u32 dirty_ring_size; static bool host_quit; static int iteration; static int vcpu_last_completed_iteration[KVM_MAX_VCPUS]; +static struct timespec vcpu_dirty_ring_collect[KVM_MAX_VCPUS]; + +static void dirty_ring_collect(struct kvm_vcpu *vcpu, u32 *ring_idx, + struct timespec *ts) +{ + static pthread_mutex_t collect =3D PTHREAD_MUTEX_INITIALIZER; + struct timespec start; + struct kvm_dirty_gfn *dirty_gfns =3D vcpu_map_dirty_ring(vcpu); + u32 idx =3D *ring_idx; + u32 ring_size =3D vcpu->vm->dirty_ring_size / sizeof(struct kvm_dirty_gfn= ); + int cleared, count; + + pthread_mutex_lock(&collect); + + clock_gettime(CLOCK_MONOTONIC, &start); + + while (true) { + struct kvm_dirty_gfn *cur; + + cur =3D &dirty_gfns[idx % ring_size]; + if (smp_load_acquire(&cur->flags) !=3D KVM_DIRTY_GFN_F_DIRTY) + break; + + smp_store_release(&cur->flags, KVM_DIRTY_GFN_F_RESET); + idx++; + } + + count =3D idx - *ring_idx; + *ring_idx =3D idx; + + cleared =3D kvm_vm_reset_dirty_ring(vcpu->vm); + + /* Cleared pages should be the same as collected, as KVM is supposed to + * clear only the entries that have been harvested, and a single vcpu will + * harvest at time. + */ + TEST_ASSERT(cleared =3D=3D count, "Reset dirty pages (%u) mismatch " + "with collected (%u)", cleared, count); + + *ts =3D timespec_add(*ts, timespec_elapsed(start)); + + pthread_mutex_unlock(&collect); +} =20 static void vcpu_worker(struct memstress_vcpu_args *vcpu_args) { struct kvm_vcpu *vcpu =3D vcpu_args->vcpu; int vcpu_idx =3D vcpu_args->vcpu_idx; u64 pages_count =3D 0; struct kvm_run *run; struct timespec start; struct timespec ts_diff; struct timespec total =3D (struct timespec){0}; struct timespec avg; + bool use_dirty_ring =3D !!vcpu->vm->dirty_ring_size; + u32 ring_idx =3D 0; int ret; =20 run =3D vcpu->run; =20 while (!READ_ONCE(host_quit)) { int current_iteration =3D READ_ONCE(iteration); + struct timespec collect =3D (struct timespec){0}; =20 clock_gettime(CLOCK_MONOTONIC, &start); - ret =3D _vcpu_run(vcpu); + + do { + ret =3D _vcpu_run(vcpu); + if (!use_dirty_ring) + break; + + dirty_ring_collect(vcpu, &ring_idx, &collect); + } while (run->exit_reason =3D=3D KVM_EXIT_DIRTY_RING_FULL); + ts_diff =3D timespec_elapsed(start); =20 + if (use_dirty_ring) { + ts_diff =3D timespec_sub(ts_diff, collect); + vcpu_dirty_ring_collect[vcpu_idx] =3D collect; + } + TEST_ASSERT(ret =3D=3D 0, "vcpu_run failed: %d", ret); TEST_ASSERT(get_ucall(vcpu, NULL) =3D=3D UCALL_SYNC, "Invalid guest sync status: exit_reason=3D%s", exit_reason_str(run->exit_reason)); =20 pr_debug("Got sync event from vCPU %d\n", vcpu_idx); vcpu_last_completed_iteration[vcpu_idx] =3D current_iteration; pr_debug("vCPU %d updated last completed iteration to %d\n", vcpu_idx, vcpu_last_completed_iteration[vcpu_idx]); =20 @@ -112,42 +173,45 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) struct timespec start; struct timespec ts_diff; struct timespec get_dirty_log_total =3D (struct timespec){0}; struct timespec vcpu_dirty_total =3D (struct timespec){0}; struct timespec avg; struct timespec clear_dirty_log_total =3D (struct timespec){0}; int i; =20 vm =3D memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, p->slots, p->backing_src, - p->partition_vcpu_memory_access, 0); + p->partition_vcpu_memory_access, + dirty_ring_size); =20 memstress_set_write_percent(vm, p->write_percent); =20 guest_num_pages =3D (nr_vcpus * guest_percpu_mem_size) >> vm->page_shift; guest_num_pages =3D vm_adjust_num_guest_pages(mode, guest_num_pages); host_num_pages =3D vm_num_host_pages(mode, guest_num_pages); pages_per_slot =3D host_num_pages / p->slots; =20 bitmaps =3D memstress_alloc_bitmaps(p->slots, pages_per_slot); =20 if (dirty_log_manual_caps) vm_enable_cap(vm, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2, dirty_log_manual_caps); =20 /* Start the iterations */ iteration =3D 0; host_quit =3D false; =20 clock_gettime(CLOCK_MONOTONIC, &start); - for (i =3D 0; i < nr_vcpus; i++) + for (i =3D 0; i < nr_vcpus; i++) { vcpu_last_completed_iteration[i] =3D -1; + vcpu_dirty_ring_collect[i] =3D (struct timespec){0}; + } =20 /* * Use 100% writes during the population phase to ensure all * memory is actually populated and not just mapped to the zero * page. The prevents expensive copy-on-write faults from * occurring during the dirty memory iterations below, which * would pollute the performance results. */ memstress_set_write_percent(vm, 100); memstress_set_random_access(vm, false); @@ -188,20 +252,35 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) while (READ_ONCE(vcpu_last_completed_iteration[i]) !=3D iteration) ; } =20 ts_diff =3D timespec_elapsed(start); vcpu_dirty_total =3D timespec_add(vcpu_dirty_total, ts_diff); pr_info("Iteration %d dirty memory time: %ld.%.9lds\n", iteration, ts_diff.tv_sec, ts_diff.tv_nsec); =20 + if (dirty_ring_size) { + struct timespec iteration_sum =3D (struct timespec){0}; + + for (i =3D 0; i < nr_vcpus; i++) + iteration_sum =3D timespec_add(iteration_sum, + vcpu_dirty_ring_collect[i]); + + pr_info("Iteration %d clear dirty ring time: %ld.%.9lds\n", + iteration, iteration_sum.tv_sec, iteration_sum.tv_nsec); + + clear_dirty_log_total =3D timespec_add(clear_dirty_log_total, + iteration_sum); + continue; + } + clock_gettime(CLOCK_MONOTONIC, &start); memstress_get_dirty_log(vm, bitmaps, p->slots); ts_diff =3D timespec_elapsed(start); get_dirty_log_total =3D timespec_add(get_dirty_log_total, ts_diff); pr_info("Iteration %d get dirty log time: %ld.%.9lds\n", iteration, ts_diff.tv_sec, ts_diff.tv_nsec); =20 if (dirty_log_manual_caps) { clock_gettime(CLOCK_MONOTONIC, &start); @@ -231,46 +310,51 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) ts_diff.tv_sec, ts_diff.tv_nsec); =20 /* * Tell the vCPU threads to quit. No need to manually check that vCPUs * have stopped running after disabling dirty logging, the join will * wait for them to exit. */ host_quit =3D true; memstress_join_vcpu_threads(nr_vcpus); =20 - avg =3D timespec_div(get_dirty_log_total, p->iterations); - pr_info("Get dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9l= ds/iteration)\n", - p->iterations, get_dirty_log_total.tv_sec, - get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); + if (!dirty_ring_size) { + avg =3D timespec_div(get_dirty_log_total, p->iterations); + pr_info("Get dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9= lds/iteration)\n", + p->iterations, get_dirty_log_total.tv_sec, + get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); + } =20 - if (dirty_log_manual_caps) { + if (dirty_log_manual_caps || dirty_ring_size) { avg =3D timespec_div(clear_dirty_log_total, p->iterations); pr_info("Clear dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%= .9lds/iteration)\n", p->iterations, clear_dirty_log_total.tv_sec, clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); } =20 memstress_free_bitmaps(bitmaps, p->slots); memstress_destroy_vm(vm); } =20 static void help(char *name) { puts(""); printf("usage: %s [-h] [-a] [-i iterations] [-p offset] [-g] " "[-m mode] [-n] [-b vcpu bytes] [-v vcpus] [-o] [-r random seed ] = [-s mem type]" "[-x memslots] [-w percentage] [-c physical cpus to run test on]\n= ", name); puts(""); printf(" -a: access memory randomly rather than in order.\n"); printf(" -i: specify iteration counts (default: %"PRIu64")\n", TEST_HOST_LOOP_N); + printf(" -d: specify the size of dirty-ring for tracking dirty pages.\n" + " If non-zero, will cause dirty-ring to be used instead of\n" + " dirty-bitmap. Must be a power of two.\n"); printf(" -g: Do not enable KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2. This\n" " makes KVM_GET_DIRTY_LOG clear the dirty log (i.e.\n" " KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE is not enabled)\n" " and writes will be tracked as soon as dirty logging is\n" " enabled on the memslot (i.e. KVM_DIRTY_LOG_INITIALLY_SET\n" " is not enabled).\n"); printf(" -p: specify guest physical test memory offset\n" " Warning: a low offset can conflict with the loaded test code= .\n"); guest_modes_help(); printf(" -n: Run the vCPUs in nested mode (L2)\n"); @@ -313,31 +397,36 @@ int main(int argc, char *argv[]) /* Override the seed to be deterministic by default. */ guest_random_seed =3D 1; =20 dirty_log_manual_caps =3D kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); dirty_log_manual_caps &=3D (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | KVM_DIRTY_LOG_INITIALLY_SET); =20 guest_modes_append_default(); =20 - while ((opt =3D getopt(argc, argv, "ab:c:eghi:m:nop:r:s:v:x:w:")) !=3D -1= ) { + while ((opt =3D getopt(argc, argv, "ab:c:d:eghi:m:nop:r:s:v:x:w:")) !=3D = -1) { switch (opt) { case 'a': p.random_access =3D true; break; case 'b': guest_percpu_mem_size =3D parse_size(optarg); break; case 'c': pcpu_list =3D optarg; break; + case 'd': + dirty_ring_size =3D parse_size(optarg); + dirty_ring_size *=3D sizeof(struct kvm_dirty_gfn); + dirty_log_manual_caps =3D 0; + break; case 'e': /* 'e' is for evil. */ run_vcpus_while_disabling_dirty_logging =3D true; break; case 'g': dirty_log_manual_caps =3D 0; break; case 'h': help(argv[0]); break; --=20 2.55.0