From patchwork Wed Jan 29 17:23:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13953972 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CC01C0218D for ; Wed, 29 Jan 2025 17:23:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B71A28025F; Wed, 29 Jan 2025 12:23:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 73DDC280067; Wed, 29 Jan 2025 12:23:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 540B828025F; Wed, 29 Jan 2025 12:23:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 341E4280067 for ; Wed, 29 Jan 2025 12:23:43 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E53EE1A0F81 for ; Wed, 29 Jan 2025 17:23:42 +0000 (UTC) X-FDA: 83061161484.16.A5CC362 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf12.hostedemail.com (Postfix) with ESMTP id 16ABA4000C for ; Wed, 29 Jan 2025 17:23:40 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2CSQTa8V; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3G2SaZwUKCJADuvvu08805y.w86527EH-664Fuw4.8B0@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3G2SaZwUKCJADuvvu08805y.w86527EH-664Fuw4.8B0@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738171421; a=rsa-sha256; cv=none; b=C+etThlz2LndaGfG0V+P4ExyWVJ87GqJDQ5QE8bGhAP4ajvJoDGXXlEBZWiPCQ/T6A6Gyn 7ywnF5UFiecClQ/mcX04XZ80D/xCaXBZmpbHMX1Y8hBIV95u9ArHOfMqkVRVNZOK+8XdIc gj6oN9ckUfp6wyR7F2izQm9wUoJJM6U= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2CSQTa8V; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3G2SaZwUKCJADuvvu08805y.w86527EH-664Fuw4.8B0@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3G2SaZwUKCJADuvvu08805y.w86527EH-664Fuw4.8B0@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738171421; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gJUlshLqVqxF/x1Q6yciZ2xZ0kPW08W616puHCRAMyA=; b=4oovlwiyZhl2N3opObJ5HaynN0FjHD0uEGu4j7WAgTThZE3yZaGljOuvaFm2aML9o/dycW jHFv/7GVPqPq9z2Ju4+2X6HiL9M3sUE9BUQDZezn/0vXDFKS+sUrL5ldPoGnLpsrckc0My YekMgUFaNzeXdgzS6vyiD3xP2z+M7RE= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-38a684a096eso3189044f8f.2 for ; Wed, 29 Jan 2025 09:23:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738171420; x=1738776220; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=gJUlshLqVqxF/x1Q6yciZ2xZ0kPW08W616puHCRAMyA=; b=2CSQTa8V6vRzTP3ZluCAk42ujoMRiIV84Ua7JTQQwhBJZyp4PqO0kN+PHlZeTbNtrp aIPaZm5oMdvzczk8ezsWQjLdYyMBgjusHtYdHcIX/QWNn2mVz6KsBJV78D5tZ7kw5jvM e2jIED1Himkj/vuYwuDS2ZUoOlTYNxQG8f4HCK7GiOscI4aRG138KU8P5Cc2RGCWp04n v+BHJgK+J5RxiuHPv/k7EtfINKLW1hdLZdei8TxsKCKb07jhZ/x/vrvOikFTKbRshVQK 7fB2Gza3HpEzYWYFPems+Y4D1OTB/F0DLg3mzh9U69+uO3PktuGNyBVI68gFXxDuqWAg BztQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738171420; x=1738776220; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gJUlshLqVqxF/x1Q6yciZ2xZ0kPW08W616puHCRAMyA=; b=EFY/sCZbzul1TPb9dNQt/EMV/zXbVeBZCpNa+U8oUeQ6RUc3ha5oANxWNGk0+Crq1n YZBRDwgZpgrDoNzOYhwXYskhYxaP8J36q/ThYzncHaM9qf/lE3MSGjwZ3mSp8zc8Eo95 lLRvZtDDZWMBmPv7H8RGhvUgB+Im7j2HEp5J8z3lWt7e+UNw5MMLR02byqnRt42yl58O xWhvWc+YKulS/MkTDHVH7QttZyBpzBycnWysO0peWemKVCsoZdybl7ntqTnC76OPrpjU HdfsX2nBsW9GIiQvAddsiBOoVN45cPXimny8N7B9yDReWVkYMT+2Yp0op86T0R5Cqgu9 RPXQ== X-Forwarded-Encrypted: i=1; AJvYcCUoWPHIBBSrSE8Fn0BP56GjZzIOvUomNYettmO1KEFj/mxm9jjNZ9qTZVPVhdf9UNiBeuN46na0oQ==@kvack.org X-Gm-Message-State: AOJu0YxvVxgm2a2EXc/PcRGbfLGQRRiFGKw0i+BqIBrLU2a8AReaOPn9 wRfz1D6TDmX+zkwZ26a+q2mECjn/U0Qi7w2VYKjN8aezX5aXQ7ssdTGC7ak/s8t7iVPUQgoBiQ= = X-Google-Smtp-Source: AGHT+IFusRcrVc4/78hZx6Io2rOdbnPNNEy+1FtewyjP5IWPByZvcuY2dAJE2DcSV4SFz55Z5sZ6LOwVmQ== X-Received: from wmgg8.prod.google.com ([2002:a05:600d:8:b0:436:e67f:5b4c]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:adf:fa11:0:b0:385:e8aa:2a4e with SMTP id ffacd0b85a97d-38c51b87e3fmr2896212f8f.31.1738171419742; Wed, 29 Jan 2025 09:23:39 -0800 (PST) Date: Wed, 29 Jan 2025 17:23:17 +0000 In-Reply-To: <20250129172320.950523-1-tabba@google.com> Mime-Version: 1.0 References: <20250129172320.950523-1-tabba@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129172320.950523-9-tabba@google.com> Subject: [RFC PATCH v2 08/11] KVM: arm64: Handle guest_memfd()-backed guest page faults From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspam-User: X-Rspamd-Queue-Id: 16ABA4000C X-Rspamd-Server: rspam10 X-Stat-Signature: ckdqp1193iuepc3c4rgo88skkffqiyqq X-HE-Tag: 1738171420-974651 X-HE-Meta: U2FsdGVkX1/FmWvYELuUpQi3M6CChGS8mkyCcNb9oXqsx34JMwbcqHezPkk/EyvsREen+TDZif8QG6CbMzq3IpoM4VDVIUeDgY5hn4v507GwWXDPHgRVotGQKGkNqU5IF+CL3hoYIzbAlONo7w/o+hkAq6bz3x5p8K0dktjmzArgsUrW7eP9wyX0oC3LPbhKpb2RhRYEZvj6uRaMuz/rhcWRAE2S1gOEV3D2AWJqLbhpPedOVZ2M3wSn5holwx1Oq/vCftAcpDNmiIzJ19VobSA7YrlZu0O/N7VXokpKP9UY8fzcPeqwNi54OoeT3Ak4JVdNI4vXLJKUGblQouvKrf4BUECDh4urqTAYZyIkVtf4kDVzZ2JZA0uDET5d4XZVUOCcyz+TnpMJdn1u+fNaqfRppjSgC4fbgX9Anxx0Fc4BXzGyV1MrFSxI6jiroBuaeiHdC4a8RS8EHw7E0FTwosNSsq7LppT3iJxibzeh5BXjwKQhQ7eJ3ks2Q0e/Ek+tJCNNj7lVcFvuAWrrxlMSYxu92S02gw62a2B34PNU3S6pXlmDqjqvlVe4c/gdszoLU1hchklaYWK02ShbdrAk3kN5MKV/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, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add arm64 support for handling guest page faults on guest_memfd backed memslots. For now, the fault granule is restricted to PAGE_SIZE. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 84 ++++++++++++++++++++++++++-------------- include/linux/kvm_host.h | 5 +++ virt/kvm/kvm_main.c | 5 --- 3 files changed, 61 insertions(+), 33 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 1ec362d0d093..c1f3ddb88cb9 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1430,6 +1430,33 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) return vma->vm_flags & VM_MTE_ALLOWED; } +static kvm_pfn_t faultin_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, bool write_fault, bool *writable, + struct page **page, bool is_private) +{ + kvm_pfn_t pfn; + int ret; + + if (!is_private) + return __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page); + + *writable = false; + + if (WARN_ON_ONCE(write_fault && memslot_is_readonly(slot))) + return KVM_PFN_ERR_NOSLOT_MASK; + + ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL); + if (!ret) { + *writable = write_fault; + return pfn; + } + + if (ret == -EHWPOISON) + return KVM_PFN_ERR_HWPOISON; + + return KVM_PFN_ERR_NOSLOT_MASK; +} + static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, struct kvm_s2_trans *nested, struct kvm_memory_slot *memslot, unsigned long hva, @@ -1437,24 +1464,25 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, { int ret = 0; bool write_fault, writable; - bool exec_fault, mte_allowed; + bool exec_fault, mte_allowed = false; bool device = false, vfio_allow_any_uc = false; unsigned long mmu_seq; phys_addr_t ipa = fault_ipa; struct kvm *kvm = vcpu->kvm; struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; - struct vm_area_struct *vma; + struct vm_area_struct *vma = NULL; short vma_shift; - gfn_t gfn; + gfn_t gfn = ipa >> PAGE_SHIFT; kvm_pfn_t pfn; bool logging_active = memslot_is_logging(memslot); - bool force_pte = logging_active; - long vma_pagesize, fault_granule; + bool is_private = kvm_mem_is_private(kvm, gfn); + bool force_pte = logging_active || is_private; + long vma_pagesize, fault_granule = PAGE_SIZE; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; struct page *page; - if (fault_is_perm) + if (fault_is_perm && !is_private) fault_granule = kvm_vcpu_trap_get_perm_fault_granule(vcpu); write_fault = kvm_is_write_fault(vcpu); exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu); @@ -1478,24 +1506,30 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, return ret; } + mmap_read_lock(current->mm); + /* * Let's check if we will get back a huge page backed by hugetlbfs, or * get block mapping for device MMIO region. */ - mmap_read_lock(current->mm); - vma = vma_lookup(current->mm, hva); - if (unlikely(!vma)) { - kvm_err("Failed to find VMA for hva 0x%lx\n", hva); - mmap_read_unlock(current->mm); - return -EFAULT; - } + if (!is_private) { + vma = vma_lookup(current->mm, hva); + if (unlikely(!vma)) { + kvm_err("Failed to find VMA for hva 0x%lx\n", hva); + mmap_read_unlock(current->mm); + return -EFAULT; + } - /* - * logging_active is guaranteed to never be true for VM_PFNMAP - * memslots. - */ - if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP))) - return -EFAULT; + /* + * logging_active is guaranteed to never be true for VM_PFNMAP + * memslots. + */ + if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP))) + return -EFAULT; + + vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED; + mte_allowed = kvm_vma_mte_allowed(vma); + } if (force_pte) vma_shift = PAGE_SHIFT; @@ -1565,18 +1599,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, ipa &= ~(vma_pagesize - 1); } - gfn = ipa >> PAGE_SHIFT; - mte_allowed = kvm_vma_mte_allowed(vma); - - vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED; - /* Don't use the VMA after the unlock -- it may have vanished */ vma = NULL; /* * Read mmu_invalidate_seq so that KVM can detect if the results of - * vma_lookup() or __kvm_faultin_pfn() become stale prior to - * acquiring kvm->mmu_lock. + * vma_lookup() or faultin_pfn() become stale prior to acquiring + * kvm->mmu_lock. * * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs * with the smp_wmb() in kvm_mmu_invalidate_end(). @@ -1584,8 +1613,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, mmu_seq = vcpu->kvm->mmu_invalidate_seq; mmap_read_unlock(current->mm); - pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0, - &writable, &page); + pfn = faultin_pfn(kvm, memslot, gfn, write_fault, &writable, &page, is_private); if (pfn == KVM_PFN_ERR_HWPOISON) { kvm_send_hwpoison_signal(hva, vma_shift); return 0; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index e57cdf4e3f3f..42397fc4a1fb 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1864,6 +1864,11 @@ static inline int memslot_id(struct kvm *kvm, gfn_t gfn) return gfn_to_memslot(kvm, gfn)->id; } +static inline bool memslot_is_readonly(const struct kvm_memory_slot *slot) +{ + return slot->flags & KVM_MEM_READONLY; +} + static inline gfn_t hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot) { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 40e4ed512923..9f913a7fc44b 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2622,11 +2622,6 @@ unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn) return size; } -static bool memslot_is_readonly(const struct kvm_memory_slot *slot) -{ - return slot->flags & KVM_MEM_READONLY; -} - static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t gfn, gfn_t *nr_pages, bool write) {