From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 159624A1D for ; Wed, 18 Dec 2024 00:01:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480074; cv=none; b=DUjgKui8zJmeDT4OC5BSiF/axngqu79/tJsxifWFKV2+I3LZh6hOM0E8iTR40ecL1zOKuEDVoqgvYvmR4ZpA/gxqUoPL3RJWWZaV9vIXg5bw6UtdMTOKl/9+Ea8xlyWVqqEEtb8a1vLMxjsW217oiHDvvLZepl5fBcpe8Kx1ofc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480074; c=relaxed/simple; bh=Y7B93kZ4ubuLJty/fkbTjImMyI1OknKK0109YgfX6N4=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=OOwWft1Tmmf1Fn3TF+SWp+Mu2UG9p1uf4IPjzyPlaSVJr04b/XxTZ7mcPo2A8JtucInhiMilN7ChfU0ZImUIz7mDgi/BjRTg4ZjrnvUFKELoLjM70Q8fhOHrgrZkK3ewogNuqKeZxkPaDJMSsTWywDVJToQDq4LAWK/ePzAqycA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Dkz43h48; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Dkz43h48" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734480072; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bo/RUCct0KIkYzDrZWYOaagFHcqmpMYIqMZsuD7r9TM=; b=Dkz43h48A9kcD2Mi+6Um6rccgFVF3PFsFj7sfYWOQ42Muuv8rkQX/6y1eedsI6tzymz822 dE0YWf0wekNNn4YQineeX1zGWcNB/Uv6h5PQTmD+Yh2RtI0qinU+KMMs8Ad5pNvBShbodG RhPOYxRqdy5wGX3ea9eq39QtvjZLvCU= Received: from mail-io1-f72.google.com (mail-io1-f72.google.com [209.85.166.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-486-qpnBfAFUMOqfKwb3Kyl0ZA-1; Tue, 17 Dec 2024 19:01:11 -0500 X-MC-Unique: qpnBfAFUMOqfKwb3Kyl0ZA-1 X-Mimecast-MFC-AGG-ID: qpnBfAFUMOqfKwb3Kyl0ZA Received: by mail-io1-f72.google.com with SMTP id ca18e2360f4ac-844db0decffso21413839f.0 for ; Tue, 17 Dec 2024 16:01:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734480070; x=1735084870; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=bo/RUCct0KIkYzDrZWYOaagFHcqmpMYIqMZsuD7r9TM=; b=r/wVr681pMKQOoHgQk3qTatEapglrIakEu3vcbd6fL2N3QMbcNrP4CRTEbuHDe2Ovy QLfNAXCi2RrQvqYomNLcFC7HlBoBepvoKFc8feQln17OGyICp2MmEAr9MYYIQ44z6Fdh skeS+tUw12z5CYS30ET9f+6JtQ96Z+s8kvWy0Jei3TAC2kIHS8zBi3TftFWF2FUq0TaY hR8PySNKpzHL6l2Lnkxppo0mZ+TnboXPHpsBmSW2f8FY6Z3vOfB54vKaF04HOAdWEYwK sE2SSAgABWXykIXuKuDj+SSAUpVLLpKUirDJmK5Ziw/pSXT7oYMS08gMGXkHwopHHI6d LsIQ== X-Forwarded-Encrypted: i=1; AJvYcCUBVDLd5CN4AaS1ijyj5RjHwiuFdCeC9iJDpQ4ohA12u2X5qNPR0wZZfrxQGwJxZAcT4Hn+SsltskKgOdI=@vger.kernel.org X-Gm-Message-State: AOJu0Yx/CDBbnUbO0oWnlwXkeSXYR/9pxAlaVKYu/0M9e9Zc1/whQXx9 RUQSSRgosBdsBG19pjFcLnOqbp/8ZdxcpHYRClDAnmpuU1iswRm+SCA9o/XB6IGOoCHMWpj5lMb rEfxoapps/8tTuES6CtX+vnYYC1RC/8QGySWSlPK/OKgbdroTUNKdRB1X64CT+A== X-Gm-Gg: ASbGncuCJn9HYBXbYGRU6VeX7qNebCWlkEdepQarfsUeXZnJ5IuvYmMrkloywEOfEDo LpWuvtSAZiilS6ctEt2cPbYmT3wwvAMHXH1Xw6q7BJoXeAOV/F8KGvQIU167KFDYODAXUhctz9B CR6MIuIT2sPJoMqeovRPkj/kwiH/lzoLtokf9H4Ub65ToZiGkpCAXLi5Aa7BgeTTdCbdQHum11s siDSFqSyeMOiJ3cnSwuuqNikcmgTnYPTff/lHHccqNaTptAs1gYSNr9 X-Received: by 2002:a05:6e02:1c81:b0:3a7:7bab:33f8 with SMTP id e9e14a558f8ab-3bb0ac12cbamr52858855ab.12.1734480070165; Tue, 17 Dec 2024 16:01:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwPxvtR0YHy3yL4zTEl4QvwE7PEyoVWytKRX324x9QG27BIsE935rwQFZVXy70I2ydwLMH2w== X-Received: by 2002:a05:6e02:1c81:b0:3a7:7bab:33f8 with SMTP id e9e14a558f8ab-3bb0ac12cbamr52858475ab.12.1734480069843; Tue, 17 Dec 2024 16:01:09 -0800 (PST) Received: from starship ([2607:fea8:fc01:8d8d:6adb:55ff:feaa:b156]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4e5e32a3360sm1933269173.106.2024.12.17.16.01.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 16:01:09 -0800 (PST) Message-ID: Subject: Re: [PATCH 10/20] KVM: selftests: Keep dirty_log_test vCPU in guest until it needs to stop From: Maxim Levitsky To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu Date: Tue, 17 Dec 2024 19:01:08 -0500 In-Reply-To: <20241214010721.2356923-11-seanjc@google.com> References: <20241214010721.2356923-1-seanjc@google.com> <20241214010721.2356923-11-seanjc@google.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 7bit On Fri, 2024-12-13 at 17:07 -0800, Sean Christopherson wrote: > In the dirty_log_test guest code, exit to userspace Once again, "exit to userspace" is misleading. > 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. > > 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/selftests/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 > > -/* 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 > > @@ -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; > > /* > * Guest physical memory offset of the testing memory slot. > @@ -96,9 +94,10 @@ static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM; > static void guest_code(void) > { > uint64_t addr; > - int i; > > #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 > > while (true) { > - for (i = 0; i < TEST_PAGES_PER_LOOP; i++) { > + while (!READ_ONCE(vcpu_stop)) { > addr = guest_test_virt_mem; > addr += (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 interrupted > - * 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_vcpu *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 *arg) > } > > /* > - * 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 == LOG_MODE_DIRTY_RING || > - atomic_read(&vcpu_sync_stop_requested) == false); > vm_dirty_log_verify(mode, bmap); > > /*