Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1222746rdb; Fri, 9 Feb 2024 14:30:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IED6FCm1vTylbXYot0wQkj9g29kPn7a7j4sBf3EwR6NhAqbMqmSbxuYxAXKEMP5uQxjP/AD X-Received: by 2002:a19:645c:0:b0:511:612a:c633 with SMTP id b28-20020a19645c000000b00511612ac633mr250802lfj.52.1707517831438; Fri, 09 Feb 2024 14:30:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707517831; cv=pass; d=google.com; s=arc-20160816; b=Yr84fjNEPVdvQnthprkJydVjMttdpqq7DNzi2tWIrvfWpP+YekVVYuSVpc1vjY7J97 oC18eEEeX68BHQkhJHSW0xPtMlKKtIRGbXQxEP1oHRx6zbmQa+Dqk9QU+NNCt8lbUqV4 +ukxhVtLsi4bpREvAdLlE9G8z+MgP2nsrNoVlD1CHoCapOqrS2JGNz20hlERKyK6hPcV WOoJaSALDkhO6Z2jmacNLQ+svEOM/grlBSfEPNHxYPsFeNL6mySQ2oZ8hMy3U/wc2gyG Kmr6wznTHjH1vZFHOlMs1MJfF8zqq3PuYTTD4C8Ru5a5qA5r+RJ+KgIogXX+WT5LR6Ap i+tg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=a9w16DkYUKNtIq1BB7P1j7ef4bijSTE+8L2OPdx5r88=; fh=735Ekjjy1e8lDW5aETxGuhOytG2DXDMubRvEMgiw2WU=; b=NPJarbPvhheaPc2/zoO395NfAWp6xZOaye5/AIW7uC3JOkLgy2QbtHzjex4A1OawgH M/ZDVETyoomuwMS/eiYBceuhA+x5hGlyeDPiMDDnG+/+OVyMMupvcaUyM+QRWYYINVj5 oPiUEiLZ/nB155KUPJ5rSnmu9pmlLdPu4kOhw2dQU8IbJ3q9KGcT53Ya7+t66Tiw/5ve ETVmJ/PDcc/80q12xHYVNHxD8S6ocK/NnKl9vj5a10pW3ClZMdDpKgeYSd/yoXBxLkYn eZmcG1ZsdcApLgLnsEYZb0nhct3/ZLSfExS9Joe2/w9rXbPIPI+7v6b9OUYhbWn2GrzF pKmw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4rSD36P0; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-60107-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60107-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCW83USrBdSrFmoETosNfiSfwrXTJtV/nAZ29JBrY3sp7Cj6PYI1bA5Eviq+pvIrdZ3h8kXX7Yq3u7If+MUZzMpTyBLuevBFkeEhpP2KMQ== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id qw14-20020a1709066a0e00b00a3891a0ce36si1357303ejc.340.2024.02.09.14.30.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 14:30:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60107-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4rSD36P0; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-60107-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60107-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 00B281F24014 for ; Fri, 9 Feb 2024 22:30:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E485439ACB; Fri, 9 Feb 2024 22:29:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4rSD36P0" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 724F538DFB for ; Fri, 9 Feb 2024 22:29:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707517749; cv=none; b=Rz+R5B4+UIIHd1+Ezs7A+T0Un0g/Q1d/9CKzoTMIrG/vMawWcrI2jCjQGZZ6osUMeUDDUN8JQCyf6gU0gklTV+ILVM8BGkLAFnkJVRcCuHcT2u7BCQV+jrvLKqeAjSRTfAjNR2hvQf/Vo9wzhwayInuQh0gXDSzN3Rl/WJ7bsRQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707517749; c=relaxed/simple; bh=MzdZYOJ8bcmLxbBzxayTEgk/rh3FciY0A5qDjUGjyCw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ntacnGQiG1ekRnE3tf3O3EuNeIe2Xth6isdswpLtDLHWcAxWvc+sbv0PQlDvC/6Ok0ODH5faNl1GfUVptgiB5/IpjwhRW5hUOXYTJUketMjSgZHSPWV4HAzhC/p4KmsZXqYFgoUby2Xz57srYGvkWujcDTqYZD/oIo1kjfS0++Q= 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=4rSD36P0; arc=none smtp.client-ip=209.85.128.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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-604acd1d164so28997387b3.3 for ; Fri, 09 Feb 2024 14:29:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707517746; x=1708122546; 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=a9w16DkYUKNtIq1BB7P1j7ef4bijSTE+8L2OPdx5r88=; b=4rSD36P0XAQFuAK/Nms2+vP/iKlh4jKw3co2K416l19tvRWVQfYeGHrwdHSTAhdK+V vGF9bF9x4tJqT2Fs/aqmNcVD0wXXTcDxm7laxOjZP7vNf3K/+EY/1JkAS/hEG8k2RgLa JHCZ9zLtIv7REH4uXprpSVNI0BoLlo6WxA/tQ7XEqSbQBBC7cyPybdbbWUkck9TiZv44 h8Fz5VxXWXB4n9XVi+0mnMNelaXL/z2HNk60TcvyrGScblw4s2uLylza8BBd93AKCTQE 4W/livcJRfn51On7TIWf2oyfMuCVPC6MIL7oUlONZAuCYCp+UsGJ3+qwPSTJ7NV+LLVy abhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707517746; x=1708122546; 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=a9w16DkYUKNtIq1BB7P1j7ef4bijSTE+8L2OPdx5r88=; b=R0oI4DM42f+Tud2U6PN9HDLDDxtDJEdHHaM4jQiARG36DyNesWRS2jBKWYGWMsSaNn tHaSb0L18aO2UZcnMCvM/nMFYmKWrVPW0OQs+Wqw9sRbY1LRNAwjJgQRFvOKOx5YNGNF sZ/V37EtwhUqI+3XuvjunL3uQ7hT8J5N2BbpXnGo0mCrUpCnZXEkGA1XW+mWqtH/ToEX g4m8VDWLoGD84Y6FimR/AL5Hl/ZLM4+2CK9j1iOChYJbhlXF7Trw1vn4nG9TTeR+NzWf KRQlqpRzBCrLCANsHyZP1O0O6elsaimnVj9U01ylZ8Axd2BtUJUixUdvuiiVN6395ZWP +NoQ== X-Gm-Message-State: AOJu0YwOq+Hs+XripXVp7mlsG3YO7O+lskk98T+OaekYcH5LoxDca9X0 MrBvuK0KwztRotj2XdyhDQD2AYdyz2la6nmIZNycfE5/YWJoP8t2eqJLYdjl1WBKYI/E/TCBiio bzw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:ec9:b0:604:648:6dc0 with SMTP id cs9-20020a05690c0ec900b0060406486dc0mr161009ywb.10.1707517746491; Fri, 09 Feb 2024 14:29:06 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 9 Feb 2024 14:28:57 -0800 In-Reply-To: <20240209222858.396696-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: <20240209222858.396696-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209222858.396696-4-seanjc@google.com> Subject: [PATCH v4 3/4] KVM: x86/mmu: Move slot checks from __kvm_faultin_pfn() to kvm_faultin_pfn() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yan Zhao , Friedrich Weber , Kai Huang , Yuan Yao , Xu Yilun , Yu Zhang , Chao Peng , Fuad Tabba , Michael Roth , Isaku Yamahata , David Matlack Content-Type: text/plain; charset="UTF-8" Move the checks related to the validity of an access to a memslot from the inner __kvm_faultin_pfn() to its sole caller, kvm_faultin_pfn(). This allows emulating accesses to the APIC access page, which don't need to resolve a pfn, even if there is a relevant in-progress mmu_notifier invalidation. Ditto for accesses to KVM internal memslots from L2, which KVM also treats as emulated MMIO. More importantly, this will allow for future cleanup by having the "no memslot" case bail from kvm_faultin_pfn() very early on. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 62 ++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 50bfaa53f3f2..505fc7eef533 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4333,33 +4333,6 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault struct kvm_memory_slot *slot = fault->slot; bool async; - /* - * Retry the page fault if the gfn hit a memslot that is being deleted - * or moved. This ensures any existing SPTEs for the old memslot will - * be zapped before KVM inserts a new MMIO SPTE for the gfn. - */ - if (slot && (slot->flags & KVM_MEMSLOT_INVALID)) - return RET_PF_RETRY; - - if (!kvm_is_visible_memslot(slot)) { - /* Don't expose private memslots to L2. */ - if (is_guest_mode(vcpu)) { - fault->slot = NULL; - fault->pfn = KVM_PFN_NOSLOT; - fault->map_writable = false; - return RET_PF_CONTINUE; - } - /* - * If the APIC access page exists but is disabled, go directly - * to emulation without caching the MMIO access or creating a - * MMIO SPTE. That way the cache doesn't need to be purged - * when the AVIC is re-enabled. - */ - if (slot && slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT && - !kvm_apicv_activated(vcpu->kvm)) - return RET_PF_EMULATE; - } - if (fault->is_private) return kvm_faultin_pfn_private(vcpu, fault); @@ -4406,6 +4379,37 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq; smp_rmb(); + if (!slot) + goto faultin_pfn; + + /* + * Retry the page fault if the gfn hit a memslot that is being deleted + * or moved. This ensures any existing SPTEs for the old memslot will + * be zapped before KVM inserts a new MMIO SPTE for the gfn. + */ + if (slot->flags & KVM_MEMSLOT_INVALID) + return RET_PF_RETRY; + + if (!kvm_is_visible_memslot(slot)) { + /* Don't expose KVM's internal memslots to L2. */ + if (is_guest_mode(vcpu)) { + fault->slot = NULL; + fault->pfn = KVM_PFN_NOSLOT; + fault->map_writable = false; + return RET_PF_CONTINUE; + } + + /* + * If the APIC access page exists but is disabled, go directly + * to emulation without caching the MMIO access or creating a + * MMIO SPTE. That way the cache doesn't need to be purged + * when the AVIC is re-enabled. + */ + if (slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT && + !kvm_apicv_activated(vcpu->kvm)) + return RET_PF_EMULATE; + } + /* * Check for a relevant mmu_notifier invalidation event before getting * the pfn from the primary MMU, and before acquiring mmu_lock. @@ -4427,10 +4431,10 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, * *guaranteed* to need to retry, i.e. waiting until mmu_lock is held * to detect retry guarantees the worst case latency for the vCPU. */ - if (!slot && - mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn)) + if (mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn)) return RET_PF_RETRY; +faultin_pfn: ret = __kvm_faultin_pfn(vcpu, fault); if (ret != RET_PF_CONTINUE) return ret; -- 2.43.0.687.g38aa6559b0-goog