From patchwork Fri Jul 26 23:51:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743516 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 0131BC3DA4A for ; Sat, 27 Jul 2024 00:17:09 +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=Xse5B7AOfcqAOlY7bR5VB/olShqjuvoHnQg+aILML2Y=; b=bVkWn5E5P26/La5afb49/SmUJ0 OoVOBmDpQg04iDc/lHJ18ezCxrf6YeqY49ZdSRgFH4B+nfX9tL0KhZh3P3U7xiCQKdd4VNHZySzm0 Dm+Rmn1vLIj3/FWm9vfgfiaBqMjF9qJ2WOzpPn6msYUP0U3qFuwf24KNvrFN1M4SvuM+ScBWIaxVS 1g1vWORTW61F2QUWb69n1vV7SuNrTnv/INIEvcfllU9a1xPoa2SUzKu7m5GnfiOs68Hnb4qLH0xMI YJG3XYQrO5MAa4sXCGI/tgUiHC/AkiCxQBd83s3zRWry6BKNirZcxrkKAiExPnndOMV03aApsEgkt C/g+Bwgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV7K-00000005c7S-03Sa; Sat, 27 Jul 2024 00:16:58 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkx-00000005Q6T-1H3A for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:53 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d19a4137dso1423100b3a.1 for ; Fri, 26 Jul 2024 16:53:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038030; x=1722642830; 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=Xse5B7AOfcqAOlY7bR5VB/olShqjuvoHnQg+aILML2Y=; b=h7J9kfzruyv2/4kKVm58ZGH4y0IlNudqVeWE6/BepGyMXbF/2OSz2HQIaYFgzPANsE xUcLEO9JpXjPzZzr74/E8L1Q7IEpvD6ANmdk5UbfbbWAlZLUCJTkYhWqczOc1RhQYJA9 PBi0olLVJERvd5wQ8y0FyJwNCQMU3T1+RQkknHc+nNwgu1eVXXzaqI6UhfcTzzULaelf vaX+ktifoVrRrEM9eYyCjfvMpgch7a9lXURCHm1I3L40OJDTuBXOc3HroasjaJB0Cor6 PWJaGmwNbDAykEgtJcOhcLdg43TWWBCwmBxZ0xnKLx1lz/VB0XEzkiEasQyUFJ2b0VLx p09g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038030; x=1722642830; 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=Xse5B7AOfcqAOlY7bR5VB/olShqjuvoHnQg+aILML2Y=; b=nhm4mn9uRkC/DRdeuikgZ0VTaypWkuDoMySWsJtPj8g7f0vyDTNWo+3u65sVZh1WZh LS/BYI86y4wExf5/es3JyCuSesl4tv19527+0snHCr5lc2fwbxQnD/fxMOoYG2p9L4/c m4jPt2wTT2PQsyQIAGW98HDFEZ5Pjmb5gayodKcpKPSzf1RTNLituxLvwIDMASOY8EYw RbqEOSuS/FBf1hsQYZJYj8isHWt1Elux9UWPx4kRz19rab5v8QBqRabtyYLXvKSupop3 SFUawFhq6WlAH0YFBMoLhJoJZNExXIlslZotV9HBnX4rDRL4j5sqizI48qBIrzJeJITL +pkg== X-Forwarded-Encrypted: i=1; AJvYcCVxspb7Knaf5ZYmQI6S+O9f7AmfV7HyMKt8J6v75zY3bv0NCa2pzTxg3dqdQhwgXwFr1ZpZ0ceSJw3WffQWNTtzBrb7+vsfwXAaAKdq3eC359yGals= X-Gm-Message-State: AOJu0YyYPWh76DbygcN9Nm1zVHNNmPl1IMCYslWgwaBgiBRJtoA1W5ij zx/TU6uK8mwtfzIz+sUPyTi+LPKM06w6tqbd/PNXoZUBjYbyUg+jkRazC9tWWzXs7VqWYOvUPiR vQA== X-Google-Smtp-Source: AGHT+IEFcNwoZPtxzQ32cLJ1vxa7gkQJVcgltXDXIuLlGv+WV+Ss10kIc9nwtzcNKm3zIIgT6vAGf5Ndk9o= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2e2a:b0:70d:9a0e:c13b with SMTP id d2e1a72fcca58-70ece7f0562mr55820b3a.3.1722038029490; Fri, 26 Jul 2024 16:53:49 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:44 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-36-seanjc@google.com> Subject: [PATCH v12 35/84] KVM: x86: Use kvm_lookup_pfn() to check if retrying #PF is useful From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165351_580533_873CDB3F X-CRM114-Status: GOOD ( 16.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 Use kvm_lookup_pfn() instead of an open coded equivalent when checking to see if KVM should exit to userspace or re-enter the guest after failed instruction emulation triggered by a guest page fault. Note, there is a small functional change as kvm_lookup_pfn() doesn't mark the page as accessed, whereas kvm_release_pfn_clean() does mark the page accessed (if the pfn is backed by a refcounted struct page). Neither behavior is wrong per se, e.g. querying the gfn=>pfn mapping doesn't actually access the page, but the guest _did_ access the gfn, otherwise the fault wouldn't have occurred. That said, either KVM will exit to userspace and the guest will likely be terminated, or KVM will re-enter the guest and, barring weirdness in the guest, the guest will re-access the gfn, and KVM will fault-in the pfn and mark it accessed. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index af6c8cf6a37a..59501ad6e7f5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8867,7 +8867,6 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, int emulation_type) { gpa_t gpa = cr2_or_gpa; - kvm_pfn_t pfn; if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) return false; @@ -8892,22 +8891,15 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, } /* - * Do not retry the unhandleable instruction if it faults on the - * readonly host memory, otherwise it will goto a infinite loop: + * Do not retry the unhandleable instruction if emulation was triggered + * for emulated MMIO, e.g. by a readonly memslot or lack of a memslot, + * otherwise KVM will send the vCPU into an infinite loop: * retry instruction -> write #PF -> emulation fail -> retry * instruction -> ... */ - pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); - - /* - * If the instruction failed on the error pfn, it can not be fixed, - * report the error to userspace. - */ - if (is_error_noslot_pfn(pfn)) + if (is_error_noslot_pfn(kvm_lookup_pfn(vcpu->kvm, gpa_to_gfn(gpa)))) return false; - kvm_release_pfn_clean(pfn); - /* * If emulation may have been triggered by a write to a shadowed page * table, unprotect the gfn (zap any relevant SPTEs) and re-enter the