From patchwork Thu Oct 10 18:23:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13831067 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 2C45DD2444D for ; Thu, 10 Oct 2024 20:21:15 +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-Transfer-Encoding:Content-Type:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=W6Zzl9AmyqedpEIwNpLXiwPcmrnHw6ZPa/9pyFpLcZ0=; b=WCb8PZ6JfatFxB LmB6flgCWBJ/X1jZtABmMov4IuihAeVVonHT7sU9vMF2s1QnBClzBOmz8s0e7BQFEeDfgoHvfv9gz T0OI6l+rG+nE2hlVQLsCaTF07jKcEKQaAueVwY3bn+E2Xpg8dItOcn69WaOV6CT5ltQe0v9aOYOxp MebChTgfHIohKdDYoIZxD7Pm04SIdD8cWX3RcwE9+lgmY6LccCPGzCeUw4QTB9PvsODpMxKkbFkej 7FKZpACWkbbZoekC0KjZrrMneQUmHsofg5Y8V/cNZm/xtvwuUd9h3K5ZjA7wNjhVBHJaUvwqwkdFP WQ9MT+K3+QBeSRHIj2Wg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1syzej-0000000EFmb-1pw8; Thu, 10 Oct 2024 20:21:05 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1syxs2-0000000Dr2S-2lb9 for linux-arm-kernel@lists.infradead.org; Thu, 10 Oct 2024 18:26:44 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-6e3245ed6b8so23856787b3.0 for ; Thu, 10 Oct 2024 11:26:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728584801; x=1729189601; darn=lists.infradead.org; h=content-transfer-encoding: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=W6Zzl9AmyqedpEIwNpLXiwPcmrnHw6ZPa/9pyFpLcZ0=; b=OrgrVnTtrz+qiD9I0S55GdwguFxumnKv3WwSP9lTvNcek28RXofi/P4YgBMLPPYMhb zCOiCCo9fd6Nk8bAoy3uGkg5aB+LFL9zzvsIoGmtZJWHk7S3XH6jGl4UgTdmy0jdzTRe uigdTw+2nDTBogSg6xzYhiat0dWmlOkE4NK4fdBCXO6eLIjWbfaMMyYAgIx+4I9JXs+q YYlcvGdNTOyx4Od36oRcYng2RADTfSTd5NrCF3iOCvOKsNAyx+6m8RHLPIhQGep99KBK 95kPdxMBUQVU3/eq0UM2UXAuETUAQK8pz5gNiTU0JXjKfe390eNJWy/Ucn6yEkU139JE isgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728584801; x=1729189601; h=content-transfer-encoding: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=W6Zzl9AmyqedpEIwNpLXiwPcmrnHw6ZPa/9pyFpLcZ0=; b=np7HjsuGFgoDa7Z4gAqoqVgI/LEmBik/Kqn7pI7nyOuUUWVjPyRyJ6GuvENR6up/I2 EacAAnGM1mOwrQDCexQM+wECYufc6KPOj0qDmXDFBNkVSboASYlRY0s5lnz2u19ChYEZ WAVQkkL0h4C7hWwce4wUOeHQdBxWn5GN4OvK+r/d4Tvm7D5pZU65sJW9Lg3myuTUyOuA cjnkFlO6WTn2n4XD5+XUVdAycGJf8CA69O6vCjW/Ra0U2z6c8SpddCz/YP36P17lwsHG ISgJxIYRp6y9+CXwk0xZfZkaUXCjMqnT6RWl2czM0Wwpiw3znJwMbPmaGQESIslO9EEG LRTA== X-Forwarded-Encrypted: i=1; AJvYcCXqMow+H7lRwq/1uTKfktNsnlpKihsD+Zcpy6jtxgM8YuR3J92scznxCRThVPJYDx1F+qRTfXUYLYnfTTe3B2dB@lists.infradead.org X-Gm-Message-State: AOJu0YyOwE0D8Wq2IZfDuK4n9pNo6SrLX1wK6XuldLV0GOvS/HcnRBkp E5zsCgMWM/T/Pn9asL0XbOULUf8p4s+kdO9TmcSCBAJo/UqzKPrt7XI73dtjNShOovfnXLSuUfx qOQ== X-Google-Smtp-Source: AGHT+IGFUTZueI+Si3awRWj8jA+YGlhXMEzUcm/2nm7iA7dHF8070ZlvI1MSF0nUJUhFBhXPPz9v/uVg5Fg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a05:690c:2f08:b0:6e2:2c72:3abb with SMTP id 00721157ae682-6e3224db667mr816567b3.7.1728584801239; Thu, 10 Oct 2024 11:26:41 -0700 (PDT) Date: Thu, 10 Oct 2024 11:23:53 -0700 In-Reply-To: <20241010182427.1434605-1-seanjc@google.com> Mime-Version: 1.0 References: <20241010182427.1434605-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241010182427.1434605-52-seanjc@google.com> Subject: [PATCH v13 51/85] KVM: VMX: Use __kvm_faultin_page() to get APIC access page/pfn 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, " =?utf-8?q?Alex_Benn=C3=A9e?= " , Yan Zhao , David Matlack , David Stevens , Andrew Jones X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241010_112642_735781_7E781906 X-CRM114-Status: GOOD ( 15.80 ) 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_faultin_page() get the APIC access page so that KVM can precisely release the refcounted page, i.e. to remove yet another user of kvm_pfn_to_refcounted_page(). While the path isn't handling a guest page fault, the semantics are effectively the same; KVM just happens to be mapping the pfn into a VMCS field instead of a secondary MMU. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 851be0820e04..44cc25dfebba 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6790,8 +6790,10 @@ void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) struct kvm *kvm = vcpu->kvm; struct kvm_memslots *slots = kvm_memslots(kvm); struct kvm_memory_slot *slot; + struct page *refcounted_page; unsigned long mmu_seq; kvm_pfn_t pfn; + bool writable; /* Defer reload until vmcs01 is the current VMCS. */ if (is_guest_mode(vcpu)) { @@ -6827,7 +6829,7 @@ void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) * controls the APIC-access page memslot, and only deletes the memslot * if APICv is permanently inhibited, i.e. the memslot won't reappear. */ - pfn = gfn_to_pfn_memslot(slot, gfn); + pfn = __kvm_faultin_pfn(slot, gfn, FOLL_WRITE, &writable, &refcounted_page); if (is_error_noslot_pfn(pfn)) return; @@ -6838,10 +6840,13 @@ void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) vmcs_write64(APIC_ACCESS_ADDR, pfn_to_hpa(pfn)); /* - * Do not pin apic access page in memory, the MMU notifier - * will call us again if it is migrated or swapped out. + * Do not pin the APIC access page in memory so that it can be freely + * migrated, the MMU notifier will call us again if it is migrated or + * swapped out. KVM backs the memslot with anonymous memory, the pfn + * should always point at a refcounted page (if the pfn is valid). */ - kvm_release_pfn_clean(pfn); + if (!WARN_ON_ONCE(!refcounted_page)) + kvm_release_page_clean(refcounted_page); /* * No need for a manual TLB flush at this point, KVM has already done a