From mboxrd@z Thu Jan 1 00:00:00 1970 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 2864AC83F17 for ; Tue, 15 Jul 2025 09:34:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B0DA26B00AB; Tue, 15 Jul 2025 05:34:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ABE256B00AC; Tue, 15 Jul 2025 05:34:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 986C46B00AD; Tue, 15 Jul 2025 05:34:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 851506B00AB for ; Tue, 15 Jul 2025 05:34:27 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 124AE1D9F92 for ; Tue, 15 Jul 2025 09:34:27 +0000 (UTC) X-FDA: 83665988574.23.41102A5 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf24.hostedemail.com (Postfix) with ESMTP id 2FE22180006 for ; Tue, 15 Jul 2025 09:34:24 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nV5yg8sE; spf=pass (imf24.hostedemail.com: domain of 3nyB2aAUKCAQxeffeksskpi.gsqpmry1-qqozego.svk@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3nyB2aAUKCAQxeffeksskpi.gsqpmry1-qqozego.svk@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752572065; 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=0x5SEsp+wQeAofoXD9kRa6NgpqrTyhovX0ubFwE0gGE=; b=gyw17PV1l1LYJ2Ge+nOYSdPCj+24GMoPi4rznPbthOdhp9NU5Mv8PZNXKu5xMUO3ItEUDC Wv5IQ5G1oAmf9vNASnTjMnrRvJv5sRWfSJk144zovtvd6qAQUHJeihMnbB/OMXTtQlBHyh T1Zl4yBk4WTIQH5SPvORFkF4L5b0Jm4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752572065; a=rsa-sha256; cv=none; b=kYqG2Bkpq+w3zgYLj0GPfaes/EaXvjq7OATHzinIQ7zVw8K4Pt0o3A2S+zPhDfH34jvC2H +T/7VXDnlp86qya3BPcWyTngwiTWIzxDO/gDuKA/1i5blYHngx8T/Y8xLwb5ZNhTm17UNu NcejJJbdK7sdFBkwVR40yppgVVFLhBU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nV5yg8sE; spf=pass (imf24.hostedemail.com: domain of 3nyB2aAUKCAQxeffeksskpi.gsqpmry1-qqozego.svk@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3nyB2aAUKCAQxeffeksskpi.gsqpmry1-qqozego.svk@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-454dee17a91so40266385e9.3 for ; Tue, 15 Jul 2025 02:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752572064; x=1753176864; 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=0x5SEsp+wQeAofoXD9kRa6NgpqrTyhovX0ubFwE0gGE=; b=nV5yg8sEM+8WiygXHlHXR/4G0SO4IyxpJ12fc+KLjaEZCneWCSIaVBFNhz0iJquUot iJL2uYgWnjRpcvnZtegDOzCJKB8uEOx5EREgLhd7xxRtFbfUOMU8kqKc/fb0XRgYHoPb 7JTdZzB2AvosD3vRfBwHcr7kT/lV2VnGjH33gcHBcOH5VWzgwQX9gqBLrDdR8Jo8Xwbw R9ZeBCBI3C1hr0ym5jBtLa8dhAquwfIAmqwb8Ij6g83+iU6dNs/z5Fu8do717u8JilhL tl8rNl2Z4QcnRZ/gYbxASbrm5RpPjdUwA37LTD1RHlL/A8LIAVWFXEUvk05Ft/Licd2e 0zpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752572064; x=1753176864; 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=0x5SEsp+wQeAofoXD9kRa6NgpqrTyhovX0ubFwE0gGE=; b=RKEFh7yNz8GKAll4z+PflJbjjTepcFao2lY/yYUX2R1WQVnIpdvIsstMXcsQN1SpwH aysjNsdoszrGwMoXccLRbcaBlrrD5E5oZxfsfGjvLHD+5BlyiK6dOuw7vjWVdYZ3bZyY DjHB7ptb3jKw7G7WHogYAWVFuLioIuPvgcCYvi+S9oZgWcvfY+j1Q0tGkHHb9sfBjn/G ONANYmEWmnWFw5aYuknxcnFZNpyAWQG485eFtVxv9sQvqRTnXNHXCfUW1moMBS/I7tYe uMeYFddzQpkUDdTjA1RwrOYfZfRC+mj/SIbSS9uEK8gzquo2c6NASYmGZaEdwRBYyDji jKjA== X-Forwarded-Encrypted: i=1; AJvYcCVxljl8bQgk1T819/XOQ4VdkBqfH1zvHkjKog2KvauE+sBTcTilUOll9MP0ROg9yOQfYA/nNevmuw==@kvack.org X-Gm-Message-State: AOJu0Yw1vZAbNhjmIFdyH36lV6coDr1zFWbNto8RH1DXB6Q1PxbnFSMA 0nXOtlAMccdhn1c+EeM5Zpye4OavTSHUmSanvRGYWb26AksTVUGzCrb6U8QZ308v8uNmqopiFaG fcw== X-Google-Smtp-Source: AGHT+IETce9zW/LrnBQzC3dja+JnEMeftry72HzwDnDzdO2fU3uPKPspLyKTgMqcU0MlFHJYIBST8urFAw== X-Received: from wmcq10.prod.google.com ([2002:a05:600c:c10a:b0:456:2437:14d6]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:4029:b0:3b6:463:d886 with SMTP id ffacd0b85a97d-3b60463dc8fmr5591019f8f.20.1752572063764; Tue, 15 Jul 2025 02:34:23 -0700 (PDT) Date: Tue, 15 Jul 2025 10:33:44 +0100 In-Reply-To: <20250715093350.2584932-1-tabba@google.com> Mime-Version: 1.0 References: <20250715093350.2584932-1-tabba@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250715093350.2584932-16-tabba@google.com> Subject: [PATCH v14 15/21] KVM: arm64: Refactor user_mem_abort() From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org, kvmarm@lists.linux.dev 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, 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, peterx@redhat.com, pankaj.gupta@amd.com, ira.weiny@intel.com, tabba@google.com Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 2FE22180006 X-Stat-Signature: d1sbbxj8ifzsmb69rpn3rn3ryfn8rwcp X-Rspam-User: X-HE-Tag: 1752572064-7379 X-HE-Meta: U2FsdGVkX1+ht94NhfOTKFxbBus55Bx5MUaoenJIx9SbqUYw3QTY5AmirH/6J5fluJdFkP9Yrj+thFCsiyh5zmcZEptfnyGROEjmoIj20hK6TmvUhFoM/6CMjkKChxeL2NO7pns8S/xPjeWObxkODGIEGC9tcGZe4a5dO3/DmO2xmmPwCvwX+j44KnmRq02pme9a6wE3ZhQPB0AjIt6CyiPCXTD6beXBlnuq6UfHPaMz6R5ycZrrWNWn8DU0ZjO10ErTlPKH0Ljo1Dedo77OjvehfU4SxbSKiOcxSucmtHepP/XSPoGnWkqA2uKGvE6H2CXQDeE5AWgfno1xQbHn1KGNkew9nDEjD4hpIvF+Lmox3wZ3jwDZJXjYaxSsUtzdR/7lUs4HCN0MULzbZ4Vq/OADvU3DJDI/pVwzzhMju+naEeTMikiuHJVYLz2AoodQFXctNXv7T/T9xuMRtc/QOKsQOHJIaYYkL7RL3MwYWAF4dC0LimJzAKoWPn7qCodvD0HQ+olKMIzAIiPeXaZDx1hracLNNVlJmIji59sb5Q1zvLLX5F6FFLx69Fcx/NoTwtVCTfjxnt82NpN25U52ED9h/LKxFbbsCSf9GF1ZT3R/q3qhgab+6c+ZA6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Ham, tests=bogofilter, 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: Refactor user_mem_abort() to improve code clarity and simplify assumptions within the function. Key changes include: * Immediately set force_pte to true at the beginning of the function if logging_active is true. This simplifies the flow and makes the condition for forcing a PTE more explicit. * Remove the misleading comment stating that logging_active is guaranteed to never be true for VM_PFNMAP memslots, as this assertion is not entirely correct. * Extract reusable code blocks into new helper functions: * prepare_mmu_memcache(): Encapsulates the logic for preparing and topping up the MMU page cache. * adjust_nested_fault_perms(): Isolates the adjustments to shadow S2 permissions and the encoding of nested translation levels. * Update min(a, (long)b) to min_t(long, a, b) for better type safety and consistency. * Perform other minor tidying up of the code. These changes primarily aim to simplify user_mem_abort() and make its logic easier to understand and maintain, setting the stage for future modifications. Reviewed-by: Gavin Shan Reviewed-by: Marc Zyngier Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 110 +++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 51 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 2942ec92c5a4..b3eacb400fab 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1470,13 +1470,56 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) return vma->vm_flags & VM_MTE_ALLOWED; } +static int prepare_mmu_memcache(struct kvm_vcpu *vcpu, bool topup_memcache, + void **memcache) +{ + int min_pages; + + if (!is_protected_kvm_enabled()) + *memcache = &vcpu->arch.mmu_page_cache; + else + *memcache = &vcpu->arch.pkvm_memcache; + + if (!topup_memcache) + return 0; + + min_pages = kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu); + + if (!is_protected_kvm_enabled()) + return kvm_mmu_topup_memory_cache(*memcache, min_pages); + + return topup_hyp_memcache(*memcache, min_pages); +} + +/* + * Potentially reduce shadow S2 permissions to match the guest's own S2. For + * exec faults, we'd only reach this point if the guest actually allowed it (see + * kvm_s2_handle_perm_fault). + * + * Also encode the level of the original translation in the SW bits of the leaf + * entry as a proxy for the span of that translation. This will be retrieved on + * TLB invalidation from the guest and used to limit the invalidation scope if a + * TTL hint or a range isn't provided. + */ +static void adjust_nested_fault_perms(struct kvm_s2_trans *nested, + enum kvm_pgtable_prot *prot, + bool *writable) +{ + *writable &= kvm_s2_trans_writable(nested); + if (!kvm_s2_trans_readable(nested)) + *prot &= ~KVM_PGTABLE_PROT_R; + + *prot |= kvm_encode_nested_level(nested); +} + 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, bool fault_is_perm) { int ret = 0; - bool write_fault, writable, force_pte = false; + bool topup_memcache; + bool write_fault, writable; bool exec_fault, mte_allowed; bool device = false, vfio_allow_any_uc = false; unsigned long mmu_seq; @@ -1488,6 +1531,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, gfn_t gfn; kvm_pfn_t pfn; bool logging_active = memslot_is_logging(memslot); + bool force_pte = logging_active; long vma_pagesize, fault_granule; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; @@ -1498,17 +1542,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, 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); - VM_BUG_ON(write_fault && exec_fault); - - if (fault_is_perm && !write_fault && !exec_fault) { - kvm_err("Unexpected L2 read permission error\n"); - return -EFAULT; - } - - if (!is_protected_kvm_enabled()) - memcache = &vcpu->arch.mmu_page_cache; - else - memcache = &vcpu->arch.pkvm_memcache; + VM_WARN_ON_ONCE(write_fault && exec_fault); /* * Permission faults just need to update the existing leaf entry, @@ -1516,17 +1550,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, * only exception to this is when dirty logging is enabled at runtime * and a write fault needs to collapse a block entry into a table. */ - if (!fault_is_perm || (logging_active && write_fault)) { - int min_pages = kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu); - - if (!is_protected_kvm_enabled()) - ret = kvm_mmu_topup_memory_cache(memcache, min_pages); - else - ret = topup_hyp_memcache(memcache, min_pages); - - if (ret) - return ret; - } + topup_memcache = !fault_is_perm || (logging_active && write_fault); + ret = prepare_mmu_memcache(vcpu, topup_memcache, &memcache); + if (ret) + return ret; /* * Let's check if we will get back a huge page backed by hugetlbfs, or @@ -1540,16 +1567,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, return -EFAULT; } - /* - * logging_active is guaranteed to never be true for VM_PFNMAP - * memslots. - */ - if (logging_active) { - force_pte = true; + if (force_pte) vma_shift = PAGE_SHIFT; - } else { + else vma_shift = get_vma_page_shift(vma, hva); - } switch (vma_shift) { #ifndef __PAGETABLE_PMD_FOLDED @@ -1601,7 +1622,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, max_map_size = PAGE_SIZE; force_pte = (max_map_size == PAGE_SIZE); - vma_pagesize = min(vma_pagesize, (long)max_map_size); + vma_pagesize = min_t(long, vma_pagesize, max_map_size); } /* @@ -1630,7 +1651,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs * with the smp_wmb() in kvm_mmu_invalidate_end(). */ - mmu_seq = vcpu->kvm->mmu_invalidate_seq; + mmu_seq = kvm->mmu_invalidate_seq; mmap_read_unlock(current->mm); pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0, @@ -1665,24 +1686,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, if (exec_fault && device) return -ENOEXEC; - /* - * Potentially reduce shadow S2 permissions to match the guest's own - * S2. For exec faults, we'd only reach this point if the guest - * actually allowed it (see kvm_s2_handle_perm_fault). - * - * Also encode the level of the original translation in the SW bits - * of the leaf entry as a proxy for the span of that translation. - * This will be retrieved on TLB invalidation from the guest and - * used to limit the invalidation scope if a TTL hint or a range - * isn't provided. - */ - if (nested) { - writable &= kvm_s2_trans_writable(nested); - if (!kvm_s2_trans_readable(nested)) - prot &= ~KVM_PGTABLE_PROT_R; - - prot |= kvm_encode_nested_level(nested); - } + if (nested) + adjust_nested_fault_perms(nested, &prot, &writable); kvm_fault_lock(kvm); pgt = vcpu->arch.hw_mmu->pgt; @@ -1953,6 +1958,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) goto out_unlock; } + VM_WARN_ON_ONCE(kvm_vcpu_trap_is_permission_fault(vcpu) && + !write_fault && !kvm_vcpu_trap_is_exec_fault(vcpu)); + ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, hva, esr_fsc_is_permission_fault(esr)); if (ret == 0) -- 2.50.0.727.gbf7dc18ff4-goog