From patchwork Fri Jul 26 23:51: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: 1965498 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=E64Zr+m0; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=K3AZZMXb; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WW4mB35bLz1ybY for ; Sat, 27 Jul 2024 10:11:22 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Reply-To:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id: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=F8wNAV4MWNb+/mpsW3E+zoZjJHoQa0gJYi/yxCCNssA=; b=E64Zr+m0slKCcn ebdV/yvMtX5Ea799tOM3v6KkjfIuX+RPZcSFASAk24niyZDLqMBaPP3ETLpVhXS0iL54jCX7ZOwZ9 gzAk/eAHKf1iqa58NbUVJWHu2pfMJMh0aGYs6eP0/zUCk6S9tMVhoDZPNDzaD+QA432O25T9ZOiiF fJyxXWfFK1MRVZ13Fw2siklIa9egJVM6KKX/AL2LJFw6Ht7xyKPvs/b+YTc9Bw11SwvECMEgVhSGe 9Po3KO4AEPwEeZVMoaSsryXVOb1EyyavTBbNPHkiLlfQWhyfpjiYCyMfyfOaCHnDkM7N8jeXyBXvN khWzqXusl3mw/W1FdSHQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV1t-00000005ZrY-0hN8; Sat, 27 Jul 2024 00:11:21 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlE-00000005QKl-2oLM for kvm-riscv@lists.infradead.org; Fri, 26 Jul 2024 23:54:10 +0000 Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-7a1188b3bc2so1412751a12.2 for ; Fri, 26 Jul 2024 16:54:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038047; x=1722642847; 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=wxuzXAlWAmTLEMJkRQnxVQGU0NLqznOMJ0954FoR3Ro=; b=K3AZZMXbKStUlpEqnDA7Yiq3IrBAUw5pVtr5jVRsmGK4kEdHN/4REBaAbhTiuUc1cu hfUzFB8ky43XTmSxk1A8bCWtTdof58i29wVaFXQlUL7VZEFiVUfEIYBdPS1adFQxVza5 keAyTpXut2Y5IKgJTatpz1m8PjW44DnpoQzdnb3gRvGuEaJh6lteHEEwHlNIs3LxrNVV L98zuBnVI+i4Lm+bKDRw8gO+Mm1deHKGSU4G1I2EpJgoxoOF0fZYPg38cc9ycnFD7peP Hzs7HkIu5oOsR23yh0f1Jm0datpauiY7TRD9jHu65ag5x0fwRvrdsjU/TcIiAgwuTTE2 Jc6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038047; x=1722642847; 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=wxuzXAlWAmTLEMJkRQnxVQGU0NLqznOMJ0954FoR3Ro=; b=dD+SlXNMrMtcLDK0dspITE4AACBldCj6BXgUQv+vOztVWTaieMWAyWKYwZRv7G9E7f YOPGwNJBvrP/69cTOkoXYDnyE1hQPnLQ7eeIhtonqe7ya1CeAGNg7SVY5RS74YBDuKY/ nvP7ifjrJ9S5akESoAVoHwWLGUcr1pmZpA4wsuCwaalfGorsETpipnSa9Dj6eo/Oss5W vQMH/AhCSz9HbzK67gLygRlex8NsMcniItppJiPf+qNlG5zr1YIs4dX9GJl0XrdQg5yi sRZM17tmKWA+4N9LscvFR3FZgV7tTPXi1vtPCj16xip4WnbqQs3awxraCA7IfqBkc5O9 vxSw== X-Forwarded-Encrypted: i=1; AJvYcCWMsEoXrSoypXtgFdLBWEAuUiDI+mBaRudxKCQvegSNcarBVCkCRiZMNYGyWys4h31Sru6gHuwSAv4EXJ2wE+J3j6H5tcQVsZec7BR5vQ== X-Gm-Message-State: AOJu0YzhftRcArXV2nSLS/uXn0YDbHzMWx8xxQBqMguI/1KKzFOja1yZ WJ5/HMHzu7WX++r1cUNhfcc1ezBT2gtkv9d9Pc6O1MZ9BY5pa52bjGg4R9VVFoxpAXBQo+A3gi4 5Zg== X-Google-Smtp-Source: AGHT+IHHkDF9jDFipyHQqfPDWUmaQYRR7lKWyDIChzQG4y5VknOXhGJqf1L9qSRAYqKwiuxxpki7u8BCvjE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:141f:0:b0:6e5:ef07:5922 with SMTP id 41be03b00d2f7-7ac8d9d818bmr4158a12.1.1722038046008; Fri, 26 Jul 2024 16:54:06 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:52 -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-44-seanjc@google.com> Subject: [PATCH v12 43/84] KVM: Add kvm_faultin_pfn() to specifically service guest page faults 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_165408_766131_991E6531 X-CRM114-Status: GOOD ( 12.28 ) X-Spam-Score: -9.5 (---------) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Add a new dedicated API, kvm_faultin_pfn(), for servicing guest page faults, i.e. for getting pages/pfns that will be mapped into the guest via an mmu_notifier-protected KVM MMU. Keep struct kvm_follo [...] Content analysis details: (-9.5 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:549 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM welcome-list 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.0 DKIMWL_WL_MED DKIMwl.org - Medium trust sender X-BeenThere: kvm-riscv@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: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Add a new dedicated API, kvm_faultin_pfn(), for servicing guest page faults, i.e. for getting pages/pfns that will be mapped into the guest via an mmu_notifier-protected KVM MMU. Keep struct kvm_follow_pfn buried in internal code, as having __kvm_faultin_pfn() take "out" params is actually cleaner for several architectures, e.g. it allows the caller to have its own "page fault" structure without having to marshal data to/from kvm_follow_pfn. Long term, common KVM would ideally provide a kvm_page_fault structure, a la x86's struct of the same name. But all architectures need to be converted to a common API before that can happen. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 11 +++++++++++ virt/kvm/kvm_main.c | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index ef0277b77375..e0548ae92659 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1217,6 +1217,17 @@ 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 __kvm_faultin_pfn(const struct kvm_memory_slot *slot, gfn_t gfn, + unsigned int foll, bool *writable, + struct page **refcounted_page); + +static inline kvm_pfn_t kvm_faultin_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, + bool write, bool *writable, + struct page **refcounted_page) +{ + return __kvm_faultin_pfn(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, + write ? FOLL_WRITE : 0, writable, refcounted_page); +} 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, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ad84dab8c5dc..6dc448602751 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3134,6 +3134,28 @@ kvm_pfn_t kvm_lookup_pfn(struct kvm *kvm, gfn_t gfn) return pfn; } +kvm_pfn_t __kvm_faultin_pfn(const struct kvm_memory_slot *slot, gfn_t gfn, + unsigned int foll, bool *writable, + struct page **refcounted_page) +{ + struct kvm_follow_pfn kfp = { + .slot = slot, + .gfn = gfn, + .flags = foll, + .map_writable = writable, + .refcounted_page = refcounted_page, + }; + + if (WARN_ON_ONCE(!writable || !refcounted_page)) + return KVM_PFN_ERR_FAULT; + + *writable = false; + *refcounted_page = NULL; + + return kvm_follow_pfn(&kfp); +} +EXPORT_SYMBOL_GPL(__kvm_faultin_pfn); + int kvm_prefetch_pages(struct kvm_memory_slot *slot, gfn_t gfn, struct page **pages, int nr_pages) {