From patchwork Wed Oct 9 15:49:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13828854 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BF0E4CEE333 for ; Wed, 9 Oct 2024 17:15:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=EGuxTu/doK/WQ641h0uZhH1HM6LHjGgjZS75tNbw6SU=; b=DgodUIMHuI4GIiuaF+1iRyoaaz W0OCfKxNfg7FTt8sU485mWh7eNtResfIG0nPxJht8eTM9gIQxID4/4Ej66JMkGMsLmZiZvCQ56ImU Ek1to5UrUCeHDW1DbbwtTGkvP9i2yGC+qaNOiIVaYyK8OuyBTJ4Qmza0zeVXIc7paj2qb33tW2mN5 YeSE8FyOi8c8NCyRvubzw+Vch4a5UTpgavbTWLTAf90o424UCrZEqwWP9QPcYq+MLQHCPVGuvfulg KbZ8nsca3U2TzJmtGA2c8ZUktCurtdxSnliwCwRU5vOZ7Wm0oJwc/FA1wbYZmrQTAKe7ZtgH5JyAA Grhay2eg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1syaHu-0000000A8mG-2aBK; Wed, 09 Oct 2024 17:15:50 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1syYxE-00000009oFm-1lqn for linux-arm-kernel@lists.infradead.org; Wed, 09 Oct 2024 15:50:26 +0000 Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-7ea07766e09so3283209a12.2 for ; Wed, 09 Oct 2024 08:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728489023; x=1729093823; darn=lists.infradead.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=EGuxTu/doK/WQ641h0uZhH1HM6LHjGgjZS75tNbw6SU=; b=iwb9EdFFySSuLf0USm+3X7FdBCzO6t4jXcJaBkkls/dV3S+trgbQ/zCwbS+SfkQzXR nceGp18l6xNVFjwEZxQ8/CXEjdd+sqgfAOb6kaAxUUJrrVMMJqD5gQ4a7A7X1R6tsNHW TWJSC8N4mHR8ImpIvEwquskOCHp4d2B0MmBAh2YiZyBVPOblXpbGErVlNk2oImq64VTe zyIQBoyvmwyXCawjEqTHtKX7fUfQSwWk01bKwhLZSxhrUYKMw2yxFw5VAIpRJbyUQZHo ol45pxlnBQ/go/frGovgtcuvBNB4Gj9SAEYuZd9q6dswyw09sd9mExF8wvOVF+KSamj7 Z9mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728489023; x=1729093823; 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=EGuxTu/doK/WQ641h0uZhH1HM6LHjGgjZS75tNbw6SU=; b=I/fgY4kCpuXrsfqrAw/13N0vjXGqqjDWR6IYSOeGujDZQVV+l+fM6rrwHpQhj9fDw9 /65Xd+ayryUbwBkGBzoP/kxL9X57xqJGGl3zNuVCYhnK6JVm3WfGNPRmVhOxw+x3f1Hk /to80XXnlT4ERck1iOuwgY8NJT9D4bv042LHuyqrLx7k73nG1CRT5ycuSmHBzSiJ15Cd ZoT2kqbYQkxnl2hO6eFNtST1yqgu1dudMdQsbWlXcn5jBmF7sJ16IQEIpZ7F8mPxrlyX wg5EDHm4Pm8GElzJujw/SUugNNUauowpnoPBiCcZN5F35S9X+WkCNsDMLt52nJPxQcip 3vJw== X-Gm-Message-State: AOJu0YyWTavPg4Y4WHqQxklvCBfkSKE3UpvIM84ffXvgg2mcoKLwrAip oJHN9gS3dP3ZBLumQlBz7nGas7SBJp9gPRNC+sUMt6xM6AUunrbKBkTxyAJFyfPrJfAdTB6CGLk wAA== X-Google-Smtp-Source: AGHT+IG8czWnoykzOejAJo4GJw9hcMcaR/sDDZx+5vRUFyaEmI5qH9Oe9i56gZjLIllNwEPwjpmEWoICf38= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a63:704c:0:b0:7cd:8363:9f29 with SMTP id 41be03b00d2f7-7ea320345ffmr3164a12.2.1728489022361; Wed, 09 Oct 2024 08:50:22 -0700 (PDT) Date: Wed, 9 Oct 2024 08:49:52 -0700 In-Reply-To: <20241009154953.1073471-1-seanjc@google.com> Mime-Version: 1.0 References: <20241009154953.1073471-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241009154953.1073471-14-seanjc@google.com> Subject: [PATCH v3 13/14] KVM: selftests: Add a read-only mprotect() phase to mmu_stress_test From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Paolo Bonzini , Christian Borntraeger , Janosch Frank , Claudio Imbrenda Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Sean Christopherson , Andrew Jones , James Houghton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241009_085024_546688_140D20BC X-CRM114-Status: GOOD ( 12.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a third phase of mmu_stress_test to verify that mprotect()ing guest memory to make it read-only doesn't cause explosions, e.g. to verify KVM correctly handles the resulting mmu_notifier invalidations. Reviewed-by: James Houghton Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/mmu_stress_test.c | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/mmu_stress_test.c b/tools/testing/selftests/kvm/mmu_stress_test.c index c6bf18cb7c89..0918fade9267 100644 --- a/tools/testing/selftests/kvm/mmu_stress_test.c +++ b/tools/testing/selftests/kvm/mmu_stress_test.c @@ -28,6 +28,10 @@ static void guest_code(uint64_t start_gpa, uint64_t end_gpa, uint64_t stride) GUEST_SYNC(i); } + for (gpa = start_gpa; gpa < end_gpa; gpa += stride) + *((volatile uint64_t *)gpa); + GUEST_SYNC(2); + GUEST_ASSERT(0); } @@ -95,6 +99,10 @@ static void *vcpu_worker(void *data) run_vcpu(vcpu, 1); rendezvous_with_boss(); + /* Stage 2, read all of guest memory, which is now read-only. */ + run_vcpu(vcpu, 2); + rendezvous_with_boss(); + return NULL; } @@ -175,7 +183,7 @@ int main(int argc, char *argv[]) const uint64_t start_gpa = SZ_4G; const int first_slot = 1; - struct timespec time_start, time_run1, time_reset, time_run2; + struct timespec time_start, time_run1, time_reset, time_run2, time_ro; uint64_t max_gpa, gpa, slot_size, max_mem, i; int max_slots, slot, opt, fd; bool hugepages = false; @@ -279,14 +287,20 @@ int main(int argc, char *argv[]) rendezvous_with_vcpus(&time_reset, "reset"); rendezvous_with_vcpus(&time_run2, "run 2"); + mprotect(mem, slot_size, PROT_READ); + rendezvous_with_vcpus(&time_ro, "mprotect RO"); + + time_ro = timespec_sub(time_ro, time_run2); time_run2 = timespec_sub(time_run2, time_reset); - time_reset = timespec_sub(time_reset, time_run1); + time_reset = timespec_sub(time_reset, time_run1); time_run1 = timespec_sub(time_run1, time_start); - pr_info("run1 = %ld.%.9lds, reset = %ld.%.9lds, run2 = %ld.%.9lds\n", + pr_info("run1 = %ld.%.9lds, reset = %ld.%.9lds, run2 = %ld.%.9lds, " + "ro = %ld.%.9lds\n", time_run1.tv_sec, time_run1.tv_nsec, time_reset.tv_sec, time_reset.tv_nsec, - time_run2.tv_sec, time_run2.tv_nsec); + time_run2.tv_sec, time_run2.tv_nsec, + time_ro.tv_sec, time_ro.tv_nsec); /* * Delete even numbered slots (arbitrary) and unmap the first half of