From nobody Thu Dec 18 05:23:44 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CC24186E2F for ; Sat, 11 Jan 2025 00:30:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555427; cv=none; b=Nry+far1MqckG+DusMAIHm2D5A5zuIkelAxdoaLOhk+WxmFsXjlpCSxLEQ92vf4YmITOBsEeAjm+dv6d8SDE87rJ+OtxDl/d4JbYxBvg/UNDW4GnsjW2dbv9brDh2EZol1bC8NFZWWUW95q8MxzqJSrMG1icc8jPnX4d8F+JIhA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555427; c=relaxed/simple; bh=dvr0z46QuRPjeO+M3P6fjhyLCMuwGFDzgudUy3batWo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gSLGJucfL/mAWDGVajTwkmOcuR0NyczVOzP0I86B1yRLxuixGKpV1jKb2Gu0y+pfvuAP+onxN6j3sVLHSL7m1Ycb7inUROLGxcGr5ebZDkU4oIVSr17f3wDQi5N5zNqUWKmS/dkf2FAdQ2nJ0i6ridfi0SOX8eT7VLPDqeMDNRA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PnhHesvV; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PnhHesvV" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2166f9f52fbso79045245ad.2 for ; Fri, 10 Jan 2025 16:30:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555426; x=1737160226; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=RNK3sklc8NzNdhQ6DzodSTUjKP3FfBCZUP6CSMXfdw4=; b=PnhHesvV7HGdsBG6mqlTRZga9oRwXuNxcxm7/r2Crxv5JOPODNfCWf08fiRzONJKRM gBA0Wp6eJh4OvziUhwtRFwLe0JH360IZNTok+LBJNOYK/w/K+6kEIuxhSVgqinHNMXAH /C3WkFflewq7U6S6GXElYoQRx9ExeiFW98yPoIkHQp1nRHLcO3qjalBx9SR63XIfOZst /A7Ejp50g/VfN8Nas5xjCw631ie36RPUFrhVbDbUz1TqPGucqxBPd78GcMUZPqNMRKfr 6xawDejeyCh7nkxUxptZe1comBAH84kOkJ/caPwBRdW9h9U8S5iTyz4Dq6103TV+SYPD 0OXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555426; x=1737160226; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=RNK3sklc8NzNdhQ6DzodSTUjKP3FfBCZUP6CSMXfdw4=; b=k0m1F48igM/pFcPJbcloA4MRXYCZqccCIA2mlKa2vRupdNRFUp+/lUUkqqjiTaWRiX 0Kf3CtBU1Yn3iJSe0tR/16sxrQ7RE8ihoUhhZW5mpQpiozQLgJ6LT2WuLffY+BOELexR Sgib/ayaHI813AX8CPnsa0lGCxU7pXhzOfA6eKMxKfPcc4rMZE3mz6hsHYaEHw/DTzug VqAigYWcMIi6gGV1l5lKOYof1lhcXuolR42/ccicEao1MUdguHA2YFHeJj0tGwvhc9Kg bYStD9Rj1Ed8L8AGIgZL1uHGtaLu12DOjMoXFZnxkzYjcRwa/cw8ZAGOLKs25TL2f/lM OMSw== X-Forwarded-Encrypted: i=1; AJvYcCWsmmPi+hpehCxjKGNVo0PQEjMNbREnAyvIki9qSl77Qva36kRjyc+MiuOBkhyR+uJEntXUOx9+cF4hGXk=@vger.kernel.org X-Gm-Message-State: AOJu0Yxnv+7C7sG0pFoPL/G6/wVe2Zmh6FhRy77eP6tXmRGMiEEr1T95 sIQBHBrAKe43fPiQUxrDessPjA5nrL/Dm1wmhj9lUIfbEUZXAABZQLBPNvmmw8TN7K+m2tOdq3E 9QQ== X-Google-Smtp-Source: AGHT+IFZVo7ZKv5IRkv1HXxsi6LOVpEl2/GjPDFzkEZRkFVeRBFtcXWmAC3wo8WF/6xryaFkvYLsgqOGng8= X-Received: from plks12.prod.google.com ([2002:a17:903:2cc:b0:211:fb3b:763b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c941:b0:215:a179:14ca with SMTP id d9443c01a7336-21a83f3eec9mr194424425ad.2.1736555425724; Fri, 10 Jan 2025 16:30:25 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:54 -0800 In-Reply-To: <20250111003004.1235645-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111003004.1235645-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111003004.1235645-11-seanjc@google.com> Subject: [PATCH v2 10/20] KVM: selftests: Keep dirty_log_test vCPU in guest until it needs to stop From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu , Maxim Levitsky , Sean Christopherson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the dirty_log_test guest code, exit to userspace only when the vCPU is explicitly told to stop. Periodically exiting just to check if a flag has been set is unnecessary, weirdly complex, and wastes time handling exits that could be used to dirty memory. Opportunistically convert 'i' to a uint64_t to guard against the unlikely scenario that guest_num_pages exceeds the storage of an int. Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 43 ++++++++++---------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 8d31e275a23d..40c8f5551c8e 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -31,9 +31,6 @@ /* Default guest test virtual memory offset */ #define DEFAULT_GUEST_TEST_MEM 0xc0000000 =20 -/* How many pages to dirty for each guest loop */ -#define TEST_PAGES_PER_LOOP 1024 - /* How many host loops to run (one KVM_GET_DIRTY_LOG for each loop) */ #define TEST_HOST_LOOP_N 32UL =20 @@ -75,6 +72,7 @@ static uint64_t host_page_size; static uint64_t guest_page_size; static uint64_t guest_num_pages; static uint64_t iteration; +static bool vcpu_stop; =20 /* * Guest physical memory offset of the testing memory slot. @@ -96,9 +94,10 @@ static uint64_t guest_test_virt_mem =3D DEFAULT_GUEST_TE= ST_MEM; static void guest_code(void) { uint64_t addr; - int i; =20 #ifdef __s390x__ + uint64_t i; + /* * On s390x, all pages of a 1M segment are initially marked as dirty * when a page of the segment is written to for the very first time. @@ -112,7 +111,7 @@ static void guest_code(void) #endif =20 while (true) { - for (i =3D 0; i < TEST_PAGES_PER_LOOP; i++) { + while (!READ_ONCE(vcpu_stop)) { addr =3D guest_test_virt_mem; addr +=3D (guest_random_u64(&guest_rng) % guest_num_pages) * guest_page_size; @@ -140,14 +139,7 @@ static uint64_t host_track_next_count; /* Whether dirty ring reset is requested, or finished */ static sem_t sem_vcpu_stop; static sem_t sem_vcpu_cont; -/* - * This is only set by main thread, and only cleared by vcpu thread. It is - * used to request vcpu thread to stop at the next GUEST_SYNC, since GUEST= _SYNC - * is the only place that we'll guarantee both "dirty bit" and "dirty data" - * will match. E.g., SIG_IPI won't guarantee that if the vcpu is interrup= ted - * after setting dirty bit but before the data is written. - */ -static atomic_t vcpu_sync_stop_requested; + /* * This is updated by the vcpu thread to tell the host whether it's a * ring-full event. It should only be read until a sem_wait() of @@ -272,9 +264,7 @@ static void clear_log_collect_dirty_pages(struct kvm_vc= pu *vcpu, int slot, /* Should only be called after a GUEST_SYNC */ static void vcpu_handle_sync_stop(void) { - if (atomic_read(&vcpu_sync_stop_requested)) { - /* It means main thread is sleeping waiting */ - atomic_set(&vcpu_sync_stop_requested, false); + if (READ_ONCE(vcpu_stop)) { sem_post(&sem_vcpu_stop); sem_wait(&sem_vcpu_cont); } @@ -801,11 +791,24 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) } =20 /* - * See vcpu_sync_stop_requested definition for details on why - * we need to stop vcpu when verify data. + * Stop the vCPU prior to collecting and verifying the dirty + * log. If the vCPU is allowed to run during collection, then + * pages that are written during this iteration may be missed, + * i.e. collected in the next iteration. And if the vCPU is + * writing memory during verification, pages that this thread + * sees as clean may be written with this iteration's value. */ - atomic_set(&vcpu_sync_stop_requested, true); + WRITE_ONCE(vcpu_stop, true); + sync_global_to_guest(vm, vcpu_stop); sem_wait(&sem_vcpu_stop); + + /* + * Clear vcpu_stop after the vCPU thread has acknowledge the + * stop request and is waiting, i.e. is definitely not running! + */ + WRITE_ONCE(vcpu_stop, false); + sync_global_to_guest(vm, vcpu_stop); + /* * NOTE: for dirty ring, it's possible that we didn't stop at * GUEST_SYNC but instead we stopped because ring is full; @@ -813,8 +816,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) * the flush of the last page, and since we handle the last * page specially verification will succeed anyway. */ - assert(host_log_mode =3D=3D LOG_MODE_DIRTY_RING || - atomic_read(&vcpu_sync_stop_requested) =3D=3D false); vm_dirty_log_verify(mode, bmap); =20 /* --=20 2.47.1.613.gc27f4b7a9f-goog