From patchwork Fri Jul 26 23:51:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743473 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 C538BC3DA4A for ; Sat, 27 Jul 2024 00:07:23 +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=f0UOaTihDpRJ7PP+Ej2doGcOk+AHWo0BkuOsHzozLVI=; b=wJx8W2GqimW00xPqRsDlBRppP1 xle3yK1t8YMrR4gvVPgkcmLuwtYlWLIsk/nqm63Plhi38xmOwul5PkarZY9U9wezXDPPUZn7cp5At NsfB6Cr2o5qOaPTQWQmWOsoACIcMigyFWmG1dUL7oMnPbxboS1n9CB0bak+uYaRhWN4xsrYGBBfNF L9Cx0rfGzrxPx7uf/P4Xo6R2rxFQ3hNODxmFWugkKbpsx2ynOT4mgxzWMFy4MqwqaFEvHeYd4vBxb 0pE0NI1gQx9UHn9c4yahsCULZdtYvHDAS9EK+5PwGQtfLD137wBmRvaFt0ng5Nk+1I4pUy2yOOJDm nbAmqUlQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUxt-00000005Xe9-2EOK; Sat, 27 Jul 2024 00:07:13 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUku-00000005Q4M-3wJz for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:52 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fc54c57a92so10626555ad.3 for ; Fri, 26 Jul 2024 16:53:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038028; x=1722642828; 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=f0UOaTihDpRJ7PP+Ej2doGcOk+AHWo0BkuOsHzozLVI=; b=euoXExK9GWAFuMpIdFQ7q8HBipFFrXmajA3RSJ0IUws9NyIaLOSByOXhNl74zIRdvE VSE/px8NDztYc0LeivyQjS8KKxIEKYMjKDpgGXeCdKensDYIUXnwhS3EoCWtoTrs8Ukr yXI+wZ2PSQMVVgDD3HhXLkin7p13ft0zh6hWSGOVbkj6N1yQOP/h8LSIy11Hm69GTpp3 izYwkBJu2zF+H98+30h/NBmgLub4s4DLNPQso4Acl47SqXicQnUNZV0HSzCxp4jGzHn3 3q1GfmGJ/S/s8x3l8rpP4cQ5LKwGpy+gL0QZscNe3m3L2tHcSGLf7/7FtKrWLxSwo4GE VmOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038028; x=1722642828; 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=f0UOaTihDpRJ7PP+Ej2doGcOk+AHWo0BkuOsHzozLVI=; b=iEoYY9tal0/oo7VbtCdr+M4gX/aIQd9AZ+2IbB+SiDzTXXoQ1tKD0w+w0jbRl7AQ75 t2gnJSTArOorg7sq0xQkogpJmwvN4tD52ApET7eIk/dSHfpvCudOIxgHxdz2sSkdQoCg 6262FR+sceCk6viIf0rnj6Lq22m5xdEIt3oExdUSJxRz7Iyrs+fwWWfSGl9pd+lhANU3 OYMm4eWbxs80yT6qSKJx3/5OBcsNdasBIMBS6EZuq3JEU4ShS4KZuihp6e5kjVWDd55l STVHQBBcTrxy1+Ut+kx+ENAtNBRJoZXKxoJrmbA6oGtqPHj3EujjsP0Mzc1zRSRXZWUP FzuQ== X-Forwarded-Encrypted: i=1; AJvYcCUacQk+N4OzG1VJy5edxGAqMd9aa8QGmtA0VHl/aqFML8Vz/vU6zMnhAWdIxNLNPYvWgOsRjPbgPaW6JpoLq6IP@lists.infradead.org X-Gm-Message-State: AOJu0YzpbgolPtt6w0TmcgG1lQ/405b/fXXHtPRzkuzpPwJ/OrERZzLE dT4HeMf87dll/W77Pywnl+XSLeqAehmuLyjOvnDr2bBwSbFLCGFnLSugsvy6JsyHIYRBpVukBlB 0Ow== X-Google-Smtp-Source: AGHT+IGYUMRc1uUxr912DKcgHH4gUUnvf0wLEKnF8dkY5ruUFOJLgoqFTBS1LD3evC3JUrfTD0ZWGbhz678= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:cecd:b0:1fd:6529:7443 with SMTP id d9443c01a7336-1ff0486a65amr545105ad.11.1722038027663; Fri, 26 Jul 2024 16:53:47 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:43 -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-35-seanjc@google.com> Subject: [PATCH v12 34/84] KVM: Add a helper to lookup a pfn without grabbing a reference 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_165349_206069_1250D7CF X-CRM114-Status: GOOD ( 13.13 ) 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 kvm_follow_pfn() wrapper, kvm_lookup_pfn(), to allow looking up a gfn=>pfn mapping without the caller getting a reference to any underlying page. The API will be used in flows that want to know if a gfn points at a valid pfn, but don't actually need to do anything with the pfn. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 82ca0971c156..5a572cef4adc 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1212,6 +1212,8 @@ static inline void kvm_release_page_unused(struct page *page) void kvm_release_page_clean(struct page *page); void kvm_release_page_dirty(struct page *page); +kvm_pfn_t kvm_lookup_pfn(struct kvm *kvm, gfn_t gfn); + kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn); kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, bool *writable); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0b3c0bddaa07..ad84dab8c5dc 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3118,6 +3118,22 @@ kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) } EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn); +kvm_pfn_t kvm_lookup_pfn(struct kvm *kvm, gfn_t gfn) +{ + struct page *refcounted_page = NULL; + struct kvm_follow_pfn kfp = { + .slot = gfn_to_memslot(kvm, gfn), + .gfn = gfn, + .flags = FOLL_WRITE, + .refcounted_page = &refcounted_page, + }; + kvm_pfn_t pfn; + + pfn = kvm_follow_pfn(&kfp); + kvm_release_page_unused(refcounted_page); + return pfn; +} + int kvm_prefetch_pages(struct kvm_memory_slot *slot, gfn_t gfn, struct page **pages, int nr_pages) {