From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 B1BD1139D for ; Sat, 11 Jan 2025 00:30:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555413; cv=none; b=TWVp3jC+X1vGgrxg86KR9x7BWk3WY9F+ayZMyXtqPBwu4AruICDHCkhU44WzFhh5E0SShQI1aUL33hisXP7y++IEB33MFv7oHwSLUzQQh94zUhUUaatPBopKufIuIGG5RIgY2ycp7UxzA5b2KwPK1cSYis1ODLB2cxQn57nxVtg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555413; c=relaxed/simple; bh=cSnwUK/2rXGD+rrAuQ2cWaSYdAoHz1OuzoU7A8Ugyzw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mZvlSN/z5mCKscRcaVStjpKDNPhCL7530JrIjPWkF3OZB8r4+jkGg3MxxcWJQwRwXHa6qn4c05tzoRKsA/33mM+1ADRAeKT387tqQidssmVEo3v67weBrNzITv0qLz+cTrEH1lvsf9kJfUqroKCc4l43CXP523pHKnOYazOdbqQ= 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=fQ/mviTB; arc=none smtp.client-ip=209.85.216.73 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="fQ/mviTB" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef114d8346so4621233a91.0 for ; Fri, 10 Jan 2025 16:30:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555411; x=1737160211; 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=FDkeem6MqewIEXRWoJcJJSDt8+RewhtQwmeUHyoVvwQ=; b=fQ/mviTB+vc0gMRH8aZt7HJpEhBjxP3GqCHJtTA/E1bNwa61Gkum1Xkix5M0VRenYK fHIyoqb29IXLZzpUcxEUYU8rQwC/jdxsprtwRjJipQUibJoCLudZOPpBl4n4oaEaGXn9 LwBWZo6rp6kkHtgfEoAYoYwHmkasZjZ/sV2FO3PmDIylZH3xcqr0WJo21kVTandEMb2j 3RqOzUID1Hqg9SH3tK1DMaKKwW12nX7XZs00PFN6l74gA3pEln9aUTK8DPvElL1uRHpa YEidLcjS1I89kHXXn9Ekvy9gwNpwRAljkXwj3BOWOxF9oo5i+wxbpW4206e85y5aY6e+ ClZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555411; x=1737160211; 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=FDkeem6MqewIEXRWoJcJJSDt8+RewhtQwmeUHyoVvwQ=; b=fLqkyl0iI/K62ULLqicJx08WqQtCTH4BKR5S9JfBfSdp0dSdY+y+rawTzjuvqbMgqU jd1XK5FcNe3XxhVUoTefiq4lPzQWcEBd/ZMTnFDOOh6BjzLbZlzJR8zfQlQcS/LakFFB Ydra4DsQpw7OSMANfW3kzJyOBaP+P44WcPJDAFbNRThHH0Yn8vDtKQkX48CIwNrM2KOE w3F5CAcgENFe2TBVRMoB1aHsC3uM2ROuWOCPQvEA54EUlLLeTJ6yC48kZv97UZVDaxJp 4T3iAyDpCITTVAsjmtIYZWzhM5lm4AEBbTqDPeKvQ2xkNijHRDaAokkL786/sog2iJ3y ljWQ== X-Forwarded-Encrypted: i=1; AJvYcCUXnwK0tvhHGRxmwgDKyn9FwbrMq4vZXsmHlzDoutGbCm3/v60mbu0v2Q/aJ7u6BV1GFCzoQIXBSvQx5A4=@vger.kernel.org X-Gm-Message-State: AOJu0YxiyR53FptA+sPRm7bwjYoR/7dXaZVV2VCsbMagGSiLjbQFa3Z3 NxqZ270axrV8yriKLLCv3KlJmS+cCit4XJhgFLGSCgf22+lS8LEZHnVwA+5omQP1Sqn+rh0TBu4 G+A== X-Google-Smtp-Source: AGHT+IFl9e/CO6aIFQMtlWws0maKnM0IkLVkQ0R6MTG560dWJ3PE9b2Z/S4xdE96ZH1JQkGfgqN+ePxlgQQ= X-Received: from pjbqi8.prod.google.com ([2002:a17:90b:2748:b0:2d8:8d32:2ea3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5487:b0:2ee:f80c:688d with SMTP id 98e67ed59e1d1-2f5490bd537mr18660616a91.25.1736555411003; Fri, 10 Jan 2025 16:30:11 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:45 -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-2-seanjc@google.com> Subject: [PATCH v2 01/20] KVM: selftests: Support multiple write retires in dirty_log_test 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" From: Maxim Levitsky If dirty_log_test is run nested, it is possible for entries in the emulated PML log to appear before the actual memory write is committed to the RAM, due to the way KVM retries memory writes as a response to a MMU fault. In addition to that in some very rare cases retry can happen more than once, which will lead to the test failure because once the write is finally committed it may have a very outdated iteration value. Detect and avoid this case. Cc: Peter Xu Signed-off-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 52 +++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index aacf80f57439..cdae103314fc 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -152,6 +152,7 @@ static atomic_t vcpu_sync_stop_requested; * sem_vcpu_stop and before vcpu continues to run. */ static bool dirty_ring_vcpu_ring_full; + /* * This is only used for verifying the dirty pages. Dirty ring has a very * tricky case when the ring just got full, kvm will do userspace exit due= to @@ -166,7 +167,51 @@ static bool dirty_ring_vcpu_ring_full; * dirty gfn we've collected, so that if a mismatch of data found later in= the * verifying process, we let it pass. */ -static uint64_t dirty_ring_last_page; +static uint64_t dirty_ring_last_page =3D -1ULL; + +/* + * In addition to the above, it is possible (especially if this + * test is run nested) for the above scenario to repeat multiple times: + * + * The following can happen: + * + * - L1 vCPU: Memory write is logged to PML but not committed. + * + * - L1 test thread: Ignores the write because its last dirty ring entry + * Resets the dirty ring which: + * - Resets the A/D bits in EPT + * - Issues tlb flush (invept), which is intercepted b= y L0 + * + * - L0: frees the whole nested ept mmu root as the response to invept, + * and thus ensures that when memory write is retried, it will fault= again + * + * - L1 vCPU: Same memory write is logged to the PML but not commit= ted again. + * + * - L1 test thread: Ignores the write because its last dirty ring entry (= again) + * Resets the dirty ring which: + * - Resets the A/D bits in EPT (again) + * - Issues tlb flush (again) which is intercepted by = L0 + * + * ... + * + * N times + * + * - L1 vCPU: Memory write is logged in the PML and then committed. + * Lots of other memory writes are logged and committed. + * ... + * + * - L1 test thread: Sees the memory write along with other memory writes + * in the dirty ring, and since the write is usually not + * the last entry in the dirty-ring and has a very outda= ted + * iteration, the test fails. + * + * + * Note that this is only possible when the write was the last log entry + * write during iteration N-1, thus remember last iteration last log entry + * and also don't fail when it is reported in the next iteration, together= with + * an outdated iteration count. + */ +static uint64_t dirty_ring_prev_iteration_last_page; =20 enum log_mode_t { /* Only use KVM_GET_DIRTY_LOG for logging */ @@ -316,6 +361,8 @@ static uint32_t dirty_ring_collect_one(struct kvm_dirty= _gfn *dirty_gfns, struct kvm_dirty_gfn *cur; uint32_t count =3D 0; =20 + dirty_ring_prev_iteration_last_page =3D dirty_ring_last_page; + while (true) { cur =3D &dirty_gfns[*fetch_index % test_dirty_ring_count]; if (!dirty_gfn_is_dirtied(cur)) @@ -613,7 +660,8 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode= , unsigned long *bmap) */ min_iter =3D iteration - 1; continue; - } else if (page =3D=3D dirty_ring_last_page) { + } else if (page =3D=3D dirty_ring_last_page || + page =3D=3D dirty_ring_prev_iteration_last_page) { /* * Please refer to comments in * dirty_ring_last_page. --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 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 514DF4A1E for ; Sat, 11 Jan 2025 00:30:13 +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=1736555415; cv=none; b=pH7U6/+GrnA8w6t4+VXH6Bt1UJ+VdWl9iEgyM4NAGAC3527FEaPZdX/oVcXXC3y404F1mAE31lZsTyWA1zDdFk0KwBzSBBNpSXpYEsQUQxGk/O8WBGN+zeSeclf0sfDg70QhMBAKn3Z7gcOqInV9ktZAPv6dXKm/qt4o5SBrz+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555415; c=relaxed/simple; bh=kRFkQPfv0PSWUJLw1F058m2xO/VHwn24jbFbfVNRITs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=I4BeYuxAD1B8dodYth8RNHJmUBxQ81R2/hwmZO0m/vw4sohqrUx4yiU8OaFh5zElBl6mchvjb9bW7fN8MHIwbphe7ci9PrLHtpcYWDlJcjtjPfvc5hrrZmimM85HmGpuUErmMZQcERgrzRCbSdfZ/EYryrcdfj3Z5xyKtmLFR1o= 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=It68Rp+y; 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="It68Rp+y" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-216717543b7so65683285ad.0 for ; Fri, 10 Jan 2025 16:30:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555412; x=1737160212; 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=a/SsNBrmeOL+XTcAujo8LRjEscp+rd5UoAKZNA95NGM=; b=It68Rp+yDxu/7/kCZev2rO9STwcX/eyBzG8h1wBr5LUlJyveZCnfc+3yXrTRolMLzA BRMLXWamNICP1lGr1OEDX/iO29/RM2A9n1AX1BgxJy7agZxWEjmbzt+n9kcFGKpxlByS pSP3WqHy1TqmIFum1I0hbBjLfBbq0xBiV1Kx7ZMgMtihuqHbl5Tm/2lvWNZw4gt2gE96 p4+wsiaEC0eiSFfFFY/1Tpj9sbPu7Pkh4IkhSW/ASf02BV2sSXIK5P2VjmgFvqHP1QWF ZWLBDYz9D8OFTdYfMedm3CZ+DD8qpXF8uB+j1U2ONuowsCozxfIKE+vneWhUEqGa1lio tfRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555412; x=1737160212; 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=a/SsNBrmeOL+XTcAujo8LRjEscp+rd5UoAKZNA95NGM=; b=RB2XjejU+HZpQ7ueOL4jT1t31Nux8JvLgvSrBfMGkIOJTkAueH9Rz8mDpPrcOypzqu uUY6BuHrNY9kCv7fhIt9B3NIRjM+JHyzT1RfjczHAM/BkKqBspdSeVgf4/0Agnc8z4QF VE94LrMQwQQOTee0QtmSwPWqSjP8SG+PMmtyK8XbTxsopHfkqmxOHthPtGzT+8+A3kru nhwUElYjxP0oLKjdI3dL+zR5YPv+0wCDF0StjUQnArqQdFTw1iZP96svJyPASDuLxIzU ET2uXYKSGIvBT8k83IAd+fAoH2XunlY2JDxts3/Ej6unsxMlKdug/KaJRb1ya7nu6eaQ 7Fng== X-Forwarded-Encrypted: i=1; AJvYcCX6PrDMd47Hk72ZSyaXHnugm9q9E+KZ22AMXjhdmov2ZedPvhq5KIMwnlQwtYTzrakCvrenFia4VEdS68o=@vger.kernel.org X-Gm-Message-State: AOJu0YzHUigry2uAypKriQuL6Yl71MM5eBijndP0sPb+7RlIJ1jdQjwn mgQAIHQ7wmQ5nnSZnQozz4BGOITCUWPo4OV3QLFFEXMK0OlmawC8qD+yhnHQimvG25Lc0NMdQBa 3Dw== X-Google-Smtp-Source: AGHT+IER8rVpMyBVqwsOrLxxaVqakEwToEI3FXRKOmF6JTF9kogaGHv2F8pSmh3aP9HQjsY+vFg7meksq5s= X-Received: from pgwc7.prod.google.com ([2002:a65:66c7:0:b0:7fd:5164:d918]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f644:b0:215:b9a7:526d with SMTP id d9443c01a7336-21a83fa210cmr212852135ad.32.1736555412578; Fri, 10 Jan 2025 16:30:12 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:46 -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-3-seanjc@google.com> Subject: [PATCH v2 02/20] KVM: selftests: Sync dirty_log_test iteration to guest *before* resuming 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" Sync the new iteration to the guest prior to restarting the vCPU, otherwise it's possible for the vCPU to dirty memory for the next iteration using the current iteration's value. Note, because the guest can be interrupted between the vCPU's load of the iteration and its write to memory, it's still possible for the guest to store the previous iteration to memory as the previous iteration may be cached in a CPU register (which the test accounts for). Note #2, the test's current approach of collecting dirty entries *before* stopping the vCPU also results dirty memory having the previous iteration. E.g. if page is dirtied in the previous iteration, but not the current iteration, the verification phase will observe the previous iteration's value in memory. That wart will be remedied in the near future, at which point synchronizing the iteration before restarting the vCPU will guarantee the only way for verification to observe stale iterations is due to the CPU register caching case, or due to a dirty entry being collected before the store retires. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index cdae103314fc..41c158cf5444 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -859,9 +859,9 @@ static void run_test(enum vm_guest_mode mode, void *arg) */ if (++iteration =3D=3D p->iterations) WRITE_ONCE(host_quit, true); - - sem_post(&sem_vcpu_cont); sync_global_to_guest(vm, iteration); + + sem_post(&sem_vcpu_cont); } =20 pthread_join(vcpu_thread, NULL); --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 4B44B17C98 for ; Sat, 11 Jan 2025 00:30:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555418; cv=none; b=G5vK5UgNAXTHO1qVKssLjaKiETriA3+4YGolz7eJXCSDJWIqz9Nv72n/tYqrF02rUzTcF820GxvORhETN1aySQem+Ap+OgfBtPXjtCG6zxlkm3BL5WL1RhnzqIIltKC5i7UBFpM+WL7syFp/bB4GrtpqDVA66e7InOxUsd513EM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555418; c=relaxed/simple; bh=VpeIAx4HgBrBdjrulL38P4sjULv3zDVf3Ct1h85Wlb0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iXz1GSKSemv6lD/X4jz1e4yu0dTFlAQ0wVNXPN8gn0L09xVPzSFL4IlbeBLYAOroz4XPdjvVGXD20eEteKUp8ud8kn6MfpeiJV82+8ItQQrKOt5jvj9sgJ3pdv0mGAPbaDBODGFfomWMmvZ02HXuj2RCahsxtFSNdrZZBr6YSRw= 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=tz8G89RD; arc=none smtp.client-ip=209.85.216.74 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="tz8G89RD" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef728e36d5so4797836a91.3 for ; Fri, 10 Jan 2025 16:30:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555414; x=1737160214; 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=CWJNZezGcooW6XpZeQGEoIGegjFgZChrjwFhXc5Ge/w=; b=tz8G89RDGZ33CBYlpoNj0f5dUsKTYaW3dnwe5Mc3WcAMzZxVQcIHDFnHoLkE168ZDl H0nmGnBh1dBnPX80+D7WAxzsCnkljFj+U1QwDVUJfKMV3pz43SInuLQlajPFT1L22k1o wbisF7Y22dQEtbhj8RBqVAJo7XzopBQ4GpN04rH5cbIFOWYNV/l1glklt3F0O5E5o1jf Etg+omJEeu3bzd0txtgMtlvyxJh5cru1+ZYzs5X85NO2QjDue19nFNSd7Typdi+BG84E 1iT6WAXnIEfXIn6ygCvzUAmuyxIdTEADqJruky9OJqhgdPaXRS3zHEjSv1Cf1NamBesG 5tYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555414; x=1737160214; 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=CWJNZezGcooW6XpZeQGEoIGegjFgZChrjwFhXc5Ge/w=; b=fted/Qth2spK/sNi3oE2BW42IzCcFLkki1wcro/x8WqpMiSx3cujMhYVsimFZyJaZa UOamOaBoPyL3S7BTSWJ1k9FVbtf/3So5ovMkuPdCiKbywr6vwxKI/WPTyaqGkNIte+h4 0H24RjfNwv5nWoIuwKFBSKLDdPdcWNMicunJbVu9lplznKd4CaULEtBxMhkYC0NQ+0YV svhBqfmm2lmfxzdvewJvP5r6kwQFItq+AbsyHVu7eTHuLX3uj1hf2UJ4DBtPWtRB5u+X eKLE62KXSX14RZ4yfH8qnXbqJQwNyoeiB1/LbBF4HvSGAXfhETc1dFbE9DDhYBmroi+v jlKQ== X-Forwarded-Encrypted: i=1; AJvYcCVshrQz1XxIESTtKpf1GrbUZkaRItAT4YmX21KlVG5WIohG0vJgFHemWPgkZDt0pcylLInrbCEWbmVS1Og=@vger.kernel.org X-Gm-Message-State: AOJu0Yz4vsEs4fu/wnwQSpI4Vy2s8t7vzB/Y29GoDL5On4+GyIcWEk/f xpXOtZKt7yObnrNNjLGoeD9Q4Gs/AW78pj+BoCUUkSLf3eSmTTzokxkpBDv2NA3W6CD7JyjlONz BPw== X-Google-Smtp-Source: AGHT+IGbAKPfzmJX4632s4dXZRLNCWhesSvipTL7gmk5Cn1vyQsk7QDvUwvy1wSu/11M+W3IJ3hHD7SP1As= X-Received: from pfbgq35.prod.google.com ([2002:a05:6a00:3be3:b0:725:cd3b:326c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:398b:b0:727:d55e:4be3 with SMTP id d2e1a72fcca58-72d21f315c4mr17757050b3a.7.1736555414278; Fri, 10 Jan 2025 16:30:14 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:47 -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-4-seanjc@google.com> Subject: [PATCH v2 03/20] KVM: selftests: Drop signal/kick from dirty ring testcase 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" Drop the signal/kick from dirty_log_test's dirty ring handling, as kicking the vCPU adds marginal value, at the cost of adding significant complexity to the test. Asynchronously interrupting the vCPU isn't novel; unless the kernel is fully tickless, the vCPU will be interrupted by IRQs for any decently large interval. And exiting to userspace mode in the middle of a sequence isn't novel either, as the vCPU will do so every time the ring becomes full. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 106 +++---------------- 1 file changed, 15 insertions(+), 91 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 41c158cf5444..d9911e20337f 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -236,24 +236,6 @@ static enum log_mode_t host_log_mode; static pthread_t vcpu_thread; static uint32_t test_dirty_ring_count =3D TEST_DIRTY_RING_COUNT; =20 -static void vcpu_kick(void) -{ - pthread_kill(vcpu_thread, SIG_IPI); -} - -/* - * In our test we do signal tricks, let's use a better version of - * sem_wait to avoid signal interrupts - */ -static void sem_wait_until(sem_t *sem) -{ - int ret; - - do - ret =3D sem_wait(sem); - while (ret =3D=3D -1 && errno =3D=3D EINTR); -} - static bool clear_log_supported(void) { return kvm_has_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); @@ -292,17 +274,14 @@ static void vcpu_handle_sync_stop(void) /* It means main thread is sleeping waiting */ atomic_set(&vcpu_sync_stop_requested, false); sem_post(&sem_vcpu_stop); - sem_wait_until(&sem_vcpu_cont); + sem_wait(&sem_vcpu_cont); } } =20 -static void default_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err) +static void default_after_vcpu_run(struct kvm_vcpu *vcpu) { struct kvm_run *run =3D vcpu->run; =20 - TEST_ASSERT(ret =3D=3D 0 || (ret =3D=3D -1 && err =3D=3D EINTR), - "vcpu run failed: errno=3D%d", err); - TEST_ASSERT(get_ucall(vcpu, NULL) =3D=3D UCALL_SYNC, "Invalid guest sync status: exit_reason=3D%s", exit_reason_str(run->exit_reason)); @@ -371,7 +350,6 @@ static uint32_t dirty_ring_collect_one(struct kvm_dirty= _gfn *dirty_gfns, "%u !=3D %u", cur->slot, slot); TEST_ASSERT(cur->offset < num_pages, "Offset overflow: " "0x%llx >=3D 0x%x", cur->offset, num_pages); - //pr_info("fetch 0x%x page %llu\n", *fetch_index, cur->offset); __set_bit_le(cur->offset, bitmap); dirty_ring_last_page =3D cur->offset; dirty_gfn_set_collected(cur); @@ -382,13 +360,6 @@ static uint32_t dirty_ring_collect_one(struct kvm_dirt= y_gfn *dirty_gfns, return count; } =20 -static void dirty_ring_wait_vcpu(void) -{ - /* This makes sure that hardware PML cache flushed */ - vcpu_kick(); - sem_wait_until(&sem_vcpu_stop); -} - static void dirty_ring_continue_vcpu(void) { pr_info("Notifying vcpu to continue\n"); @@ -400,18 +371,6 @@ static void dirty_ring_collect_dirty_pages(struct kvm_= vcpu *vcpu, int slot, uint32_t *ring_buf_idx) { uint32_t count =3D 0, cleared; - bool continued_vcpu =3D false; - - dirty_ring_wait_vcpu(); - - if (!dirty_ring_vcpu_ring_full) { - /* - * This is not a ring-full event, it's safe to allow - * vcpu to continue - */ - dirty_ring_continue_vcpu(); - continued_vcpu =3D true; - } =20 /* Only have one vcpu */ count =3D dirty_ring_collect_one(vcpu_map_dirty_ring(vcpu), @@ -427,16 +386,13 @@ static void dirty_ring_collect_dirty_pages(struct kvm= _vcpu *vcpu, int slot, TEST_ASSERT(cleared =3D=3D count, "Reset dirty pages (%u) mismatch " "with collected (%u)", cleared, count); =20 - if (!continued_vcpu) { - TEST_ASSERT(dirty_ring_vcpu_ring_full, - "Didn't continue vcpu even without ring full"); + if (READ_ONCE(dirty_ring_vcpu_ring_full)) dirty_ring_continue_vcpu(); - } =20 pr_info("Iteration %ld collected %u pages\n", iteration, count); } =20 -static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int = err) +static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu) { struct kvm_run *run =3D vcpu->run; =20 @@ -444,17 +400,14 @@ static void dirty_ring_after_vcpu_run(struct kvm_vcpu= *vcpu, int ret, int err) if (get_ucall(vcpu, NULL) =3D=3D UCALL_SYNC) { /* We should allow this to continue */ ; - } else if (run->exit_reason =3D=3D KVM_EXIT_DIRTY_RING_FULL || - (ret =3D=3D -1 && err =3D=3D EINTR)) { + } else if (run->exit_reason =3D=3D KVM_EXIT_DIRTY_RING_FULL) { /* Update the flag first before pause */ - WRITE_ONCE(dirty_ring_vcpu_ring_full, - run->exit_reason =3D=3D KVM_EXIT_DIRTY_RING_FULL); + WRITE_ONCE(dirty_ring_vcpu_ring_full, true); sem_post(&sem_vcpu_stop); - pr_info("vcpu stops because %s...\n", - dirty_ring_vcpu_ring_full ? - "dirty ring is full" : "vcpu is kicked out"); - sem_wait_until(&sem_vcpu_cont); + pr_info("Dirty ring full, waiting for it to be collected\n"); + sem_wait(&sem_vcpu_cont); pr_info("vcpu continues now.\n"); + WRITE_ONCE(dirty_ring_vcpu_ring_full, false); } else { TEST_ASSERT(false, "Invalid guest sync status: " "exit_reason=3D%s", @@ -473,7 +426,7 @@ struct log_mode { void *bitmap, uint32_t num_pages, uint32_t *ring_buf_idx); /* Hook to call when after each vcpu run */ - void (*after_vcpu_run)(struct kvm_vcpu *vcpu, int ret, int err); + void (*after_vcpu_run)(struct kvm_vcpu *vcpu); } log_modes[LOG_MODE_NUM] =3D { { .name =3D "dirty-log", @@ -544,47 +497,24 @@ static void log_mode_collect_dirty_pages(struct kvm_v= cpu *vcpu, int slot, mode->collect_dirty_pages(vcpu, slot, bitmap, num_pages, ring_buf_idx); } =20 -static void log_mode_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int er= r) +static void log_mode_after_vcpu_run(struct kvm_vcpu *vcpu) { struct log_mode *mode =3D &log_modes[host_log_mode]; =20 if (mode->after_vcpu_run) - mode->after_vcpu_run(vcpu, ret, err); + mode->after_vcpu_run(vcpu); } =20 static void *vcpu_worker(void *data) { - int ret; struct kvm_vcpu *vcpu =3D data; uint64_t pages_count =3D 0; - struct kvm_signal_mask *sigmask =3D alloca(offsetof(struct kvm_signal_mas= k, sigset) - + sizeof(sigset_t)); - sigset_t *sigset =3D (sigset_t *) &sigmask->sigset; - - /* - * SIG_IPI is unblocked atomically while in KVM_RUN. It causes the - * ioctl to return with -EINTR, but it is still pending and we need - * to accept it with the sigwait. - */ - sigmask->len =3D 8; - pthread_sigmask(0, NULL, sigset); - sigdelset(sigset, SIG_IPI); - vcpu_ioctl(vcpu, KVM_SET_SIGNAL_MASK, sigmask); - - sigemptyset(sigset); - sigaddset(sigset, SIG_IPI); =20 while (!READ_ONCE(host_quit)) { - /* Clear any existing kick signals */ pages_count +=3D TEST_PAGES_PER_LOOP; /* Let the guest dirty the random pages */ - ret =3D __vcpu_run(vcpu); - if (ret =3D=3D -1 && errno =3D=3D EINTR) { - int sig =3D -1; - sigwait(sigset, &sig); - assert(sig =3D=3D SIG_IPI); - } - log_mode_after_vcpu_run(vcpu, ret, errno); + vcpu_run(vcpu); + log_mode_after_vcpu_run(vcpu); } =20 pr_info("Dirtied %"PRIu64" pages\n", pages_count); @@ -838,7 +768,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) * we need to stop vcpu when verify data. */ atomic_set(&vcpu_sync_stop_requested, true); - sem_wait_until(&sem_vcpu_stop); + sem_wait(&sem_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; @@ -905,7 +835,6 @@ int main(int argc, char *argv[]) .interval =3D TEST_HOST_LOOP_INTERVAL, }; int opt, i; - sigset_t sigset; =20 sem_init(&sem_vcpu_stop, 0, 0); sem_init(&sem_vcpu_cont, 0, 0); @@ -964,11 +893,6 @@ int main(int argc, char *argv[]) =20 srandom(time(0)); =20 - /* Ensure that vCPU threads start with SIG_IPI blocked. */ - sigemptyset(&sigset); - sigaddset(&sigset, SIG_IPI); - pthread_sigmask(SIG_BLOCK, &sigset, NULL); - if (host_log_mode_option =3D=3D LOG_MODE_ALL) { /* Run each log mode */ for (i =3D 0; i < LOG_MODE_NUM; i++) { --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 E27A854765 for ; Sat, 11 Jan 2025 00:30:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555419; cv=none; b=def/N+DjJUAIB8lS1CBcviAHlMKG/2exoXuq+HBBrFScKbmAPgQfc14L0x71aqcZSinN3vyO98bU4WBohJemKTETVl+xhG7WBE9p/HxASWSsakQyC5/FXlRpyh1FpQWLbiQXxZDw3lzqk+C3QyRU/SJt/kBGnN1RKz4NSU4rbq8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555419; c=relaxed/simple; bh=tjFdMjX6lyJjTAWqK8reLPzxs+8rAonFk59izniqoqg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GBHu2yHISuRhoDUL7jciH0KT85HTCVc2Ybd4i0xXQRGsHTvd41a8//TjWgRACwqO2OUAPi50I/j001eY3aH1lWkkD4mWKSuSGKIdn4jVyV4GoCnnAuwQQDvgPApeX351HKXHmtkIaGhyZut52U1TB5XyE19YvZ1dwz/Wr5Hn66U= 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=HpU5pn3a; arc=none smtp.client-ip=209.85.214.202 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="HpU5pn3a" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2166a1a5cc4so43094685ad.3 for ; Fri, 10 Jan 2025 16:30:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555416; x=1737160216; 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=os8abNvX6CFnZNFiWQWTmtZN80dTM1n/70vxr9R8AE8=; b=HpU5pn3aq3CgikCbov7EboM8CLqyopxxBWCSS4foGLz8QaxeAyN5TaXOL83UpEsfZV 4X4sb5Atskb3kI866/S8gWGPDCeu6C7pQ7y1OrTNPWphqABe/RK3aEjmWEw186vwJGt3 7PW12F/4yiV41ezADDvm4fK6wdnpsOpiZmTp7BL0EPdfEdpOsFUkSNI8R6JrWhHfVH1I CMaI4WT4aB7vbliAYSiv8/gdejfqu+RPipN4rXT7Zjqx58XPefsJbW7xSHp0GomKKIgS XIvSpTODYwJiH4q+1+slBrtmoTzgOwU3mxrRM5yncwO0TvhG7TbbMIX8+3jubsGQSB/2 SiJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555416; x=1737160216; 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=os8abNvX6CFnZNFiWQWTmtZN80dTM1n/70vxr9R8AE8=; b=LBvWHvweHOeea3Yf7DP90Jh+ZXZwIK79nbq31mXpeDaV94Axo+se8B68yFior6y9mb XnklLX0IdxFGNAHpkMipIFyEVLl89E6u/kzQfn/8UouMRcuSxHTkKKjYVnkQ6gC/AFKz Dj04ZlIi/gfrlirooBIW3TE7Tzor1nEnZNV9BtoNv2OkdwsNyU9md5h7o4nEdb4A9JlV nAcJECzYkH4PL6ehhGk8lo9SkJUU8sMo/Oyvuxo67Ft+gYsHp3TGa+G89MqCsJh+aLCL MOQAMVKBgTknpnxxx7tVFXRDaViqJeChEAeyd83v+1cA5/5phCwRg4UFFNSYUMAbEVHF bz5A== X-Forwarded-Encrypted: i=1; AJvYcCUMcYK7oiE2WGWHbuyDtiN1x03jniV1MzRgiXy9mH0m7NOYVEdC0B8FgSBt6F0ytnmTAdHBOlZmpjaWpEE=@vger.kernel.org X-Gm-Message-State: AOJu0YxyyzaeC5Ob+VNI7GdCtABGZprxXs9DTCoPvwhEorPqYK/9f6tb GJg464ndonGObtFwI9/iwN3g+Eb3ToR/68eg0xHwqtj9VgHiyZ4y2h+vDLchTiNrtL+ZFD0EmXH Ysg== X-Google-Smtp-Source: AGHT+IF9IWwdflYzR8M2xWZC7N/dYo31gRYsrweOMeidToMTdrclC2+399p202NcxYC9UauowCG/8v7eQkw= X-Received: from pgmm4.prod.google.com ([2002:a05:6a02:5504:b0:7fd:4497:f282]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3996:b0:1db:d738:f2ff with SMTP id adf61e73a8af0-1e88cf7f75dmr21580604637.2.1736555415847; Fri, 10 Jan 2025 16:30:15 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:48 -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-5-seanjc@google.com> Subject: [PATCH v2 04/20] KVM: selftests: Drop stale srandom() initialization from dirty_log_test 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" Drop an srandom() initialization that was leftover from the conversion to use selftests' guest_random_xxx() APIs. Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index d9911e20337f..55a744373c80 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -891,8 +891,6 @@ int main(int argc, char *argv[]) pr_info("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n", p.iterations, p.interval); =20 - srandom(time(0)); - if (host_log_mode_option =3D=3D LOG_MODE_ALL) { /* Run each log mode */ for (i =3D 0; i < LOG_MODE_NUM; i++) { --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 0BA8A7442F for ; Sat, 11 Jan 2025 00:30:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555419; cv=none; b=EMZ3uSY4ZrQKa0eqsZlmUGYArt7t10dJHy2VSyWkhrVhqWJ4wI4P6aGh0yzT/KM3mTIjr4w48qXBtsIKrjARULuDP4JolzgiwFNVhhyXMWSBCH+hMhcFxQrNfZGNWcN4XuljizoejqQOEywZSa0GuM35xbIENbHJuOHKlx7ZV0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555419; c=relaxed/simple; bh=ClODUFGrnkeK47VHzL8EGhn6t9shMEOwc3unT1VlQVE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nOy1PKIGQtALC5hdjBjBTIETmRBoyAUmcTHxgXVsGG/j8/7ZeLUlxkT/4eT2uM+/8iZS+nzUN5bggIcC2AtBNJiJn6OSJNfuIKBCV3uIz58BlDFWcJQqixWj4aez0Km70Advep0ldvOAMLua/vH4tegU+Wv1YDVyWkws5IW8lRI= 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=XjkINmoL; arc=none smtp.client-ip=209.85.216.74 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="XjkINmoL" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef9da03117so6688521a91.1 for ; Fri, 10 Jan 2025 16:30:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555417; x=1737160217; 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=lND6mfK1KolGbB6fWPNpX/ljO5ryDlTwA1yXgkJjBtY=; b=XjkINmoLRu+VePp0vMNMhwi1YJKZanuuo9OwUHrDvQk5kBN8PlYWy9jXMkwo1hmH2z SW0MbPVU3yyzvi+WHqHm09Coch9U9WNg8A5wGABYg14eNKIQubm7LXEHAFrNvREOxLyy H7wSCsAgfcT6OWGPh3E87AvF2itRAgRb7937TvIBNyI8XW/nypvC4RqhGb6lxkkYgDyZ CIyF5r4ljXupE1OcvOZ+nweB9yKyg0v5r+sXLIkPr9EAekeLmKeO1quoe6j8Uin4F7+T HWBQ/8pqKuIAc6WTP5KU39IwO6cj1hyLKH6WU9ZOUF7ojHxtzipsjdyFHkpQ2+UG2N+b W7iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555417; x=1737160217; 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=lND6mfK1KolGbB6fWPNpX/ljO5ryDlTwA1yXgkJjBtY=; b=p4m9QOJZm3A1RVepm8hcG9citfQBSmqyL6imoEw4bYKfarbBQXS7oPLYhljzEiA+Sq lBPmdNjzxBkuNRa1qY192WEZpb2AzIAA1VqqcmATX1VcxfGYpDyXH1iMo56WZuUREZE9 MSs8lrSvfC1l1QXm5/UaYb13/exKGCikHv4TU05xWa3yARuxUVRhC1YzoEWbhWl/huPj JnoIN4i9p9Iw2hhJuyhJBYSUt5LaAD3eESGX8F9r5DRRa//isyPV1VMbDMXqtDb9aIx6 FCbt0DlPKtq1Vpi8o6yzt51LrwgtDwCKEWUvMRODlisTssVD25AuQUGkpMJmS8jJdYnM eq8g== X-Forwarded-Encrypted: i=1; AJvYcCUhpm6zGA3fGnLHX4tJcNW0Cs6fGhn+Ke+D32Uol6dCCQhsFloD0KL+JFyaXCqqRRUy2e0SQ5xgXcB2bU4=@vger.kernel.org X-Gm-Message-State: AOJu0YxrepXPL6gZ/Lg9u33m6s1jQwqiSOnrrPIUKdbOfSVWO+EFStVC thfxFWWM77k9wpIsxu8NHGLc3lUggOHY8Qq8Bwz669Lhk66QBho8bzskkmIFcYvoFNxFPZSy6vO xBg== X-Google-Smtp-Source: AGHT+IHNg8o7uKSRq8i9WX+D0OybaQRuZsnIXdWPpm+yNQBkTw0cagzp8N5KhO48THrkFIPMDIb2kmggOrc= X-Received: from pjuw3.prod.google.com ([2002:a17:90a:d603:b0:2ea:9d23:79a0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:274b:b0:2ee:8427:4b02 with SMTP id 98e67ed59e1d1-2f548f5f941mr17976244a91.28.1736555417474; Fri, 10 Jan 2025 16:30:17 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:49 -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-6-seanjc@google.com> Subject: [PATCH v2 05/20] KVM: selftests: Precisely track number of dirty/clear pages for each iteration 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" Track and print the number of dirty and clear pages for each iteration. This provides parity between all log modes, and will allow collecting the dirty ring multiple times per iteration without spamming the console. Opportunistically drop the "Dirtied N pages" print, which is redundant and wrong. For the dirty ring testcase, the vCPU isn't guaranteed to complete a loop. And when the vCPU does complete a loot, there are no guarantees that it has *dirtied* that many pages; because the writes are to random address, the vCPU may have written the same page over and over, i.e. only dirtied one page. While the number of writes performed by the vCPU is also interesting, e.g. the pr_info() could be tweaked to use different verbiage, pages_count doesn't correctly track the number of writes either (because loops aren't guaranteed to a complete). Delete the print for now, as a future patch will precisely track the number of writes, at which point the verification phase can report the number of writes performed by each iteration. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 55a744373c80..08cbecd1a135 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -388,8 +388,6 @@ static void dirty_ring_collect_dirty_pages(struct kvm_v= cpu *vcpu, int slot, =20 if (READ_ONCE(dirty_ring_vcpu_ring_full)) dirty_ring_continue_vcpu(); - - pr_info("Iteration %ld collected %u pages\n", iteration, count); } =20 static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu) @@ -508,24 +506,20 @@ static void log_mode_after_vcpu_run(struct kvm_vcpu *= vcpu) static void *vcpu_worker(void *data) { struct kvm_vcpu *vcpu =3D data; - uint64_t pages_count =3D 0; =20 while (!READ_ONCE(host_quit)) { - pages_count +=3D TEST_PAGES_PER_LOOP; /* Let the guest dirty the random pages */ vcpu_run(vcpu); log_mode_after_vcpu_run(vcpu); } =20 - pr_info("Dirtied %"PRIu64" pages\n", pages_count); - return NULL; } =20 static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bm= ap) { + uint64_t page, nr_dirty_pages =3D 0, nr_clean_pages =3D 0; uint64_t step =3D vm_num_host_pages(mode, 1); - uint64_t page; uint64_t *value_ptr; uint64_t min_iter =3D 0; =20 @@ -544,7 +538,7 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode= , unsigned long *bmap) if (__test_and_clear_bit_le(page, bmap)) { bool matched; =20 - host_dirty_count++; + nr_dirty_pages++; =20 /* * If the bit is set, the value written onto @@ -605,7 +599,7 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode= , unsigned long *bmap) " incorrect (iteration=3D%"PRIu64")", page, *value_ptr, iteration); } else { - host_clear_count++; + nr_clean_pages++; /* * If cleared, the value written can be any * value smaller or equals to the iteration @@ -639,6 +633,12 @@ static void vm_dirty_log_verify(enum vm_guest_mode mod= e, unsigned long *bmap) } } } + + pr_info("Iteration %2ld: dirty: %-6lu clean: %-6lu\n", + iteration, nr_dirty_pages, nr_clean_pages); + + host_dirty_count +=3D nr_dirty_pages; + host_clear_count +=3D nr_clean_pages; } =20 static struct kvm_vm *create_vm(enum vm_guest_mode mode, struct kvm_vcpu *= *vcpu, --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 9E0BE13CFA6 for ; Sat, 11 Jan 2025 00:30:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555421; cv=none; b=d/yMiYrDBjCSbAAffKvT9XjJHb/ORwGLTrl6hHdjrkN1O16xlGcIgbnsAaNcbuJKwgY8Z2pGmcOlBswZ4VupFy5rkSpGxccjMgKofvihVLAL/gQK/p4b688/0AUzyKF2DKRV2jvuCVPo7qsthHEuWi4qij+yiPkksrfeoMCliHc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555421; c=relaxed/simple; bh=rXm6MMpaJw1qyS71V+xzDjJly4UwYlWkG0d7xvwWsQY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mgA1VbPw8H7fKudkm6cgKVqkdMNuzgt8zXpzUkO1tp6PXlg7APg61V6eUbl8xqCBcb6h2Qaaaa62h0U9QKBBXrPx1UtrmH6PoHXUWTOlILfj3c7+KAD+rf8GWX9mgRA8yY8zic+uKmSoC7BzTe1ubyV5mc38y0gTRmqWAzvai64= 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=QwBmfj0a; arc=none smtp.client-ip=209.85.216.73 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="QwBmfj0a" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ee5616e986so6611180a91.2 for ; Fri, 10 Jan 2025 16:30:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555419; x=1737160219; 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=6IhW12gqYntCz1+c2D5eBg8yPy5WGu0Pw6mNyLGrlAc=; b=QwBmfj0aX/nLbvgdBIxuKwpN3JomCHl7lT8hne1JEbhKbFG/LtH3wHityFXMvkA0zE 7ykVI/7KsstbTWD31dW1QlInBZMdZR0C4l7ENdgb1BQbm0ggPSd6D9qV5cA13vylbr1w Socwvgdzd4JERkV9vpHqMbY4qojF4GTszam29Ej6ym8peQw6ic2YTQhWT/909CPzZSg8 wz+M/ptizYuPOKQAKmEOHth/xQncpAiRCc+05nDoDj6S3ym6iOgSchH0mSwBiiPvpD46 dM6F9NAWwuAizQm59chGhiwocofoSFyCmjLKTd4JOy4oUscbecyHaMIKMh2J4bURNs+r F/SA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555419; x=1737160219; 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=6IhW12gqYntCz1+c2D5eBg8yPy5WGu0Pw6mNyLGrlAc=; b=fUhLf3nsH/WKa/6P5cWx8u80KxD/89yy1qcWZIvcgPAeW/PLbhZX9Z3HiLy6z6Opyq kJ7qM2ilydkf1MonL2gHbPaVm9LZvrxXmjDEg0rqq1Mz4MkzaE89kakHPMCFiapnv819 94gHuoyD/Ibep+xx6oxuniPCGQKJue+yAeoFbCOQlynDYYxTONIv60OrHYj/F394/8cF GHZdgFUoOAblKmtjIcjgZqj5ubnPKdEsA6MunlFM+x2WhUOu/ogptgzenwI4Frx+v6Cx QkUlC1VSD0oRjTULBTM9xroxEJY6zVu56A12CCU3BqZgXccT0AWPauYaFFsMMYAkdmIR TTIw== X-Forwarded-Encrypted: i=1; AJvYcCXqjV4gOqrL3UdTZTB//gYT1+xf84yip2B3cAuf11+H7udArvam9rNt7WinecS4gGVES05W6sJFBup4AFo=@vger.kernel.org X-Gm-Message-State: AOJu0YxBjFf1gDYPht0m6xrRNWz8g3YOE9Zg6r2NtbKd+3w2zJxxajre OpVGELj8L/J5RVOPptcJZ0jS3PWW/xY/1E1iQgQXh5ELaw7/dEjdXTMXM4nDieMHqxZipTiDkZ9 1sw== X-Google-Smtp-Source: AGHT+IGBV97wTYRM8hlFr4fJkGJVU7xqzfHB/Clf/Nbfpa7h/esWcwhR69zEh8pcrRr99qn6u0ZSD0nAMiI= X-Received: from pjwx3.prod.google.com ([2002:a17:90a:c2c3:b0:2ea:931d:7ced]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2d43:b0:2ee:fdf3:390d with SMTP id 98e67ed59e1d1-2f5490ed82dmr16491422a91.31.1736555418949; Fri, 10 Jan 2025 16:30:18 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:50 -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-7-seanjc@google.com> Subject: [PATCH v2 06/20] KVM: selftests: Read per-page value into local var when verifying dirty_log_test 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" Cache the page's value during verification in a local variable, re-reading from the pointer is ugly and error prone, e.g. allows for bugs like checking the pointer itself instead of the value. No functional change intended. Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 08cbecd1a135..5a04a7bd73e0 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -520,11 +520,10 @@ static void vm_dirty_log_verify(enum vm_guest_mode mo= de, unsigned long *bmap) { uint64_t page, nr_dirty_pages =3D 0, nr_clean_pages =3D 0; uint64_t step =3D vm_num_host_pages(mode, 1); - uint64_t *value_ptr; uint64_t min_iter =3D 0; =20 for (page =3D 0; page < host_num_pages; page +=3D step) { - value_ptr =3D host_test_mem + page * host_page_size; + uint64_t val =3D *(uint64_t *)(host_test_mem + page * host_page_size); =20 /* If this is a special page that we were tracking... */ if (__test_and_clear_bit_le(page, host_bmap_track)) { @@ -545,11 +544,10 @@ static void vm_dirty_log_verify(enum vm_guest_mode mo= de, unsigned long *bmap) * the corresponding page should be either the * previous iteration number or the current one. */ - matched =3D (*value_ptr =3D=3D iteration || - *value_ptr =3D=3D iteration - 1); + matched =3D (val =3D=3D iteration || val =3D=3D iteration - 1); =20 if (host_log_mode =3D=3D LOG_MODE_DIRTY_RING && !matched) { - if (*value_ptr =3D=3D iteration - 2 && min_iter <=3D iteration - 2) { + if (val =3D=3D iteration - 2 && min_iter <=3D iteration - 2) { /* * Short answer: this case is special * only for dirty ring test where the @@ -597,7 +595,7 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode= , unsigned long *bmap) TEST_ASSERT(matched, "Set page %"PRIu64" value %"PRIu64 " incorrect (iteration=3D%"PRIu64")", - page, *value_ptr, iteration); + page, val, iteration); } else { nr_clean_pages++; /* @@ -619,11 +617,11 @@ static void vm_dirty_log_verify(enum vm_guest_mode mo= de, unsigned long *bmap) * we'll see that page P is cleared, with * value "iteration-1". */ - TEST_ASSERT(*value_ptr <=3D iteration, + TEST_ASSERT(val <=3D iteration, "Clear page %"PRIu64" value %"PRIu64 " incorrect (iteration=3D%"PRIu64")", - page, *value_ptr, iteration); - if (*value_ptr =3D=3D iteration) { + page, val, iteration); + if (val =3D=3D iteration) { /* * This page is _just_ modified; it * should report its dirtyness in the --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 69B9514A4D1 for ; Sat, 11 Jan 2025 00:30:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555424; cv=none; b=Kwfg+Q8QJwV5xARRVZBC9p56D0kIx3x/XSZ++0/UNwTiUk1Rdkf5Je4kKODLP/3kNGcjVE7YMLUv3lrCPqk4f2sQ7zSwph5BHqf9/g4ODZ05UHwQXO8NA9D2VIbwbX+EhS/1FLRdntiCPnK6HqC9I5D2fzw1uTWs6xKH7sC1OiU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555424; c=relaxed/simple; bh=QGDCeJPchThIuDJkjyaSdj0tx8LKDXww3qbAv++T4dI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZV3uack66D9lGCASqNX+ZcKMFPAQDM6Er6uRDyMTbMPy4npKfkuSXBytus8MwrDt92VfDrIVmZvnPeZzXhEHd87mjq0PEY166bkNtSuekm2psqZBQnubtjPq9NWxSqsog1McFWc4nzZQP5aeiASZ/QAITUq2nSO98hWN0SLcB5s= 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=VrU+iic9; arc=none smtp.client-ip=209.85.216.74 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="VrU+iic9" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f550d28f7dso4597071a91.3 for ; Fri, 10 Jan 2025 16:30:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555420; x=1737160220; 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=dGMBXjHv8rojpdZoojt2m8NgO/lC7pQdQVMcdYyWG8c=; b=VrU+iic91IUAJqF0feegqsp7MK5gqiOe3P79sMVwCGhgtDkgcdhc/1xo8Z7ojZ3RXc h9QXREhw4nKwSz+kIRTbKiFQxmEN/7yMQf8IhnJorHq8EsYmyGGIs/R6XOqU1LSNB9Cl KcPVwB4tZ+lxUHQWBoLSjzJn1ZPcDLJL/DHrrs0Y0vSQrfMihtqr2149PFTq2PtnFn8y hkAHtJ2xhINW2iFZZB8VorLAIqkTqN4vVR8HK2fshSRJ+2NDh2EG7IIfFzBZcZLQyP49 Belh8CjeB2+5LrXSE9yr2YI4yhd2FpbkkLiPmxV91fQ41t728c+VtANdpdlHgRwhl1Q0 9N4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555420; x=1737160220; 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=dGMBXjHv8rojpdZoojt2m8NgO/lC7pQdQVMcdYyWG8c=; b=k+Qa55dZNgg+YyUQipZki7jX7wbMZMQafgwhqUkNKfRph73ixLERVV9H3UBMT+zPBv oUztVkr+ALrO6SJSJZqesFYjz0zozDw6XZ8VquAjRT7PNscipDq26ovpKICOCcN7C75i QUl6IVEGwt8OEKS4IgypOOifXx79zf6VtbRw43pe5xJujh0S7zY39P27WIw5FOdHb0FK LKEyvqPmhgue5J6fYM3qewgT32Z6+j4+riuBda0yzno6HjR7ksOddlAn2huQGzB100JQ cWER+43TQBpYRG38V8Bp9LsM++cbXMMHL7hL6Vmwzf2zUJMrE2PtI3vTBLss39llEest W+TQ== X-Forwarded-Encrypted: i=1; AJvYcCXCsQaYR0eIQu80LA/Zq0QS0RGuDPnd+aDidNg1euJ9wIYsmSRnn6Dc0i7foS/XNBV/h1BGsrv8HCiFcuI=@vger.kernel.org X-Gm-Message-State: AOJu0YzPEUwrG1m/Z4RJmOvKc6iKyQ9hMqm0nfaFYYVJaggZzyYz9/nr LgRVTKZ6Uv120F4gEn3x87efit8BnhqbeQ+B5w2l0r4VIdBG5A5/AnVrOCnG5lK8LtYYDJJWbXK t+A== X-Google-Smtp-Source: AGHT+IHVaZGEEdb4svmSOuWtVIDeYOOZZPZkNbz2XRwkXTumeQwS5L4YZBF1f95KtvQzctuCleUgMd0qvMo= X-Received: from pjbrs12.prod.google.com ([2002:a17:90b:2b8c:b0:2ea:6aa8:c4ad]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2e4b:b0:2f4:465d:5c94 with SMTP id 98e67ed59e1d1-2f548ebb621mr19044455a91.11.1736555420614; Fri, 10 Jan 2025 16:30:20 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:51 -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-8-seanjc@google.com> Subject: [PATCH v2 07/20] KVM: selftests: Continuously reap dirty ring while vCPU is running 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" Continue collecting entries from the dirty ring for the entire time the vCPU is running. Collecting exactly once all but guarantees the vCPU will encounter a "ring full" event and stop. While testing ring full is interesting, stopping and doing nothing is not, especially for larger intervals as the test effectively does nothing for a much longer time. To balance continuous collection with letting the guest make forward progress, chunk the interval waiting into 1ms loops (which also makes the math dead simple). To maintain coverage for "ring full", collect entries on subsequent iterations if and only if the ring has been filled at least once. I.e. let the ring fill up (if the interval allows), but after that contiuously empty it so that the vCPU can keep running. Opportunistically drop unnecessary zero-initialization of "count". Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 63 ++++++++++++++------ 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 5a04a7bd73e0..2aee047b8b1c 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -340,8 +340,6 @@ static uint32_t dirty_ring_collect_one(struct kvm_dirty= _gfn *dirty_gfns, struct kvm_dirty_gfn *cur; uint32_t count =3D 0; =20 - dirty_ring_prev_iteration_last_page =3D dirty_ring_last_page; - while (true) { cur =3D &dirty_gfns[*fetch_index % test_dirty_ring_count]; if (!dirty_gfn_is_dirtied(cur)) @@ -360,17 +358,11 @@ static uint32_t dirty_ring_collect_one(struct kvm_dir= ty_gfn *dirty_gfns, return count; } =20 -static void dirty_ring_continue_vcpu(void) -{ - pr_info("Notifying vcpu to continue\n"); - sem_post(&sem_vcpu_cont); -} - static void dirty_ring_collect_dirty_pages(struct kvm_vcpu *vcpu, int slot, void *bitmap, uint32_t num_pages, uint32_t *ring_buf_idx) { - uint32_t count =3D 0, cleared; + uint32_t count, cleared; =20 /* Only have one vcpu */ count =3D dirty_ring_collect_one(vcpu_map_dirty_ring(vcpu), @@ -385,9 +377,6 @@ static void dirty_ring_collect_dirty_pages(struct kvm_v= cpu *vcpu, int slot, */ TEST_ASSERT(cleared =3D=3D count, "Reset dirty pages (%u) mismatch " "with collected (%u)", cleared, count); - - if (READ_ONCE(dirty_ring_vcpu_ring_full)) - dirty_ring_continue_vcpu(); } =20 static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu) @@ -404,7 +393,6 @@ static void dirty_ring_after_vcpu_run(struct kvm_vcpu *= vcpu) sem_post(&sem_vcpu_stop); pr_info("Dirty ring full, waiting for it to be collected\n"); sem_wait(&sem_vcpu_cont); - pr_info("vcpu continues now.\n"); WRITE_ONCE(dirty_ring_vcpu_ring_full, false); } else { TEST_ASSERT(false, "Invalid guest sync status: " @@ -755,11 +743,52 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu); =20 while (iteration < p->iterations) { + bool saw_dirty_ring_full =3D false; + unsigned long i; + + dirty_ring_prev_iteration_last_page =3D dirty_ring_last_page; + /* Give the vcpu thread some time to dirty some pages */ - usleep(p->interval * 1000); - log_mode_collect_dirty_pages(vcpu, TEST_MEM_SLOT_INDEX, - bmap, host_num_pages, - &ring_buf_idx); + for (i =3D 0; i < p->interval; i++) { + usleep(1000); + + /* + * Reap dirty pages while the guest is running so that + * dirty ring full events are resolved, i.e. so that a + * larger interval doesn't always end up with a vCPU + * that's effectively blocked. Collecting while the + * guest is running also verifies KVM doesn't lose any + * state. + * + * For bitmap modes, KVM overwrites the entire bitmap, + * i.e. collecting the bitmaps is destructive. Collect + * the bitmap only on the first pass, otherwise this + * test would lose track of dirty pages. + */ + if (i && host_log_mode !=3D LOG_MODE_DIRTY_RING) + continue; + + /* + * For the dirty ring, empty the ring on subsequent + * passes only if the ring was filled at least once, + * to verify KVM's handling of a full ring (emptying + * the ring on every pass would make it unlikely the + * vCPU would ever fill the fing). + */ + if (READ_ONCE(dirty_ring_vcpu_ring_full)) + saw_dirty_ring_full =3D true; + if (i && !saw_dirty_ring_full) + continue; + + log_mode_collect_dirty_pages(vcpu, TEST_MEM_SLOT_INDEX, + bmap, host_num_pages, + &ring_buf_idx); + + if (READ_ONCE(dirty_ring_vcpu_ring_full)) { + pr_info("Dirty ring emptied, restarting vCPU\n"); + sem_post(&sem_vcpu_cont); + } + } =20 /* * See vcpu_sync_stop_requested definition for details on why --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 17C1E15C14B for ; Sat, 11 Jan 2025 00:30:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555424; cv=none; b=klPfbX30oPKsHfi5pbLbcqA+2vdFqhN1gAGgaDKiggxaiXRJHJvTrwV9UiJVF65MNicOp/gUq7y2j4TQZpgzBguo65Pw1dLxiEqftapjnyTZNJ9WhqHgTMdIsbAmokPAtaWHDCTcWRLKgIw7l2jM3Aod77RXQHAK9V//ifzHCZ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555424; c=relaxed/simple; bh=Inh2cv3fTNpaNX1dqJtMDaDtbEiwB1dIP0CX7Q96HQM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=luodEEG4TNVCBjyx7HUviR4FlssJcXNwb+7gMJj1iD2pY7nBx+DN5LDFMR9le6PMrH7S85znouJLGcCRl07VaghFVnke9ZLAX6BAHJ8T38Y+zGlEbf/vnp6ipF4OEljLIhYw39zSvYbfF3Gq6Y9bj2VkdIP5JzmZ6V7JHS7Ro5k= 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=1e8Odm9u; arc=none smtp.client-ip=209.85.216.74 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="1e8Odm9u" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee86953aeaso4599515a91.2 for ; Fri, 10 Jan 2025 16:30:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555422; x=1737160222; 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=m1k/pYfn2qosYE12LM1QCowrsBblB7GzrYsx0S4ECrU=; b=1e8Odm9uetRkAYbkeTWrXO6P3FPGtAUW8/5mie0MDxz7syerKqvcc8XA5ZlqFoPXAw xYDlB5E95U3gGZLJ31moU+6D939g0bf5Q3dnJQfoKf2tqeV8XJfGr6mZAYcymI/qPSic Sz7oDFgAVGC5yn9rSX+BGBs3nqEKpLkQ2DsCzItRhVYVfx/Mg/13TgccBG/13sPE3pb1 NCxv52r7LzuM35to+NtSByICQao3rUD95mm7e8WpufRy0wR7V4mmZfPik3IVZTECSXaH oknHQ12X0fuSGsZcS2nI9PQ/zv65Wb8gzcr6zr8BBL4pAClPlPQJ37w67vdR23xgA0mu 7/kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555422; x=1737160222; 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=m1k/pYfn2qosYE12LM1QCowrsBblB7GzrYsx0S4ECrU=; b=fqsB7lhEVG655pNeSnCvpy6nvK63hmHLqsi8TyBK86kITVPholB2soG+8KLEuI4jp5 E9BKGuQMq52kdkyHQfPJIGlMk3j2Ut0CCMA2Ss3btdvrmMBIApQA1nLorR+M+Pd5lKAI PnKXWgdRYRor6EtQq2R+L2VEydVEOJCGVeHtNVuW0hslj2dNhf8pwWkUo+itofild8cx XBDxPpiXwJ5+ORh+1s9JmjglxfyuwSJn//YpHTq0oxXU6wJ2jCMPExQjQcZBY8VES9+g nesJ+6xAtqQ0lNMyNjQTJRMclT3Ed61mr9pUsqTkKH6mPHrVeu/HG7tBXInKFcEia5Qy fevw== X-Forwarded-Encrypted: i=1; AJvYcCWf0zWbXHl1dwtS05x8pj3Xf7m2cMZ2i/kl912qkREx4oeAvtmN8Z9S02bkLlTKXrOwuKripRvTqP9f708=@vger.kernel.org X-Gm-Message-State: AOJu0YzG1GhmlTPTC99YxOn1VCgZeX/XysHrvve1SLm9ohXd+eKWeoTh 0utEFeCpIOGP5Q60RJ0/YGg7MvJtc/JMz8ynu8SxTfmxBl2pIGoui991ZPPIUYgfdzmqMTMYQzo q1Q== X-Google-Smtp-Source: AGHT+IF17Z0yrpB50va3qb6t7bXW3sn+V7nb0UF93emvB4u3L4QeaKeeehS2VrfQSdjxuguRho8Bqf6SeSw= X-Received: from pjx13.prod.google.com ([2002:a17:90b:568d:b0:2ee:4a90:3d06]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2dc8:b0:2ee:d024:e4f7 with SMTP id 98e67ed59e1d1-2f548db6c4dmr21639527a91.0.1736555422269; Fri, 10 Jan 2025 16:30:22 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:52 -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-9-seanjc@google.com> Subject: [PATCH v2 08/20] KVM: selftests: Limit dirty_log_test's s390x workaround to s390x 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" From: Maxim Levitsky s390 specific workaround causes the dirty-log mode of the test to dirty all guest memory on the first iteration, which is very slow when the test is run in a nested VM. Limit this workaround to s390x. Signed-off-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 2aee047b8b1c..55a385499434 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -98,6 +98,7 @@ static void guest_code(void) uint64_t addr; int i; =20 +#ifdef __s390x__ /* * 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. @@ -108,6 +109,7 @@ static void guest_code(void) addr =3D guest_test_virt_mem + i * guest_page_size; vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration)); } +#endif =20 while (true) { for (i =3D 0; i < TEST_PAGES_PER_LOOP; i++) { --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 8EB4F17A58F for ; Sat, 11 Jan 2025 00:30:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555426; cv=none; b=MZR04hbKRlEGHXfbOWnK6p4CsBRsBAnoqSpt9Z1LtKNwCWpPorrwV3ObxNUQKGjDQzVqCoYuGFCENevXynb31hlFgfYcNqjo30Iw762w1lHSu8J4m8iX4ZzPEm1k6x2iW7ySIQILLTuZ0KFe2SxxYlCTPWEfnNMtA4r+76OzAe4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555426; c=relaxed/simple; bh=OgTTk595VIyjaFheqBqRfYqIoPT5CZ9K1t127fuPGa4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RWRZaETR0F7VFVtr/k3yjnYEFf61bKIHUXDfPKq95y08hOhmFYGg7Q26/NMWDYwSMMKP0TfJ929dDr9yEfv8dv2jNtPZNrKD1eaYOH6hyMA3/4UICVrNNi9dq6oUJOZhOlrzacyGYlkgveETHN3GDNSzIheCzE+YSeGyv5Hu8CY= 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=z1MW3Wl9; arc=none smtp.client-ip=209.85.216.73 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="z1MW3Wl9" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2eeeb5b7022so4779826a91.0 for ; Fri, 10 Jan 2025 16:30:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555424; x=1737160224; 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=FAzPo3qg+GcWGQlv0+FYKxHsP1523EqIgag6kKt+kTY=; b=z1MW3Wl9m4tzjLCOvxwXc53Nhg1IQHTxndzi4SeB20dOLzfp+mJn2tOYVhw72/qQdC iBHl8nRONVc06VQCGHK+JO7GtfajXJ4kJGd6mtDA6IOgHBLAVqw6WPiwzVqGw4oyCIA8 bQR+EaiOGl/bYl3KgRhzzzxJSI4qWIhwSOO7jOr7K8GDuPnKpv1EVXJfSJMiJm0ixj6v lDl1YG8DQG3kqX2FUIqhL/e6h3j6MXCktYvD88sgINbR4HOJoBVTwJhzpzwcyxYHsRcm DmoZ0kpPZEu1cTgjsz35g2TqscM8ZevpnUv4zfdm8ec1cIkfiAH3Bt9nRTT8yxzC2BnK UWgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555424; x=1737160224; 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=FAzPo3qg+GcWGQlv0+FYKxHsP1523EqIgag6kKt+kTY=; b=N36KLdChnhIkEb1QF0sEerONxkk4JFh1kAlOPB06MJOLWv64rGXtAmH6DxRQtc3hsO wFLHtFi+PB9VNcWi2vxEK3XqJHuUE4dLHgtdaBzwENqs3rJZA/8l6U43s5kGyJMZvdCy aiUiS3uBWzKWux+TLxEHjk5IJVT052K6Dor4UxdG8aHZHuFb4vc33bukb3l0koByaFQU PkZjgXVTQA66qSWgBP2IfHwfKl6Yblkwd+AWSdASDJVQzVAfgOWJ2RehRIHJYT7hIJqr mb/nrGs3970JI2N94EU1uBpVefU1sFu8bvCAg0rle2DRNWzb/WyvHm0fZA1ncVfeCO8D 8eNg== X-Forwarded-Encrypted: i=1; AJvYcCWjuljbokFEeQJvbrxhF4QSs5k/AA3qrUVgfo8i2QJMeUz5wXjo06ff2N9N0aOJe/ui+gCfpsgj8fy42bU=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1DBnqmEdYZXdJsN1PJcecXAzEIxd2/fgn7eKs9W0GZpfJxF9N /hZkuN/9Rj0ipQftalC8TEyrWYd8cElON+TNzWHzDvuiMLMrr+74dUW2eeyxVA6ATBtFEsfvZse I0A== X-Google-Smtp-Source: AGHT+IFcZmNvkzAFZeqVU6HOmDlE0qdzEz5X0u8ak5y2AJn4YmqiFkSzOypX660zjNhdGKiOFPzhRq/DJvA= X-Received: from pjbee12.prod.google.com ([2002:a17:90a:fc4c:b0:2e5:5ffc:1c36]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:254e:b0:2ef:19d0:2261 with SMTP id 98e67ed59e1d1-2f548eae703mr20633108a91.16.1736555423972; Fri, 10 Jan 2025 16:30:23 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:53 -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-10-seanjc@google.com> Subject: [PATCH v2 09/20] KVM: selftests: Honor "stop" request in dirty ring test 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" Now that the vCPU doesn't dirty every page on the first iteration for architectures that support the dirty ring, honor vcpu_stop in the dirty ring's vCPU worker, i.e. stop when the main thread says "stop". This will allow plumbing vcpu_stop into the guest so that the vCPU doesn't need to periodically exit to userspace just to see if it should stop. Add a comment explaining that marking all pages as dirty is problematic for the dirty ring, as it results in the guest getting stuck on "ring full". This could be addressed by adding a GUEST_SYNC() in that initial loop, but it's not clear how that would interact with s390's behavior. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 55a385499434..8d31e275a23d 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -387,8 +387,7 @@ static void dirty_ring_after_vcpu_run(struct kvm_vcpu *= vcpu) =20 /* A ucall-sync or ring-full event is allowed */ if (get_ucall(vcpu, NULL) =3D=3D UCALL_SYNC) { - /* We should allow this to continue */ - ; + vcpu_handle_sync_stop(); } else if (run->exit_reason =3D=3D KVM_EXIT_DIRTY_RING_FULL) { /* Update the flag first before pause */ WRITE_ONCE(dirty_ring_vcpu_ring_full, true); @@ -697,6 +696,15 @@ static void run_test(enum vm_guest_mode mode, void *ar= g) #ifdef __s390x__ /* Align to 1M (segment size) */ guest_test_phys_mem =3D align_down(guest_test_phys_mem, 1 << 20); + + /* + * The workaround in guest_code() to write all pages prior to the first + * iteration isn't compatible with the dirty ring, as the dirty ring + * support relies on the vCPU to actually stop when vcpu_stop is set so + * that the vCPU doesn't hang waiting for the dirty ring to be emptied. + */ + TEST_ASSERT(host_log_mode !=3D LOG_MODE_DIRTY_RING, + "Test needs to be updated to support s390 dirty ring"); #endif =20 pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem= ); --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 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 From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 D2BBF188587 for ; Sat, 11 Jan 2025 00:30:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555429; cv=none; b=Gf5Rs/tkQtQyMc4kJ4DYMt0Z/4dtE/OylnwZYFre0vFXH12g8x+BwwCrlxoKBrc1WZmchrkLm8Xf++W9nY41K9Xtuu2zPCpDnNPAmls5JVYLv3ci95v9YaP6Havtqa+F+AgMwL4zZdEjY0xUuQ6kyUknIu8zAz8NNpsqMOB6gSE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555429; c=relaxed/simple; bh=3sfvgMqPiGr/4hRycKbPGFa+ObbRBz+P/dL5LYIATKQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ftNve/6/Br+pYglBxrUBqKTHRbI06EMrMRqKYa0KJZRRjoGs3dODeDc27aAjLPVzZQHqoRBM4LJ7CEJ6RQqpBgg2Wiydk85Pd+4m7RM28KwVLKWRoXieCkxl3F0mwe71maXoSm2fLXYQZgJ3M6gcc+iM68mEMaFRNi4dunDfNsg= 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=uoesLNpJ; arc=none smtp.client-ip=209.85.216.73 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="uoesLNpJ" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef79d9c692so6716539a91.0 for ; Fri, 10 Jan 2025 16:30:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555427; x=1737160227; 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=YB7ROV39Pzlxg3CsT+e1YkqRcv/fedX8ByOvnUPuqfA=; b=uoesLNpJBY02fzzlEihFEK2onjDwkXkLxXBh23o5DIxnTbN7X7s69U/MXIy/K1zU2Z BbFNuW7eSaA8GJu/O8I+LQAopz6WLf5Lyk/0GzAzgeRU+sqKz7KLRBBSgb6LkZQ+2dqE lTIffx9m0IYEsYFHf1UdzUvFJu4BXIx1KK+Y4Row8lysTKrPFaZofvCVt4yYP9ncK2m6 a63B5D8l4IWNUpCJotDMvSbO/K/eV4gTCBe4hxx3j7i1ee/992z7q30upgQ+3W29ztDE m9fp1MMwWTuzm/2fRSAAVNUz3eIyf3LVhZC0AIDRwWgS5XH8yoivGiOrwSfJFIdC+hsl EdJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555427; x=1737160227; 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=YB7ROV39Pzlxg3CsT+e1YkqRcv/fedX8ByOvnUPuqfA=; b=V4o8EofJsrgeuiFDHzFmX1WOQIAWjpizy83Q5z11Fbq5tPB76SlUM6RyhNFPq4pq2W 78jcEkVNAOJDLeLWPeTHQ8GVY4iiciVXUKlAt8YrX+ct4teUF3Y37vgK2PPcpU/Drjyu jXC/JDT0QkZGNi70HlASnudyyRNN/RfexWtN4Jjlpakv+/Z6zlOfAnTNYtYoIzPGoKII j4OWbAD3LtYNLtCgxM/Wbd88UXp19pIuHSezdEaw5aFAeIAoiV7vfCafI//jFmYeUSZC z8sG/JKjuH6wZFFt4MIQE4P4cSbH+yAfskUTiwsiRQrlg3a6bOolclX447yo6LUccJQ9 AKIg== X-Forwarded-Encrypted: i=1; AJvYcCXYyg6x8Z4fipxITvvSx8SGTi8uWhEioE5kPsObG5pNPEOUmFDJH26+TyVPKrG3K7fIIjC5Jg0pSOLZ5g0=@vger.kernel.org X-Gm-Message-State: AOJu0Yxk1fjKLYfzVSM/xwDeKuf2Kv55h3iZypRLPNBHH/acyYlYjV3S ADgu2n3cVCFiQsisGDc+uzVIfKsvH9GAxLDGIez/99tFXl9ZeBDl/r7dnOC3d6PUEZTgdmFS3Cf mUg== X-Google-Smtp-Source: AGHT+IF9PJIrfTvS9nj0PyfnuXky377OG3qzpuoymD5lbk2+qu0GEx0KxT0XjTWhVsB8BPQIq2rSf68CP6U= X-Received: from pjbqb10.prod.google.com ([2002:a17:90b:280a:b0:2ef:6fb0:55fb]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2dc6:b0:2ee:53b3:3f1c with SMTP id 98e67ed59e1d1-2f548e9f9ecmr17738470a91.5.1736555427225; Fri, 10 Jan 2025 16:30:27 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:55 -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-12-seanjc@google.com> Subject: [PATCH v2 11/20] KVM: selftests: Post to sem_vcpu_stop if and only if vcpu_stop is true 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" When running dirty_log_test using the dirty ring, post to sem_vcpu_stop only when the main thread has explicitly requested that the vCPU stop. Synchronizing the vCPU and main thread whenever the dirty ring happens to be full is unnecessary, as KVM's ABI is to actively prevent the vCPU from running until the ring is no longer full. I.e. attempting to run the vCPU will simply result in KVM_EXIT_DIRTY_RING_FULL without ever entering the guest. And if KVM doesn't exit, e.g. let's the vCPU dirty more pages, then that's a KVM bug worth finding. Posting to sem_vcpu_stop on ring full also makes it difficult to get the test logic right, e.g. it's easy to let the vCPU keep running when it shouldn't, as a ring full can essentially happen at any given time. Opportunistically rework the handling of dirty_ring_vcpu_ring_full to leave it set for the remainder of the iteration in order to simplify the surrounding logic. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 40c8f5551c8e..8544e8425f9c 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -379,12 +379,8 @@ static void dirty_ring_after_vcpu_run(struct kvm_vcpu = *vcpu) if (get_ucall(vcpu, NULL) =3D=3D UCALL_SYNC) { vcpu_handle_sync_stop(); } else if (run->exit_reason =3D=3D KVM_EXIT_DIRTY_RING_FULL) { - /* Update the flag first before pause */ WRITE_ONCE(dirty_ring_vcpu_ring_full, true); - sem_post(&sem_vcpu_stop); - pr_info("Dirty ring full, waiting for it to be collected\n"); - sem_wait(&sem_vcpu_cont); - WRITE_ONCE(dirty_ring_vcpu_ring_full, false); + vcpu_handle_sync_stop(); } else { TEST_ASSERT(false, "Invalid guest sync status: " "exit_reason=3D%s", @@ -743,7 +739,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu); =20 while (iteration < p->iterations) { - bool saw_dirty_ring_full =3D false; unsigned long i; =20 dirty_ring_prev_iteration_last_page =3D dirty_ring_last_page; @@ -775,19 +770,12 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) * the ring on every pass would make it unlikely the * vCPU would ever fill the fing). */ - if (READ_ONCE(dirty_ring_vcpu_ring_full)) - saw_dirty_ring_full =3D true; - if (i && !saw_dirty_ring_full) + if (i && !READ_ONCE(dirty_ring_vcpu_ring_full)) continue; =20 log_mode_collect_dirty_pages(vcpu, TEST_MEM_SLOT_INDEX, bmap, host_num_pages, &ring_buf_idx); - - if (READ_ONCE(dirty_ring_vcpu_ring_full)) { - pr_info("Dirty ring emptied, restarting vCPU\n"); - sem_post(&sem_vcpu_cont); - } } =20 /* @@ -829,6 +817,8 @@ static void run_test(enum vm_guest_mode mode, void *arg) WRITE_ONCE(host_quit, true); sync_global_to_guest(vm, iteration); =20 + WRITE_ONCE(dirty_ring_vcpu_ring_full, false); + sem_post(&sem_vcpu_cont); } =20 --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 585AC18C903 for ; Sat, 11 Jan 2025 00:30:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555430; cv=none; b=Si0HxeJfMqrirMyHTKyFaMziq8OkMM65VfK1tZMbEf0R5HSRVdRZuS0dSMRN/mPhnHEgHYo8Bn8ostfAW98UdpPMR6In4FLP6v61FZyXoOHn0oElS9uHrL23JBcKVyo5Zv4ZUQFqOcpN5ddL3I0bnpZ+6PF+pGsFzogbDJU8HSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555430; c=relaxed/simple; bh=Ct3FuonCRgDJp/Ui+bQ/TVeX0BiSNd/ih2pMmElS7V4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gXZYzWl4AbbLHzsDaNIFWH+4C1kOenJl20f+vyCia/JRLgBNCcnkMfR3BPTHO32b1KsZ2odawBdpl6nhB40sWjLgnLgjfIv9uKB/i6vt5OYHw9pbPHY7Dg8POiPOhneZKuV+UY+toc5OYOZ/Atirskn1QW/Ozk7Ar8ZAyL6stjo= 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=yPbSM8Ej; arc=none smtp.client-ip=209.85.214.202 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="yPbSM8Ej" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2164861e1feso46072935ad.1 for ; Fri, 10 Jan 2025 16:30:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555429; x=1737160229; 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=y05jSkqCIR81O47S4buDh42roqUgkkPVJYIkUmCWrzY=; b=yPbSM8EjFFKFftkcwWXAmBItUdHbdwNF3ybnL5WhdGG2/71N/SLxYy2exNlx5B29SR goHvdSknV74JdavsCnAVtnxquX77kvOBqgY/cXM8NEDNh+tkorKYI/tmpI5fOKkDgbc9 cG7HDahKJDLC1i6whYU1HZFq+VU8+yVEGjJZCmAiGyQN/AAHSlgH3IOyKlVaYiITIGT8 uhNdRqJOHSxW/KedGxbDoDK0jZKLiuYDkIHNrVWBbFVAS+LUdb771U8q0el6ZOdeuiPC aAVfoKWVyyUbAgRN4Et4VIuqq4x3Qk3FNAR1w47DS9v1yCasEXUzwi5UEWipGSee/zK0 YxVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555429; x=1737160229; 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=y05jSkqCIR81O47S4buDh42roqUgkkPVJYIkUmCWrzY=; b=QLNPQTfd3tYE99LYG2Jof8I5EkFkxgEekxLKEM08TqGHYTAqh7JQu8mL2fU8vclFoN 1MGh2oCtrJX4lk3QrGMgGc7uaEAuC3JRtpRHyToQFGxO911Tvuf93t7h0+G7wAwG5/BV 6f939tbeCaPGNWRyz3TnERKYUp3i9P3RDgJ41Jndk1JsnMDYf7JF89mjdQ5/vpl249fY Wgr+WOSD7r7ZZT23GyeKVEqEbEoqnd9aH+ruZ6/vKBye5/2oQB34VvsI9VjqW6hQ+S6h XlZXH8K22b8/jIKPcYm+/xWR/aiiqhD3+Nqr+vIUlxb1SOb7nVUIhtO2pygMXYLIPstv XI0g== X-Forwarded-Encrypted: i=1; AJvYcCUhJtwBv2G+kWz5upg5zOPsbXACbME55SyMvC8j1xW40VsTGHirnfM86OnZy6vguOT8yq74Vx5B3V/YDbA=@vger.kernel.org X-Gm-Message-State: AOJu0YwG7dslSbSEr7ZLmqVtIERg/wC/8LQ7WbCps1tbJJqlXA4uifW9 ZtHsJapNADic/w1Gpi2JaeY3sdLge0cyZO4edZlQGVoI8nsJqRkPXcKm4lUGTXBl8tlLoFdPV6J qlA== X-Google-Smtp-Source: AGHT+IFgrnZzIKEMTRy6t4DPba2tanrxDHSa0a90bgDXxnDvHGsIE64G7RceEr5QZnECylO0tNxGXGphF04= X-Received: from pfbfm22.prod.google.com ([2002:a05:6a00:2f96:b0:725:c96b:b1db]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:c887:b0:1e0:dc7b:4ee9 with SMTP id adf61e73a8af0-1e88cf7ba69mr21582007637.8.1736555428699; Fri, 10 Jan 2025 16:30:28 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:56 -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-13-seanjc@google.com> Subject: [PATCH v2 12/20] KVM: selftests: Use continue to handle all "pass" scenarios in dirty_log_test 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" When verifying pages in dirty_log_test, immediately continue on all "pass" scenarios to make the logic consistent in how it handles pass vs. fail. No functional change intended. Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 8544e8425f9c..d7cf1840bd80 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -510,8 +510,6 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode= , unsigned long *bmap) } =20 if (__test_and_clear_bit_le(page, bmap)) { - bool matched; - nr_dirty_pages++; =20 /* @@ -519,9 +517,10 @@ static void vm_dirty_log_verify(enum vm_guest_mode mod= e, unsigned long *bmap) * the corresponding page should be either the * previous iteration number or the current one. */ - matched =3D (val =3D=3D iteration || val =3D=3D iteration - 1); + if (val =3D=3D iteration || val =3D=3D iteration - 1) + continue; =20 - if (host_log_mode =3D=3D LOG_MODE_DIRTY_RING && !matched) { + if (host_log_mode =3D=3D LOG_MODE_DIRTY_RING) { if (val =3D=3D iteration - 2 && min_iter <=3D iteration - 2) { /* * Short answer: this case is special @@ -567,10 +566,8 @@ static void vm_dirty_log_verify(enum vm_guest_mode mod= e, unsigned long *bmap) } } =20 - TEST_ASSERT(matched, - "Set page %"PRIu64" value %"PRIu64 - " incorrect (iteration=3D%"PRIu64")", - page, val, iteration); + TEST_FAIL("Dirty page %lu value (%lu) !=3D iteration (%lu)", + page, val, iteration); } else { nr_clean_pages++; /* --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 16BAF18F2DF for ; Sat, 11 Jan 2025 00:30:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555432; cv=none; b=CvtTTQNUszxYACUmhcAPbCVZy/cc6zMWbYCWLQqRH+1SxfNGPB86tVQWwm01KvOOS8CNAlO8XQuJb1zswz2FIie/PgIeGTcnY9k3Do9hPZKZH87N+D+1hwmREEQpTPRJsFPazl6Il3ava06Fc6FQnJLZlhwqumnI8C45S+9pt4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555432; c=relaxed/simple; bh=X7hrJAn5Xd5/ffe52AsOh35kq+1hLy36dj+3TszD2jM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UykIxvKebHNUnBTLLJkF5X9gnuFssOkdmA0+FUwjpOba5bF4go0IBLPF5UOYxWatbZ/3k6Y4jGMJ74qXUaXpDW+vw17Qq3UB1CZ5nHNiBk97RRU8DdXiiw65+L1ATukIKsNz/siEuSOuD7+GIcIomuxinUmp/WY8PS9rO2mylpU= 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=F3kdqhOo; arc=none smtp.client-ip=209.85.216.74 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="F3kdqhOo" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee5668e09bso4738662a91.3 for ; Fri, 10 Jan 2025 16:30:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555430; x=1737160230; 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=xyN0UMRkk6rC6WIB3fcMIoPE8NE4GF50oCJ4Jr6Xg0Y=; b=F3kdqhOoJPU1ILmLGSbMSfJU6GKiBddLNkQpIBuzga/DrIPukoiBEIaeTFN2/cjOsO EdKislkUdig9PE0zDi2pRd4CnJ7rYm9dCJfc7R0yYKiQm9oJ1z4EWGgt3tnaKwv+v7B3 V/78b2IAY8WvIc5zsKHegWhggH/WbjEJui6F/9HZZBTi8bzNESyVcSfCuYUy9qwKryto nYxyd11M9BmPuo9S8hiqJkmCSKf/F5SqZp/qowI8gmE4Sq9/IT69v7pGJcY3kwwLpXRk E0Iq73mnxe05vw7XXIxKVzF6xZOvX2s4fDLN5r+SqZXvBVdxW8szYAsw+dKgTkUKxJVL yi1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555430; x=1737160230; 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=xyN0UMRkk6rC6WIB3fcMIoPE8NE4GF50oCJ4Jr6Xg0Y=; b=ApoXySHtgqld/uPBb3Gci8UsMkPYqjebQzPBicOEhsAPyvOYvq2q4ZXDpjIS45Sulp DM2UJxsmzf9pXb+rYeT31XxAcrXfQfiCIU/6e+l1F+c2EUQ5p67DCJP3cY/IxqpzahG4 AvTj3tHgvLNR/QHKmm0luz9ePmj0f5Tp6T1rX4G5ap/qVWmwJNWNmQBCEzpkDyEn+e6P Z58IOgtwTQnlUjERvM0sDizGu05yjx5ljUTeOZj26lO8TwvSBMChUTAr/0K+7t57PXl9 51chUB1uQHLe6Co8gv3EsjgVGpcYcxvVky8tzf7C18jXA5k43Dx2BLTSql25pZHdog7J jhKg== X-Forwarded-Encrypted: i=1; AJvYcCXpHsZHmHq+RFF8w/feL56ijIvJ2cxh4WK3rT3siQWNXHkNg2PsbQLIpcZwTbdQiMxstSasIL0eupzT8M4=@vger.kernel.org X-Gm-Message-State: AOJu0YyNsg9kknBRmNr1gG3BesJQE5Gbu4ZRUlm7Rg8b6rNf7tV/6kLI NAPLl7r96n8vMuhOGvPU3r9hMTj2a3ZUthlyYMxrXSfh1usjUZA8Z/ueI3CObJnnyAtA9/qtVaC xoQ== X-Google-Smtp-Source: AGHT+IEVKgEyyMYviyDeVcIm5o6jXTSkBhgMiqMrJzFk4QIbXfpDD6AKxEzcxV0qnKVdo1toVPZDgB8gkB8= X-Received: from pjbqi14.prod.google.com ([2002:a17:90b:274e:b0:2ef:d283:5089]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2509:b0:2f6:be57:49cd with SMTP id 98e67ed59e1d1-2f6be574ac9mr253754a91.25.1736555430491; Fri, 10 Jan 2025 16:30:30 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:57 -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-14-seanjc@google.com> Subject: [PATCH v2 13/20] KVM: selftests: Print (previous) last_page on dirty page value mismatch 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" Print out the last dirty pages from the current and previous iteration on verification failures. In many cases, bugs (especially test bugs) occur on the edges, i.e. on or near the last pages, and being able to correlate failures with the last pages can aid in debug. Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index d7cf1840bd80..fe8cc7f77e22 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -566,8 +566,10 @@ static void vm_dirty_log_verify(enum vm_guest_mode mod= e, unsigned long *bmap) } } =20 - TEST_FAIL("Dirty page %lu value (%lu) !=3D iteration (%lu)", - page, val, iteration); + TEST_FAIL("Dirty page %lu value (%lu) !=3D iteration (%lu) " + "(last =3D %lu, prev_last =3D %lu)", + page, val, iteration, dirty_ring_last_page, + dirty_ring_prev_iteration_last_page); } else { nr_clean_pages++; /* @@ -590,9 +592,10 @@ static void vm_dirty_log_verify(enum vm_guest_mode mod= e, unsigned long *bmap) * value "iteration-1". */ TEST_ASSERT(val <=3D iteration, - "Clear page %"PRIu64" value %"PRIu64 - " incorrect (iteration=3D%"PRIu64")", - page, val, iteration); + "Clear page %lu value (%lu) > iteration (%lu) " + "(last =3D %lu, prev_last =3D %lu)", + page, val, iteration, dirty_ring_last_page, + dirty_ring_prev_iteration_last_page); if (val =3D=3D iteration) { /* * This page is _just_ modified; it --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 88462190462 for ; Sat, 11 Jan 2025 00:30:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555434; cv=none; b=Zwi5oGKAxPjm7Bs3i00kG1bys1vRlbLqp2E5jG0oFbRybkaQdoXVol0e1weiucCwBV/4Vk2BJVI6X96jrSMSFRDmqmn2Zao2dJ+sR5SrdAqVpv3nfgG08otI1z5yl0GZXJK9oFmNLyG0l4Bi6IaPgdKz+vshwUmoeAlkgA9CF34= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555434; c=relaxed/simple; bh=uHBTQ1MR91fKk9C6yhATyFnfaltG5kAv8HeS2SlSvw8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gN9w8FPOpqOUilMqz36gP+5HuxSXXNmzv+WN8yHxxAnyBibUiliL+BqFXhfGNxryWkrl0gnpLsAEmsLuVwXSS9ABSrYgfuXlxAOu1kMM2DezUXf1jSHDUxCCt9JE4anrxch3ddY6DoZZVsW4A8Sq4XqZmdoUQMlvWX0HNlo8pIs= 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=MAtyv4OT; arc=none smtp.client-ip=209.85.216.73 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="MAtyv4OT" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2efa0eb9cfeso4700337a91.0 for ; Fri, 10 Jan 2025 16:30:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555432; x=1737160232; 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=k/WoCua5rXlbcFyljn1VWee9B6n0EVLZHTgnpdWpUBU=; b=MAtyv4OTS14pbVkOSVFbLYaY73k4kwYckgsh676RMpoUfmTCS29/x7zojZWCBSTKnP w3Fse3Ru80iK5gAm/fNOAR+3Pjn+COSe3tJIshzzKfI1J2n2BWr4nNoIU503fBc/t0s7 zftLG7uPmRUd44UPMBJwmp4NBicmRES5mATq8Q4te3/+WCQrvr9zIEvj7IUV3fXK8SuY ZIcMm6ik+8WHv1z3Y/DemOVmH9R4kzc/NsE/DXyhAWC1h5CZ/8hk0B15cFYqJC+8EiSt B7BqSv0Fo1Hu29n1EPH4YIorCr6NdNAgwX6AnIMZN5Ff9tDbVz28ruwSGeokwIrZMEvs k+Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555432; x=1737160232; 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=k/WoCua5rXlbcFyljn1VWee9B6n0EVLZHTgnpdWpUBU=; b=FmNuEFpJzpg39zFWfn71qu2Iayq08BSdGFFy0VHFzYV/lr2LiwIeyq2gV72gdLzBzj 8/voqjKdQL4w/n693BQGNxxXcLm4juZfsLSTUG5qZ14tNdhzta17bPbWKVbatQ/QLz/M piAEQf/gFsGeMDyRICvKMRVgKaiAjSgTfw9womjPuWc2g6TV2cjLWyF9J1zDRjewQDXr 7A9DT3yBG4rEY6/Tcnhud7auGwcYi3VbbifLJjdmFRSST7lIoFClc6MrMV+agu2dOOaO dJ0O9vmV/cWNB8yNOm+bmJV60jlZUa1UmFL2ahcgxATGZHB2PbfkEWQoj9U/+CGRUdVC LudA== X-Forwarded-Encrypted: i=1; AJvYcCVpw7F0wIzBZASeMxDT8PhVjDZUvu9xAW/4CcRhCSQpiSv/LBHW2NHexPTZffn64ZEpiXf8xlFImnEn2Y8=@vger.kernel.org X-Gm-Message-State: AOJu0YxVUwgQ+fdm3d7qYtmKNEjEO96JueJS5luTxFsRQDIA3moGivXq GH15U/5w4a2b4zxAvOQaaoe6oetMoe/FeQGEi4OmFJYDTCLKT5bnpoX9WDmKHMPE15Cx1J8XCjg YRQ== X-Google-Smtp-Source: AGHT+IGfAsD82MPTOooMUIuLAPI+tMIahrYpTh9+mHzzcwIpUsN+dIK8wbKd4wg7pMjxdHs+5rAQwgbUyRw= X-Received: from pjbov6.prod.google.com ([2002:a17:90b:2586:b0:2ef:9b30:69d3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:c106:b0:2ee:7e53:bfae with SMTP id 98e67ed59e1d1-2f55836e87amr12011326a91.10.1736555432128; Fri, 10 Jan 2025 16:30:32 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:58 -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-15-seanjc@google.com> Subject: [PATCH v2 14/20] KVM: selftests: Collect *all* dirty entries in each dirty_log_test iteration 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" Collect all dirty entries during each iteration of dirty_log_test by doing a final collection after the vCPU has been stopped. To deal with KVM's destructive approach to getting the dirty bitmaps, use a second bitmap for the post-stop collection. Collecting all entries that were dirtied during an iteration simplifies the verification logic *and* improves test coverage. - If a page is written during iteration X, but not seen as dirty until X+1, the test can get a false pass if the page is also written during X+1. - If a dirty page used a stale value from a previous iteration, the test would grant a false pass. - If a missed dirty log occurs in the last iteration, the test would fail to detect the issue. E.g. modifying mark_page_dirty_in_slot() to dirty an unwritten gfn: if (memslot && kvm_slot_dirty_track_enabled(memslot)) { unsigned long rel_gfn =3D gfn - memslot->base_gfn; u32 slot =3D (memslot->as_id << 16) | memslot->id; if (!vcpu->extra_dirty && gfn_to_memslot(kvm, gfn + 1) =3D=3D memslot) { vcpu->extra_dirty =3D true; mark_page_dirty_in_slot(kvm, memslot, gfn + 1); } if (kvm->dirty_ring_size && vcpu) kvm_dirty_ring_push(vcpu, slot, rel_gfn); else if (memslot->dirty_bitmap) set_bit_le(rel_gfn, memslot->dirty_bitmap); } isn't detected with the current approach, even with an interval of 1ms (when running nested in a VM; bare metal would be even *less* likely to detect the bug due to the vCPU being able to dirty more memory). Whereas collecting all dirty entries consistently detects failures with an interval of 700ms or more (the longer interval means a higher probability of an actual write to the prematurely-dirtied page). Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 149 ++++++------------- 1 file changed, 45 insertions(+), 104 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index fe8cc7f77e22..3a4e411353d7 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -134,7 +134,6 @@ static uint64_t host_num_pages; /* For statistics only */ static uint64_t host_dirty_count; static uint64_t host_clear_count; -static uint64_t host_track_next_count; =20 /* Whether dirty ring reset is requested, or finished */ static sem_t sem_vcpu_stop; @@ -422,15 +421,6 @@ struct log_mode { }, }; =20 -/* - * We use this bitmap to track some pages that should have its dirty - * bit set in the _next_ iteration. For example, if we detected the - * page value changed to current iteration but at the same time the - * page bit is cleared in the latest bitmap, then the system must - * report that write in the next get dirty log call. - */ -static unsigned long *host_bmap_track; - static void log_modes_dump(void) { int i; @@ -491,79 +481,52 @@ static void *vcpu_worker(void *data) return NULL; } =20 -static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bm= ap) +static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long **b= map) { uint64_t page, nr_dirty_pages =3D 0, nr_clean_pages =3D 0; uint64_t step =3D vm_num_host_pages(mode, 1); - uint64_t min_iter =3D 0; =20 for (page =3D 0; page < host_num_pages; page +=3D step) { uint64_t val =3D *(uint64_t *)(host_test_mem + page * host_page_size); + bool bmap0_dirty =3D __test_and_clear_bit_le(page, bmap[0]); =20 - /* If this is a special page that we were tracking... */ - if (__test_and_clear_bit_le(page, host_bmap_track)) { - host_track_next_count++; - TEST_ASSERT(test_bit_le(page, bmap), - "Page %"PRIu64" should have its dirty bit " - "set in this iteration but it is missing", - page); - } - - if (__test_and_clear_bit_le(page, bmap)) { + /* + * Ensure both bitmaps are cleared, as a page can be written + * multiple times per iteration, i.e. can show up in both + * bitmaps, and the dirty ring is additive, i.e. doesn't purge + * bitmap entries from previous collections. + */ + if (__test_and_clear_bit_le(page, bmap[1]) || bmap0_dirty) { nr_dirty_pages++; =20 /* - * If the bit is set, the value written onto - * the corresponding page should be either the - * previous iteration number or the current one. + * If the page is dirty, the value written to memory + * should be the current iteration number. */ - if (val =3D=3D iteration || val =3D=3D iteration - 1) + if (val =3D=3D iteration) continue; =20 if (host_log_mode =3D=3D LOG_MODE_DIRTY_RING) { - if (val =3D=3D iteration - 2 && min_iter <=3D iteration - 2) { - /* - * Short answer: this case is special - * only for dirty ring test where the - * page is the last page before a kvm - * dirty ring full in iteration N-2. - * - * Long answer: Assuming ring size R, - * one possible condition is: - * - * main thr vcpu thr - * -------- -------- - * iter=3D1 - * write 1 to page 0~(R-1) - * full, vmexit - * collect 0~(R-1) - * kick vcpu - * write 1 to (R-1)~(2R-2) - * full, vmexit - * iter=3D2 - * collect (R-1)~(2R-2) - * kick vcpu - * write 1 to (2R-2) - * (NOTE!!! "1" cached in cpu reg) - * write 2 to (2R-1)~(3R-3) - * full, vmexit - * iter=3D3 - * collect (2R-2)~(3R-3) - * (here if we read value on page - * "2R-2" is 1, while iter=3D3!!!) - * - * This however can only happen once per iteration. - */ - min_iter =3D iteration - 1; + /* + * The last page in the ring from this iteration + * or the previous can be written with the value + * from the previous iteration (relative to the + * last page's iteration), as the value to be + * written may be cached in a CPU register. + */ + if (page =3D=3D dirty_ring_last_page || + page =3D=3D dirty_ring_prev_iteration_last_page) continue; - } else if (page =3D=3D dirty_ring_last_page || - page =3D=3D dirty_ring_prev_iteration_last_page) { - /* - * Please refer to comments in - * dirty_ring_last_page. - */ - continue; - } + } else if (!val && iteration =3D=3D 1 && bmap0_dirty) { + /* + * When testing get+clear, the dirty bitmap + * starts with all bits set, and so the first + * iteration can observe a "dirty" page that + * was never written, but only in the first + * bitmap (collecting the bitmap also clears + * all dirty pages). + */ + continue; } =20 TEST_FAIL("Dirty page %lu value (%lu) !=3D iteration (%lu) " @@ -574,36 +537,13 @@ static void vm_dirty_log_verify(enum vm_guest_mode mo= de, unsigned long *bmap) nr_clean_pages++; /* * If cleared, the value written can be any - * value smaller or equals to the iteration - * number. Note that the value can be exactly - * (iteration-1) if that write can happen - * like this: - * - * (1) increase loop count to "iteration-1" - * (2) write to page P happens (with value - * "iteration-1") - * (3) get dirty log for "iteration-1"; we'll - * see that page P bit is set (dirtied), - * and not set the bit in host_bmap_track - * (4) increase loop count to "iteration" - * (which is current iteration) - * (5) get dirty log for current iteration, - * we'll see that page P is cleared, with - * value "iteration-1". + * value smaller than the iteration number. */ - TEST_ASSERT(val <=3D iteration, - "Clear page %lu value (%lu) > iteration (%lu) " + TEST_ASSERT(val < iteration, + "Clear page %lu value (%lu) >=3D iteration (%lu) " "(last =3D %lu, prev_last =3D %lu)", page, val, iteration, dirty_ring_last_page, dirty_ring_prev_iteration_last_page); - if (val =3D=3D iteration) { - /* - * This page is _just_ modified; it - * should report its dirtyness in the - * next run - */ - __set_bit_le(page, host_bmap_track); - } } } =20 @@ -639,7 +579,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) struct test_params *p =3D arg; struct kvm_vcpu *vcpu; struct kvm_vm *vm; - unsigned long *bmap; + unsigned long *bmap[2]; uint32_t ring_buf_idx =3D 0; int sem_val; =20 @@ -695,8 +635,8 @@ static void run_test(enum vm_guest_mode mode, void *arg) =20 pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem= ); =20 - bmap =3D bitmap_zalloc(host_num_pages); - host_bmap_track =3D bitmap_zalloc(host_num_pages); + bmap[0] =3D bitmap_zalloc(host_num_pages); + bmap[1] =3D bitmap_zalloc(host_num_pages); =20 /* Add an extra memory slot for testing dirty logging */ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, @@ -723,7 +663,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) WRITE_ONCE(host_quit, false); host_dirty_count =3D 0; host_clear_count =3D 0; - host_track_next_count =3D 0; WRITE_ONCE(dirty_ring_vcpu_ring_full, false); =20 /* @@ -774,7 +713,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) continue; =20 log_mode_collect_dirty_pages(vcpu, TEST_MEM_SLOT_INDEX, - bmap, host_num_pages, + bmap[0], host_num_pages, &ring_buf_idx); } =20 @@ -804,6 +743,9 @@ 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. */ + log_mode_collect_dirty_pages(vcpu, TEST_MEM_SLOT_INDEX, + bmap[1], host_num_pages, + &ring_buf_idx); vm_dirty_log_verify(mode, bmap); =20 /* @@ -824,12 +766,11 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) =20 pthread_join(vcpu_thread, NULL); =20 - pr_info("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), " - "track_next (%"PRIu64")\n", host_dirty_count, host_clear_count, - host_track_next_count); + pr_info("Total bits checked: dirty (%lu), clear (%lu)\n", + host_dirty_count, host_clear_count); =20 - free(bmap); - free(host_bmap_track); + free(bmap[0]); + free(bmap[1]); kvm_vm_free(vm); } =20 --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 7F396191F91 for ; Sat, 11 Jan 2025 00:30:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555435; cv=none; b=cwshiQNT24VWAr7mxFpPxTWynPD3asLFXbTLp/lJhPlbDfNhj6G0H94GeeLePQ3A6C2m8BQd/k2DeOzOZKYjQRH/LWIVQY9zqQtpdvWM6OuTXzUwiJTMGz+0GeYjC/LRhiMtOHLohfVvlJvQQGqOPZIWD1ZvQuvitb43PHQQbnA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555435; c=relaxed/simple; bh=ZYZYkEZZBfkKU2arptoaqJUAJlhMnndiUETqebZmKlU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IiC0fmOzhKH28nWZAyOQRNVbV1GXuznuUZ3MfEtrzRYHJH1Lum5/Ql3wKNEyJ3LGTi0NHk2PFpSGsASAgZ5qJ+RFDE8xAiH74Ra1yLnud5qKinoRI87V4JzdSS7yaaAW7Vc0l75OM1eEZRCnBA0KzE9lr8gWkTCDLdR11veyDZk= 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=UfJjxmiW; arc=none smtp.client-ip=209.85.216.73 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="UfJjxmiW" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ee9f66cb12so4754644a91.1 for ; Fri, 10 Jan 2025 16:30:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555434; x=1737160234; 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=Tf3ai7YM4gD3aeEXOHvspZExdducxhbkAx/Bf8bRNDM=; b=UfJjxmiWO0FYNe/Rs+Cb3mNXgGFKr2OeCfzttcWyhvEuNN3U2FNUNilKy5WZjK4goj 3SO+sFB0E4YwzhQUR4E3DhM8cVDpd3ivC4NifqgflrcK5XpYR6HodAeTtDELDfIONuOP qsOHxQaAmxSgYLgN5sFia+MGP+ChmPCnuJWH0pLOX8H4knswX9148ylRhWG3UudpeyFn Btz5GHRMWsQz+ohVDwE6ndFn50ApQ+njd18kI2vtN74H+BfMTqaXbgYLhi1IpR1wmU6G PGCAIqMTy9UYW22GyelS+iRBd1Ck18SiM/IpHcqJZNuMfRWG0Em/ItLGBhr8ljLJfHIo 0z6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555434; x=1737160234; 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=Tf3ai7YM4gD3aeEXOHvspZExdducxhbkAx/Bf8bRNDM=; b=BmGyyRcfqbQRxocbs67yExbE3XwaVkCKbBHYpBsQOv7N71/JRfw48ptanQiqlzYVH7 62raZEeMrB2R7VuX/1/zSrcEXxKdVRSAkTEysoqe6yEc6zesDi7GXwY6v0c895T7lpy2 8H5UjUPNJefQWl8EZ0xqNk+SInrosFPlYq1zx5zK/O/3/jDh2gixqkRMf2LSQRiW4Xao V+dhrv6kzVHDkdOlJeF6jRrYj6xbTUzHnFuksx26qMk9Qjy+Lnl1K3MxXnbwxnL0EAJK uvTgnTGNBDhoCTbxchtAVId5Q2KDmmgTjlvN/zwt7PG2iM3xLeTDVO7JnA/4KZNQS3b9 ed9w== X-Forwarded-Encrypted: i=1; AJvYcCVgKXgLOysbuhh8+YN6CRoQ8WmLyNvEbIZm8yo9jV1auiVFEzi1pBx5/0Cudy/a1ntDMQlI3zZLv6bZqcw=@vger.kernel.org X-Gm-Message-State: AOJu0YzRyDkIpLwY90TQGuplG6ME9L4ctd6gytqrlqs1lGa0/d6LraGC JARfa1wNdvk4muyyWRcr5QVfnof1u2OVgZUHyHKLftxvhI/7oIqTNAVIxeiGqO2kM7k83At0d5X F7A== X-Google-Smtp-Source: AGHT+IEOfPzQKaiEZ+lOue6bQaGOCpx2jkMD63pcudGYIIJGaMpOLZezevh69lEqfxN5HF7M5FGj/Z6+bnQ= X-Received: from pjbsw3.prod.google.com ([2002:a17:90b:2c83:b0:2f4:3ea1:9033]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:258b:b0:2f4:4500:bb4d with SMTP id 98e67ed59e1d1-2f548eca081mr18398188a91.20.1736555433836; Fri, 10 Jan 2025 16:30:33 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:29:59 -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-16-seanjc@google.com> Subject: [PATCH v2 15/20] KVM: sefltests: Verify value of dirty_log_test last page isn't bogus 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" Add a sanity check that a completely garbage value wasn't written to the last dirty page in the ring, e.g. that it doesn't contain the *next* iteration's value. Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 3a4e411353d7..500257b712e3 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -514,8 +514,9 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode= , unsigned long **bmap) * last page's iteration), as the value to be * written may be cached in a CPU register. */ - if (page =3D=3D dirty_ring_last_page || - page =3D=3D dirty_ring_prev_iteration_last_page) + if ((page =3D=3D dirty_ring_last_page || + page =3D=3D dirty_ring_prev_iteration_last_page) && + val < iteration) continue; } else if (!val && iteration =3D=3D 1 && bmap0_dirty) { /* --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 1356F190485 for ; Sat, 11 Jan 2025 00:30:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555437; cv=none; b=TerQgd6k4Gz7Q3atTIkf12bVdZI1Nr9jaHqcsttoTS9Yg/rTRzr4fMXYTT8yL7A5U5Y+d3fMhaSkCZHI+kTU6THBKGIeCNdaBqgyttt67pdrO3rcpRLfNinEu83/wkK85LLmhf+xgBs/mk6BSK32TRbjdZJu42069V0a1cPRFKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555437; c=relaxed/simple; bh=AG2lZf4hql7JMLlay7hN7O3/rUWAX62xZ3g4Ko5iqVo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZBb4NBfRL7y7t2+4+Zb6bV438SIhbTYE/i1BtdWpHclF3D91WuS/JbQ5DbcdivKLAVidC6PZJOQYD9L+6gkG5BNLAn+2r+gwpseBa2Tey4Gm7O6qtpM8GNnIE9Gv5XpA9iHW89xmOA8CcUH3SmXOrtvoR8fI6TNoaa2QfrY10ow= 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=M0R3cfMh; arc=none smtp.client-ip=209.85.216.73 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="M0R3cfMh" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f46b7851fcso7396183a91.1 for ; Fri, 10 Jan 2025 16:30:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555435; x=1737160235; 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=0OUonYd6niVAqGjD0N5fWlc8jzGx+15I7hWbB4QzxGw=; b=M0R3cfMhlmE14YNbW8aB0FBhglLBPHWoE6jWo95A13KOWjUHF03m7m0C/nvwvUiMYt ZBuyf0KG/TdBn3wYxJQubmTVra4/5VzGC4OOCm4Yy21ID04lID8CXmvlMZot+6g4ph9O uiV/+lRonqjCEqhT11N90Qt1MaH0Zm2vzWEqGhq04+4RhZNcWh4TIrjFaQkpcx8aZrCo hA3cEQsTVU4Cz/Tj/8qpPfbZr7q2XDfeH7cPgJT0MRFnlF2AGcXtLJYS1xo8nVLuUFms uHaCv0oi0paLo5yWjWsBfpRl+uTp3P1xYzpY3WLAbJczYbEIV53wpVs93cjxtTkbJjaq C7oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555435; x=1737160235; 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=0OUonYd6niVAqGjD0N5fWlc8jzGx+15I7hWbB4QzxGw=; b=Afm3/7R6kbweL0N0UhRuovrtSfRmtWM1lM9+AgS96v3MMHpmvkPygVAM791hCk5rxZ lMAt9ju63fZ0g9e4Pf1e22h43G3tMkbPK+4jXeS+pFDEMH62LCpvPlzg+66eW0lci7Dp BY4MOu4BFVmSW6PaHd1JcmgIEFi/udt3FxMiT8+9J1l1w+4ZoEKGnVk9s6PkXjMGvDj3 Vm1gZuwxMK+96wtAx9r6Tt0Hx8RLevYzUZRY620JPeOyyE1Rqai31emnjmUleJHBPa0Q W7N91r4IQh9i+qb/CfOr9dh5BAjP+ZeaXPeU26mjn/5yq1iqdFNiaMB1UF8F+J9HZLqY 9/qQ== X-Forwarded-Encrypted: i=1; AJvYcCWFHRTIVhRa0m/mhmFhAmfA/z+mxbAa/VNTtyIT5iDG2yqCqUthdOiNvqqcW4LqynHVTUY8bLHFV7gvsf0=@vger.kernel.org X-Gm-Message-State: AOJu0YxgDyVxhCtDrxNdTN5PGN4MJbDtFjB+rVZsBK3gLCRFlf6oFnN8 WMcThhmf0kidWx2how1vkeDD1M/zM2xRAjaYn0FZ2l7yFcxuhC850hdF8QAGTTNKHuyHf4chW2i VNw== X-Google-Smtp-Source: AGHT+IHFSCYgXbnt54jfk0NUzmZPGnmPTpWpi3a7iqhyPYh9Baj/SV5EmS29ezlCSBsaMsUOuiqMn1EdHXk= X-Received: from pjbsu14.prod.google.com ([2002:a17:90b:534e:b0:2e2:9f67:1ca3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2808:b0:2ee:ab10:c187 with SMTP id 98e67ed59e1d1-2f548f598e1mr19787131a91.18.1736555435546; Fri, 10 Jan 2025 16:30:35 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:30:00 -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-17-seanjc@google.com> Subject: [PATCH v2 16/20] KVM: selftests: Ensure guest writes min number of pages in dirty_log_test 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" Ensure the vCPU fully completes at least one write in each dirty_log_test iteration, as failure to dirty any pages complicates verification and forces the test to be overly conservative about possible values. E.g. verification needs to allow the last dirty page from a previous iteration to have *any* value, because the vCPU could get stuck for multiple iterations, which is unlikely but can happen in heavily overloaded and/or nested virtualization setups. Somewhat arbitrarily set the minimum to 0x100/256; high enough to be interesting, but not so high as to lead to pointlessly long runtimes. Opportunistically report the number of writes per iteration for debug purposes, and so that a human can sanity check the test. Due to each write targeting a random page, the number of dirty pages will likely be lower than the number of total writes, but it shouldn't be absurdly lower (which would suggest the pRNG is broken) Reported-by: Maxim Levitsky Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 40 ++++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 500257b712e3..c6b843ec8e0c 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -37,6 +37,12 @@ /* Interval for each host loop (ms) */ #define TEST_HOST_LOOP_INTERVAL 10UL =20 +/* + * Ensure the vCPU is able to perform a reasonable number of writes in each + * iteration to provide a lower bound on coverage. + */ +#define TEST_MIN_WRITES_PER_ITERATION 0x100 + /* Dirty bitmaps are always little endian, so we need to swap on big endia= n */ #if defined(__s390x__) # define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7) @@ -72,6 +78,7 @@ static uint64_t host_page_size; static uint64_t guest_page_size; static uint64_t guest_num_pages; static uint64_t iteration; +static uint64_t nr_writes; static bool vcpu_stop; =20 /* @@ -107,6 +114,7 @@ static void guest_code(void) for (i =3D 0; i < guest_num_pages; i++) { addr =3D guest_test_virt_mem + i * guest_page_size; vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration)); + nr_writes++; } #endif =20 @@ -118,6 +126,7 @@ static void guest_code(void) addr =3D align_down(addr, host_page_size); =20 vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration)); + nr_writes++; } =20 GUEST_SYNC(1); @@ -548,8 +557,8 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode= , unsigned long **bmap) } } =20 - pr_info("Iteration %2ld: dirty: %-6lu clean: %-6lu\n", - iteration, nr_dirty_pages, nr_clean_pages); + pr_info("Iteration %2ld: dirty: %-6lu clean: %-6lu writes: %-6lu\n", + iteration, nr_dirty_pages, nr_clean_pages, nr_writes); =20 host_dirty_count +=3D nr_dirty_pages; host_clear_count +=3D nr_clean_pages; @@ -665,6 +674,8 @@ static void run_test(enum vm_guest_mode mode, void *arg) host_dirty_count =3D 0; host_clear_count =3D 0; WRITE_ONCE(dirty_ring_vcpu_ring_full, false); + WRITE_ONCE(nr_writes, 0); + sync_global_to_guest(vm, nr_writes); =20 /* * Ensure the previous iteration didn't leave a dangling semaphore, i.e. @@ -683,10 +694,22 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) =20 dirty_ring_prev_iteration_last_page =3D dirty_ring_last_page; =20 - /* Give the vcpu thread some time to dirty some pages */ - for (i =3D 0; i < p->interval; i++) { + /* + * Let the vCPU run beyond the configured interval until it has + * performed the minimum number of writes. This verifies the + * guest is making forward progress, e.g. isn't stuck because + * of a KVM bug, and puts a firm floor on test coverage. + */ + for (i =3D 0; i < p->interval || nr_writes < TEST_MIN_WRITES_PER_ITERATI= ON; i++) { + /* + * Sleep in 1ms chunks to keep the interval math simple + * and so that the test doesn't run too far beyond the + * specified interval. + */ usleep(1000); =20 + sync_global_from_guest(vm, nr_writes); + /* * Reap dirty pages while the guest is running so that * dirty ring full events are resolved, i.e. so that a @@ -737,6 +760,12 @@ static void run_test(enum vm_guest_mode mode, void *ar= g) WRITE_ONCE(vcpu_stop, false); sync_global_to_guest(vm, vcpu_stop); =20 + /* + * Sync the number of writes performed before verification, the + * info will be printed along with the dirty/clean page counts. + */ + sync_global_from_guest(vm, nr_writes); + /* * NOTE: for dirty ring, it's possible that we didn't stop at * GUEST_SYNC but instead we stopped because ring is full; @@ -760,6 +789,9 @@ static void run_test(enum vm_guest_mode mode, void *arg) WRITE_ONCE(host_quit, true); sync_global_to_guest(vm, iteration); =20 + WRITE_ONCE(nr_writes, 0); + sync_global_to_guest(vm, nr_writes); + WRITE_ONCE(dirty_ring_vcpu_ring_full, false); =20 sem_post(&sem_vcpu_cont); --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 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 C8A6119342F for ; Sat, 11 Jan 2025 00:30:37 +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=1736555439; cv=none; b=M9nUiG1dUlwQxZCLlPre1Nz8ptmrh9su6sZsQ4aZ9xyOtacNjx8thdgWenU6XJQYo5tnR1L5QfNq3OB+4MlK42oI3kiXOCTEPR9+/JaFOJZiZz8Iywoyz7+iCXAsNyfr/Uip3osiIzcD+xwzURLRohPsElHnNRUbcodWWQDhv1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555439; c=relaxed/simple; bh=ffKdnt6OQ9sJ5KUQQMoYl2zuIsgzofVRC0DeXLSAMvI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WhqO2NPLHDfYu8OH3Z45gIbknbwMD/ASO6yJSGm2ZkMinpZUWMLJbPKO9OB2BqbAbtsH61ci4Iqn4tGJC2bhRQtbCmyn+7RHap++Xy7FxgbV5fpkpNxJl7seLeCcOKaPK7SOzrg81yJVacbd/Mek8lH4bAayJeKS23IPvWwNJdE= 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=Q3IwUtbV; 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="Q3IwUtbV" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21661949f23so75990935ad.3 for ; Fri, 10 Jan 2025 16:30:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555437; x=1737160237; 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=lA1KJSr+vieulKkZ+q7IF2xXZKzzAMoWJFf8rW49CSw=; b=Q3IwUtbVZMGYOoCiXnz9xtr6SXB+NbL86bNN1xgufd5xC9/vTxKhrzvX9dVyNKSCl4 hXkHWrmcgw1VtGES3KmXocMCBGc/Rlf17+tEv2/RBpnP4U/nWVekqxw2b8eAromR1Ydt 8qjNe2ikYuZ8A1i/XJznVU8yb+h62wRkjemvPOlK2NXdsSo1JZBgX0MS0kJaxMMOEBqM HrTxij7fI5UY1DaSysFQp1eZy3O+1iKqUIDJ17d3e4CO/E7hfIz5jQ7Wv/G+/q9BQfET +TdhGW4MsipQxnl5A8t66ChKJEf1b7ayAvpzDf4unudITUbrwibFLstzu9g6HQnFnhTf GxUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555437; x=1737160237; 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=lA1KJSr+vieulKkZ+q7IF2xXZKzzAMoWJFf8rW49CSw=; b=mqnccXcrvi0jbyyNj6u9h/jxCdFBjwNSbVkzLfy365V+VyuUt6ZD4uBcuitsUjTBjF IPxUh+RV/Xp7WHUJSyAGL45nU4mm4wSUOHA4zE+771GRx82a+jCpYqBbqiydTFGO3OY7 CQE9K9FCquaA0ori1dWhzc4fayUwwyPvGi+QMwVdWv8qpfZCWRaTAYYvDRxLljwdaf7F 3AidNqIzUoUMz4SXRJKnDFrn7Nh5vcZ2/rtlVoe9G5tQt4IHc+92AhSZUiLhJ43u7cnh CWSRnaO673QobStucrQ5VoNytA3/ZFyhzXZLUBqTeJOrvkgyT+pajYMmlNQIY3Z3jc1L SioQ== X-Forwarded-Encrypted: i=1; AJvYcCWEAi3wUc2ymTH0oTKQzgi/ob61hKZfbEeMfxcyw0XNTELzRhFvBVldGrWZ7aq2QJC0/szSqHbpnnP11+A=@vger.kernel.org X-Gm-Message-State: AOJu0Yz8ciYmKdq0qkz5TZVNKXoVauMyKJW4BEvt0Eu+KfD6PM1EZHWn OqflSH/CdhOcdV04719jFpSGeJyCzb8/s2ztDm4XCbfZetL5eNRlT2lAH+x3uP336y1pmuCzGaQ c3Q== X-Google-Smtp-Source: AGHT+IFdUmsiFredELwAhQ4qCYzXu5e+Jrsp000dNrYBVJ5QXSlgLrbjK9TLk3bbURHzIFEBProJ78YM0dY= X-Received: from plbmq14.prod.google.com ([2002:a17:902:fd4e:b0:21a:8476:ecc3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d482:b0:215:b058:28a5 with SMTP id d9443c01a7336-21a83f52831mr168627105ad.18.1736555437233; Fri, 10 Jan 2025 16:30:37 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:30:01 -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-18-seanjc@google.com> Subject: [PATCH v2 17/20] KVM: selftests: Tighten checks around prev iter's last dirty page in ring 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" Now that each iteration collects all dirty entries and ensures the guest *completes* at least one write, tighten the exemptions for the last dirty page of the previous iteration. Specifically, the only legal value (other than the current iteration) is N-1. Unlike the last page for the current iteration, the in-progress write from the previous iteration is guaranteed to have completed, otherwise the test would have hung. Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 22 +++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index c6b843ec8e0c..e9854b5a28f1 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -517,14 +517,22 @@ static void vm_dirty_log_verify(enum vm_guest_mode mo= de, unsigned long **bmap) =20 if (host_log_mode =3D=3D LOG_MODE_DIRTY_RING) { /* - * The last page in the ring from this iteration - * or the previous can be written with the value - * from the previous iteration (relative to the - * last page's iteration), as the value to be - * written may be cached in a CPU register. + * The last page in the ring from previous + * iteration can be written with the value + * from the previous iteration, as the value to + * be written may be cached in a CPU register. */ - if ((page =3D=3D dirty_ring_last_page || - page =3D=3D dirty_ring_prev_iteration_last_page) && + if (page =3D=3D dirty_ring_prev_iteration_last_page && + val =3D=3D iteration - 1) + continue; + + /* + * Any value from a previous iteration is legal + * for the last entry, as the write may not yet + * have retired, i.e. the page may hold whatever + * it had before this iteration started. + */ + if (page =3D=3D dirty_ring_last_page && val < iteration) continue; } else if (!val && iteration =3D=3D 1 && bmap0_dirty) { --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 95DDB1991CD for ; Sat, 11 Jan 2025 00:30:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555441; cv=none; b=jg+1PrPNPkOQ+X6AlLl80HbaCyAkabvk6xEMpNgxoSzSRD17ddSrp77DPX7JtKdAJUrMq/wltNn+Le154uPPz6Ee/IWEG/7dEHoiUcgds7AkDZ9GYYCrHCqSBqf84FN3avMxj3nUnMWbfR95ZlV6cdtkWV+oq/Edr4JSCwTfkE8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555441; c=relaxed/simple; bh=L8D0b1+KiW9ahs+qBDC4G+scCazWGnThE6lO6mNZavo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PJdLV4XLaf87We6rUeoMJmaK8cRj35WCGwZLYxsDnGTvJT3TsCpFq8XS3CoZRBEyrnej6+SnpZddNvbDp1drCJyRssZ5AdpjqddvKavuLUKvgjiSMJYegZb92XVorlfWapNdkGB3bJ7NI44m31QHunMSK5VnO1tE+mNW8ZSUy/0= 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=Z5JrCNUY; arc=none smtp.client-ip=209.85.216.73 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="Z5JrCNUY" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef9dbeb848so4563138a91.0 for ; Fri, 10 Jan 2025 16:30:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555439; x=1737160239; 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=S8bBOr0/HNvEyebC3bb/WW1HKqndMpC6/ffxTDW/jVI=; b=Z5JrCNUYFVEFwq0KZ9FlqZ1Om7cI+IF/5d+hW+8qXsVFOZ+bChU3rvLHzK7CJOaQev FffoTNa/ir12uQqJW92M3KtmyXIo9xkuSG64LGFTasxAAl0yS5UGVQ2J18bR0BFYfsc2 Vv4iSm7+XLcLqfz1fiSXYzrR+XGvPP43Yxd89NXMPPbIJhB6/w9jc8si3DAvsilOPFUT fVwXfcZXQpeCy3MoIU7YZxTl8ZcmKUAgTcNveUySCH4hU0hyKwb1JTTd8q9VBivnIGD+ kLYFDjSz5Gdm6byhtV7omiS4gTEfeDhVOJTUVYFz+hiy177VGPS36WiBy4i96y/gnFmb YGpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555439; x=1737160239; 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=S8bBOr0/HNvEyebC3bb/WW1HKqndMpC6/ffxTDW/jVI=; b=rzFuSUDMUBrzIUDUxVfw849SpvL+q/ncZ5UG/EXwe8DRvQ417NvHOriiMS5e1BV6UU 7VkrD2guc9J+6/zVVl2m850jOq4iVPCkAoFYvONuAk1tDlaMdgr7uc2kLZwTr5kK+cCV kXVWKlFuaBBNz6rn2gifU2qVNErLUH0N7bZeUtfRR06iy0YkAR987CzNbF2cndRI1j6W OFJ5m6uEtGY5YMwY5XGnyG0dVgSdCylbOCJrbOQ1XNxlK/8xB+ZiMzsQPRhjNMJDMo/r zCz7ZfF7nFaVutmFOU+eRzWC/dXmMDaC2so/Ro1+GRjmdA2bwBMUDb8bONJahji7n0Vk KnPQ== X-Forwarded-Encrypted: i=1; AJvYcCU5k2R/VhwciohTxWRZIn+H6t+jrRKHiC6+Ze/d+RZym4bPN26TjaSsnOM2k83n3ciDJcwS5t9p3ILiuhs=@vger.kernel.org X-Gm-Message-State: AOJu0YwGKcChO0Zl/Nwvzi2gyFNd/4zqhDDmh1gt61aTyyAgXvtDmmef iaoFpdvMrQlEz19ZP0lylSez00BGksGTmG23bRfGPOI6CUVxGl2DWquJRkzJV/WW0TwKFnukkBi vdg== X-Google-Smtp-Source: AGHT+IG8OJ4cGazMV/P/Pm31Fw9xdVTCsOZDWRosn8YO8BNm6aYjr7+8IBYnSgIAeh1WsD1lcKtnyJmoRDE= X-Received: from pjbnd3.prod.google.com ([2002:a17:90b:4cc3:b0:2f4:432d:250c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1348:b0:2ee:cb5c:6c with SMTP id 98e67ed59e1d1-2f548ee54ccmr15776191a91.22.1736555439070; Fri, 10 Jan 2025 16:30:39 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:30:02 -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-19-seanjc@google.com> Subject: [PATCH v2 18/20] KVM: selftests: Set per-iteration variables at the start of each iteration 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" Set the per-iteration variables at the start of each iteration instead of setting them before the loop, and at the end of each iteration. To ensure the vCPU doesn't race ahead before the first iteration, simply have the vCPU worker want for sem_vcpu_cont, which conveniently avoids the need to special case posting sem_vcpu_cont from the loop. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 43 ++++++++------------ 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index e9854b5a28f1..40567257ebea 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -481,6 +481,8 @@ static void *vcpu_worker(void *data) { struct kvm_vcpu *vcpu =3D data; =20 + sem_wait(&sem_vcpu_cont); + while (!READ_ONCE(host_quit)) { /* Let the guest dirty the random pages */ vcpu_run(vcpu); @@ -675,15 +677,9 @@ static void run_test(enum vm_guest_mode mode, void *ar= g) sync_global_to_guest(vm, guest_test_virt_mem); sync_global_to_guest(vm, guest_num_pages); =20 - /* Start the iterations */ - iteration =3D 1; - sync_global_to_guest(vm, iteration); - WRITE_ONCE(host_quit, false); host_dirty_count =3D 0; host_clear_count =3D 0; - WRITE_ONCE(dirty_ring_vcpu_ring_full, false); - WRITE_ONCE(nr_writes, 0); - sync_global_to_guest(vm, nr_writes); + WRITE_ONCE(host_quit, false); =20 /* * Ensure the previous iteration didn't leave a dangling semaphore, i.e. @@ -695,12 +691,22 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) sem_getvalue(&sem_vcpu_cont, &sem_val); TEST_ASSERT_EQ(sem_val, 0); =20 + TEST_ASSERT_EQ(vcpu_stop, false); + pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu); =20 - while (iteration < p->iterations) { + for (iteration =3D 1; iteration < p->iterations; iteration++) { unsigned long i; =20 + sync_global_to_guest(vm, iteration); + + WRITE_ONCE(nr_writes, 0); + sync_global_to_guest(vm, nr_writes); + dirty_ring_prev_iteration_last_page =3D dirty_ring_last_page; + WRITE_ONCE(dirty_ring_vcpu_ring_full, false); + + sem_post(&sem_vcpu_cont); =20 /* * Let the vCPU run beyond the configured interval until it has @@ -785,26 +791,11 @@ static void run_test(enum vm_guest_mode mode, void *a= rg) bmap[1], host_num_pages, &ring_buf_idx); vm_dirty_log_verify(mode, bmap); - - /* - * Set host_quit before sem_vcpu_cont in the final iteration to - * ensure that the vCPU worker doesn't resume the guest. As - * above, the dirty ring test may stop and wait even when not - * explicitly request to do so, i.e. would hang waiting for a - * "continue" if it's allowed to resume the guest. - */ - if (++iteration =3D=3D p->iterations) - WRITE_ONCE(host_quit, true); - sync_global_to_guest(vm, iteration); - - WRITE_ONCE(nr_writes, 0); - sync_global_to_guest(vm, nr_writes); - - WRITE_ONCE(dirty_ring_vcpu_ring_full, false); - - sem_post(&sem_vcpu_cont); } =20 + WRITE_ONCE(host_quit, true); + sem_post(&sem_vcpu_cont); + pthread_join(vcpu_thread, NULL); =20 pr_info("Total bits checked: dirty (%lu), clear (%lu)\n", --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 46CBC1A2550 for ; Sat, 11 Jan 2025 00:30:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555443; cv=none; b=Ex532ktK2LoEI7Yu53to+rnxSm4jKNttw8gZvOvlPl3r4UMLSxtZ3LPQhC8GLy1p1XC5dkkcW75OVY9ZdZS9C5qD3nevz62Pwq0ewxzPR8zqawkoPnT1yT+79XJCuPPe+kTdU1Ji+0eVVQtzFyDMn9GuaDF2rgOfYH5ZyolnTUw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555443; c=relaxed/simple; bh=PsjCgtsdswqiLxYs85wIVANE9O6zvdBrqZFXA8iPzxA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NnhGU86tHysPIJnDECRp5RqwcXvz0Ljo57UVajQQqvf2MLPIpbHRGBa6+AwoWFEcll74aJlhRVSerh0XazaVRpBo6S5KUI6EPqzLJbYhnR5Qy0BH4FJ33LS2xrJO705dsJyVlUqV3zbYEQF3SCptwZnE3siojoUBIwrrKoZn9Jk= 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=eeaGOH+B; arc=none smtp.client-ip=209.85.216.74 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="eeaGOH+B" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef79403c5eso7421210a91.0 for ; Fri, 10 Jan 2025 16:30:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555440; x=1737160240; 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=CTB969N3z4oSgV+Wd1f59dXC53zHA3ksCR5erFyv7TI=; b=eeaGOH+BFltafHSMS64WFiJUxn04s1sHRWbP4yhnWATCPsNwp4lrpf+eWRutbmoMXv zNp7GeFsxC5c2JJOYE/VS5kNNLeO6joMCJOl1S6kjGjqe0xjsvK8Xec2qxJgc816kJCy Kfy2x1pCtTfi4CV4gM+mD3jbIQetwHsZMCoB8CQcuAecKtxLjZKoZ3uz3cUIaSAnY5pG yA6fZo4B1yYh8Id4jeRckHiPlixZ0H947puWV2CYLx4R8NQ5hi7QOc2LebtCVnt4br/S NJVWXjhhnXWH727lT4u4JrwYgUWPSIvNZKDUtLuOeJ9BDnkIkbWYq3DGSpzug78NwgGt a6iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555440; x=1737160240; 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=CTB969N3z4oSgV+Wd1f59dXC53zHA3ksCR5erFyv7TI=; b=aGQE5JEVH06mx8EHGe+p7Omt+P11qpQfFoCb+5zdLnBxkQb8jF9reNqeljEeTs2sxL PlYcDGe3vx5mUZBO6enDZDxTKHcn0mIsGO/7C9jLWj3YEHSu0wL3F8vOm9Ko/gOSuydg aTLwaGwY3ZM+uGLIBqPlmIpuctKRmofB5+18kR0yL9Ds96v22CWyQz9zlW68rW38436v 8fh5BGSQqBlqwHQdqmrq4vwhr69wiLjYfr3ijpDSSxEBGK+vINRbvjtF3ONSvHbBPj1z gIaIqQbvWV+Jy0K3O01Ww83lXOBvP9I/Jpds4MEk9NGa1fRlvc0XYhdE877curJGhWlG Hlsg== X-Forwarded-Encrypted: i=1; AJvYcCVRMBYLDMBot+0UZ18xljBQ7KtqhKTzl5EVGxWVhauQK7JjFikjnljF2rEezTs9QX7SwNcd1n1U5Z5+qWs=@vger.kernel.org X-Gm-Message-State: AOJu0YwhpN1X+mkxQa7HxENqly96M8PoymCuigKLGbnphFBaEJrJMaje dXLP0CpkrVj7iDVMHjk15opsF9DXYc2YQT/KjsqLb4tEmi2F/jtBFT9auSC/k51WtDnNCXlsg+h p9A== X-Google-Smtp-Source: AGHT+IGvUD6mfMIH0QjQvbQ1pO3vfOtc0UNk2rtfvTC5l9ZKmUXTAlFim5JHKHRdjCL5vQj+KstFEZdygEk= X-Received: from pjbso18.prod.google.com ([2002:a17:90b:1f92:b0:2ef:701e:21c1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4c88:b0:2ee:5958:828 with SMTP id 98e67ed59e1d1-2f548f2a01emr19478940a91.9.1736555440704; Fri, 10 Jan 2025 16:30:40 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:30:03 -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-20-seanjc@google.com> Subject: [PATCH v2 19/20] KVM: selftests: Fix an off-by-one in the number of dirty_log_test iterations 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" Actually run all requested iterations, instead of iterations-1 (the count starts at '1' due to the need to avoid '0' as an in-memory value for a dirty page). Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 40567257ebea..79a7ee189f28 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -695,7 +695,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) =20 pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu); =20 - for (iteration =3D 1; iteration < p->iterations; iteration++) { + for (iteration =3D 1; iteration <=3D p->iterations; iteration++) { unsigned long i; =20 sync_global_to_guest(vm, iteration); --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 16:12:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 EA5F51A4E9E for ; Sat, 11 Jan 2025 00:30:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555444; cv=none; b=Dx8KlbCajYszUf9SccciyyZerTBR0P0tSXiYwfgIRULK9idDE6D2SK72leRVZll2GBIuDsp/dYcimrlml2a5rdeJPRj7cuv1bOMYtJkHUULZ54NdhGCb8fBrrGWS9AjaXIy/WNp3tKTGCNBjdsyZhHEfT0CBGPkr8J/JNh/f2zs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736555444; c=relaxed/simple; bh=CgCTnbjfFNnVKpFKwg84z0FvtEtLBmL4OxVK5kbQct8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dsO5BSkFAG2Q3i0ZkoF4tMGMu44mU+lMIRZcmxyq5asFxhm22xzvpApChMV7RpJLxnG7eZWffbEJMPunQI3sMtcJPCtMJTwooLsLjXJxen7OJRvS009SQDOIWlh+Zg+yuEr+Da7YTbFPiUHEwvZStIPn1EwhlpZ8k9N48HPmCk8= 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=fE1zz00A; arc=none smtp.client-ip=209.85.216.74 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="fE1zz00A" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef79403c5eso7421248a91.0 for ; Fri, 10 Jan 2025 16:30:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736555442; x=1737160242; 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=ocaylAF5hSvVSunY3A5I2NKvvF78/qIR/+EQce5KeF0=; b=fE1zz00AV27g3pTM9/VarL6mXqUsbrhdMvhYYQP80cBk8n66CU6FKoAOenHXeSzt70 5dHUxttqZ2A2pVVqaR8PbP/WyHEEOb40KDmygp7n990SKrzlITOD4Kv1XLLlnldFfLHo 7hBop3NgUOv+W+3t8DKJ5ONp1/XP4I/dynB7D3ZYulCIoSlr8xtXI0s1vJxiPFhM1gRX Cyb5sSKmMbbc5xU9cb+odY3MpqtuyUjhAgaicqJ4YrYh34u+Q/2rCBYCsfwGgg88js43 Cn5i+hW0g1kMSOJKVun3vK93kvR5TLyMUX4Wy4fZyWihZNdQWgk1nB3ZcmA68mQTYL8L FThQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736555442; x=1737160242; 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=ocaylAF5hSvVSunY3A5I2NKvvF78/qIR/+EQce5KeF0=; b=gNByJLUYXFpIZ0uUyXkMP/iXgjK8f62jgnty7tlRl8XOZxJNeZBejosDYFNBP6N4lx kkqxaU9OkurVnpo3HL6gBDbIUl1YkQA8IOrHJuph5AVS+qpCWC0ZNVvHiDF6XUUzKRAA Vw4t1uwXmnS97Kw8XweoNiN0EI7S88P0MfjIJ8j1TOws5HdKrDUWj4hyWrR46krkq6/U VsNtvtBOhX51AdB11TiqftAxvMbLe+ExpnJAHvA/n2fKiMpplGBRgaeLjSMdUHB3oZ0S 8yJ9Bu9LIp10X8bzfEsQbUwOZlJRRuRM+Hj0hTZ/r2HD1F+/FuUIbOw7j1i1zqwfF8SH S7Rg== X-Forwarded-Encrypted: i=1; AJvYcCUAMqgZn8EF9zAhjgxRboCjWshcwgWQgSFnp3B02WKkBT52Qq0NOg9Yc6hVdpsPUYw6uTiOQmWotUOZ/HU=@vger.kernel.org X-Gm-Message-State: AOJu0YwXUYOFIvs6AIrKQSJvr+5py6O+lgQLL2OaSMs4zGnbFicyYMaj h/EbUmRPO+Y+94Vq+fmKi+3VVYWlh1YX1GiurAt/CPU2crX+WfBD5pcdzPUr5d/EwVyebWuk+Vx LVg== X-Google-Smtp-Source: AGHT+IGOOC+AbNNc0zQxpJQQyZielrn+6dpDffyNQ8EiCkz3fAyLcXYEPD0T1b2cpF01xoTFHd4VcJ7W2aI= X-Received: from pjbtb12.prod.google.com ([2002:a17:90b:53cc:b0:2ea:756d:c396]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2808:b0:2ee:ab10:c187 with SMTP id 98e67ed59e1d1-2f548f598e1mr19787698a91.18.1736555442230; Fri, 10 Jan 2025 16:30:42 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:30:04 -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-21-seanjc@google.com> Subject: [PATCH v2 20/20] KVM: selftests: Allow running a single iteration of dirty_log_test 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" Now that dirty_log_test doesn't require running multiple iterations to verify dirty pages, and actually runs the requested number of iterations, drop the requirement that the test run at least "3" (which was really "2" at the time the test was written) iterations. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/kvm/dirty_log_test.c index 79a7ee189f28..23593d9eeba9 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -886,7 +886,7 @@ int main(int argc, char *argv[]) } } =20 - TEST_ASSERT(p.iterations > 2, "Iterations must be greater than two"); + TEST_ASSERT(p.iterations > 0, "Iterations must be greater than zero"); TEST_ASSERT(p.interval > 0, "Interval must be greater than zero"); =20 pr_info("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n", --=20 2.47.1.613.gc27f4b7a9f-goog