From patchwork Mon Dec 18 13:58:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180448 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1263498dyi; Mon, 18 Dec 2023 06:06:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6nBuOKGi9qF0wQeRuqMGmG46XagOMWTpB79KHv8fBBR7yAKbBEof3hfiG+sRpKkw5DOeQ X-Received: by 2002:a17:906:71c9:b0:a23:6d49:f09a with SMTP id i9-20020a17090671c900b00a236d49f09amr75094ejk.50.1702908409076; Mon, 18 Dec 2023 06:06:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908409; cv=none; d=google.com; s=arc-20160816; b=kKeqmhpPK+OeBhfl3b6GM1BtLbIBCtUWI0DHgUO9Q63+g36FbpYgtIAeMCqI46q4gu FkScS2TzNx3jS8E27g12EDZwqCIZxA8dbnJQOooLytbj77oEGxmXlOh1N7muyQZcPqDO iL01f02HILvjoi9klJ2FJH7n4pN8OPDtNvYZV31OEtswire/jTIXmdAFmYXPzpKZa3Di k7MNfkoFi9QUSUKuJ3ZHLawLtxkPc4/sVtnISXrJwbNTrHdjJyBe7ZPSKfaxresbN8rr 8m/lygWZjhbUbwdCEzPXoGRlrXqX0MV947/7f4RgtYbHkNX6qsd/SlAYTITOgCnnVDUg /jIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=6k7cehEzikJK7Gdbm4Te2RqGLAqH3crGDKksVzZAStI=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=N6mYi41/7fI/H0iM/x7jyGPCDcFlsej8CaMAnuTWK7jqDvYJtpgMdvSP/tNt1hSnyc 85eRNcktklwOTn9J0cDEr7VN40inCjgWq9Y26eVopfvz+Dq4F5Z605j0i2CcTgoOdsOa HQNi15aodB9ENWIbrq4tI7E3jfgxYoL50NbeoPTWIA+TEt6cd5r/WD6ou2vpGGvb9D3n kkfFCtauw05eruVa/LZeC0pcBlVYVmq3cMl5oBim3PF62iXntlyofRgzduuSkRpOHD8g ljVjEPVNDL1BxXKFv4KeZkDoGiV51wFf0Ay5wz9bU5tXMlyUut8D+OZa9LZMZsTeIXxt wrqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kscIP+Zg; spf=pass (google.com: domain of linux-kernel+bounces-3787-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3787-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id k7-20020a1709061c0700b00a235916049dsi788616ejg.704.2023.12.18.06.06.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:06:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3787-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kscIP+Zg; spf=pass (google.com: domain of linux-kernel+bounces-3787-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3787-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 79A041F25AA4 for ; Mon, 18 Dec 2023 14:00:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6AED11D13D; Mon, 18 Dec 2023 13:59:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kscIP+Zg" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B67041D137 for ; Mon, 18 Dec 2023 13:59:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-40d17446f11so11114535e9.0 for ; Mon, 18 Dec 2023 05:59:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907989; x=1703512789; darn=vger.kernel.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=6k7cehEzikJK7Gdbm4Te2RqGLAqH3crGDKksVzZAStI=; b=kscIP+Zgs/Fde4gk3Ywe8F56MHfj+o2iWqmmG6WTN0SanmoIMaZMhc7smQMnuKZsr0 yTr7FADeCrp33LW7GTzmlueju4+XhKM9t1hkySIjvL+Qeh34USknrP3U2DT8YMJIXui6 eWQpPYcapy/FPsOfewRl2DJidvZPZFb2T68U8225JuJ8684vQoxNDlKB/5WsuIo25+T1 4GipAKmBv3j0IMh3gI+cYaznTPODUeQLCi3o2/HW2W6on9KM6Dh3vm7czGsGQzhDx4n8 cqwYvfP7b+oas6PvgeFug+kTifEnG1rVtJsPZg4A2nM436G4i3ZK9/SyNoLU2Mp6VRg2 OD5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907989; x=1703512789; 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=6k7cehEzikJK7Gdbm4Te2RqGLAqH3crGDKksVzZAStI=; b=B6ezxvy/SfHGdzUvdoWHlY0MBZhX1VKwDWlH7Cm0yItH5SX4ikGHNaXDKI8r+0d/fm o4BDt68A9Tcj1p3grhPtSjCWGSL6tVJ0ZO7Y9fUXTh7709LXu1aQl8tfwnZdNYjpxuNu /MSxqOW9Ayx4wLvhQVS9vzHsmXWNHnln2XRvcqvwyrcw2wJF7WTDGV16MgMieS/foVaf TALc1OwyOEN5rKdIRwb0LhZqisipBDnxgaioty3TblSYBUmxBpYSIT4V0ZiL158L5T+T 2K5TOyUm3qK2N2E+cVaIka+HCNq+KtbqbVNy53p3sBkeWcLGCBSom3A+xXPZg3RlGYsy heQg== X-Gm-Message-State: AOJu0YxxEj+PUFgf+WPWtlB5+MT2gN0vKHrGnWi69S8U5pRt9YKsCXFl g4LJk/eOhZWBIEiSnLgu+4L0dXG0tyWSvqL6ayw= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:600c:1c07:b0:40b:2c9e:e144 with SMTP id j7-20020a05600c1c0700b0040b2c9ee144mr333270wms.1.1702907989006; Mon, 18 Dec 2023 05:59:49 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:51 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-3-sebastianene@google.com> Subject: [PATCH v4 01/10] KVM: arm64: Add snapshot interface for the host stage-2 pagetable From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785628888032738257 X-GMAIL-MSGID: 1785628888032738257 Introduce a new hvc that allows the pKVM hypervisor to snapshot the host stage-2 pagetables. The caller is expected to allocate & free the memory for the pagetable snapshot. The below diagram shows the sequence of events. [--- HOST ---] (1) allocate memory for the snapshot (2) invoke the __pkvm_copy_host_stage2(snapshot) interface |____ [--- HYP ---] (3) donate the snapshot from HOST -> HYP (4) save the host stage-2 pagetables in the snapshot (5) donate the snapshot from HYP -> HOST [--- return from HYP ---] [--- HOST ---] (6) free the memory for the snapshot When the host kernel invokes this interface, the memory is donated from the host to the hypervisor. By doing this the memory is unmapped from the host's translation regime to prevent the host from modifying the buffer while the hypervisor uses it. The hypervisor walks the host stage-2 pagetable copy while re-creating the original mappings. When the copying is done, the memory is donated from the hypervisor back to the host. The pages that have been used from the memcache are donated back to the host from a temporary array. The hvc is executing synchronously and the preemption is disabled for the current CPU while running inside the hypervisor so calling this interface blocks the current CPU until the snapshoting is done. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_pgtable.h | 43 ++++ arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 12 ++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 186 ++++++++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 43 ++++ 6 files changed, 286 insertions(+) diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h index 24b5e6b23..9df3367d8 100644 --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -81,6 +81,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_init_vm, __KVM_HOST_SMCCC_FUNC___pkvm_init_vcpu, __KVM_HOST_SMCCC_FUNC___pkvm_teardown_vm, + __KVM_HOST_SMCCC_FUNC___pkvm_host_stage2_snapshot, }; #define DECLARE_KVM_VHE_SYM(sym) extern char sym[] diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index d3e354bb8..f73efd8a8 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -10,6 +10,7 @@ #include #include #include +#include #define KVM_PGTABLE_MAX_LEVELS 4U @@ -351,6 +352,28 @@ struct kvm_pgtable { kvm_pgtable_force_pte_cb_t force_pte_cb; }; +/** + * struct kvm_pgtable_snapshot - Snapshot page-table wrapper. + * @pgtable: The page-table configuration. + * @mc: Memcache used for pagetable pages allocation. + * @pgd_hva: Host virtual address of a physically contiguous buffer + * used for storing the PGD. + * @pgd_pages: The size of the phyisically contiguous buffer in pages. + * @used_pages_hva: Host virtual address of a physically contiguous buffer + * used for storing the consumed pages from the memcache. + * @num_used_pages The size of the used buffer in pages. + * @used_pages_indx The current index of the used pages array. + */ +struct kvm_pgtable_snapshot { + struct kvm_pgtable pgtable; + struct kvm_hyp_memcache mc; + void *pgd_hva; + size_t pgd_pages; + phys_addr_t *used_pages_hva; + size_t num_used_pages; + size_t used_pages_indx; +}; + /** * kvm_pgtable_hyp_init() - Initialise a hypervisor stage-1 page-table. * @pgt: Uninitialised page-table structure to initialise. @@ -756,4 +779,24 @@ enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte); */ void kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, size_t size); + +#ifdef CONFIG_NVHE_EL2_DEBUG +/** + * kvm_pgtable_stage2_snapshot() - Given a memcache and a destination + * pagetable where we have the original PGD + * copied, build a snapshot table with page table + * pages from the given memcache. + * + * @to_pgt: Destination pagetable + * @mc: The memcache where we allocate the destination pagetables from + */ +int kvm_pgtable_stage2_snapshot(struct kvm_pgtable *to_pgt, + void *mc); +#else +static inline int kvm_pgtable_stage2_snapshot(struct kvm_pgtable *to_pgt, + void *mc) +{ + return -EPERM; +} +#endif /* CONFIG_NVHE_EL2_DEBUG */ #endif /* __ARM64_KVM_PGTABLE_H__ */ diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h index 0972faccc..ca8f76915 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -69,6 +69,7 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages); int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages); int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages); int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages); +int __pkvm_host_stage2_snapshot(struct kvm_pgtable_snapshot *snapshot); bool addr_is_memory(phys_addr_t phys); int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot); diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c index 2385fd03e..7b215245f 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -314,6 +314,17 @@ static void handle___pkvm_teardown_vm(struct kvm_cpu_context *host_ctxt) cpu_reg(host_ctxt, 1) = __pkvm_teardown_vm(handle); } +static void handle___pkvm_host_stage2_snapshot(struct kvm_cpu_context *host_ctxt) +{ +#ifdef CONFIG_NVHE_EL2_DEBUG + DECLARE_REG(struct kvm_pgtable_snapshot *, snapshot_hva, host_ctxt, 1); + + cpu_reg(host_ctxt, 1) = __pkvm_host_stage2_snapshot(snapshot_hva); +#else + cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED; +#endif +} + typedef void (*hcall_t)(struct kvm_cpu_context *); #define HANDLE_FUNC(x) [__KVM_HOST_SMCCC_FUNC_##x] = (hcall_t)handle_##x @@ -348,6 +359,7 @@ static const hcall_t host_hcall[] = { HANDLE_FUNC(__pkvm_init_vm), HANDLE_FUNC(__pkvm_init_vcpu), HANDLE_FUNC(__pkvm_teardown_vm), + HANDLE_FUNC(__pkvm_host_stage2_snapshot), }; static void handle_host_hcall(struct kvm_cpu_context *host_ctxt) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index 8d0a5834e..aaf07f9e1 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -266,6 +266,192 @@ int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd) return 0; } +#ifdef CONFIG_NVHE_EL2_DEBUG +static void *snap_zalloc_page(void *mc) +{ + struct hyp_page *p; + void *addr; + struct kvm_pgtable_snapshot *snap; + phys_addr_t *used_pg; + + snap = container_of(mc, struct kvm_pgtable_snapshot, mc); + used_pg = kern_hyp_va(snap->used_pages_hva); + + /* Check if we have space to track the used page */ + if (snap->used_pages_indx * sizeof(phys_addr_t) > snap->num_used_pages * PAGE_SIZE) + return NULL; + + addr = pop_hyp_memcache(mc, hyp_phys_to_virt); + if (!addr) + return addr; + used_pg[snap->used_pages_indx++] = hyp_virt_to_phys(addr); + + memset(addr, 0, PAGE_SIZE); + p = hyp_virt_to_page(addr); + memset(p, 0, sizeof(*p)); + + return addr; +} + +static void snap_free_pages_exact(void *addr, unsigned long size) +{ + u8 order = get_order(size); + unsigned int i; + struct hyp_page *p; + + for (i = 0; i < (1 << order); i++) { + p = hyp_virt_to_page(addr + (i * PAGE_SIZE)); + hyp_page_ref_dec(p); + } +} + +static void *pkvm_setup_snapshot(struct kvm_pgtable_snapshot *snap_hva) +{ + unsigned long i; + void *pgd, *used_pg; + phys_addr_t mc_page, next_mc_page; + struct kvm_pgtable_snapshot *snap; + + snap = (void *)kern_hyp_va(snap_hva); + if (!PAGE_ALIGNED(snap)) + return NULL; + + if (__pkvm_host_donate_hyp(hyp_virt_to_pfn(snap), 1)) + return NULL; + + pgd = kern_hyp_va(snap->pgd_hva); + if (!PAGE_ALIGNED(pgd)) + goto error_with_snapshot; + + if (__pkvm_host_donate_hyp(hyp_virt_to_pfn(pgd), snap->pgd_pages)) + goto error_with_snapshot; + + mc_page = snap->mc.head; + for (i = 0; i < snap->mc.nr_pages; i++) { + if (!PAGE_ALIGNED(mc_page)) + goto error_with_memcache; + + if (__pkvm_host_donate_hyp(hyp_phys_to_pfn(mc_page), 1)) + goto error_with_memcache; + + mc_page = *((phys_addr_t *)hyp_phys_to_virt(mc_page)); + } + + used_pg = kern_hyp_va(snap->used_pages_hva); + if (!PAGE_ALIGNED(used_pg)) + goto error_with_memcache; + + if (__pkvm_host_donate_hyp(hyp_virt_to_pfn(used_pg), snap->num_used_pages)) + goto error_with_memcache; + + return snap; +error_with_memcache: + mc_page = snap->mc.head; + for (; i >= 0; i--) { + next_mc_page = *((phys_addr_t *)hyp_phys_to_virt(mc_page)); + WARN_ON(__pkvm_hyp_donate_host(hyp_phys_to_pfn(mc_page), 1)); + mc_page = next_mc_page; + } + + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(pgd), snap->pgd_pages)); +error_with_snapshot: + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(snap), 1)); + return NULL; +} + +static void pkvm_teardown_snapshot(struct kvm_pgtable_snapshot *snap) +{ + size_t i; + phys_addr_t mc_page, next_mc_page; + u64 *used_pg = kern_hyp_va(snap->used_pages_hva); + void *pgd = kern_hyp_va(snap->pgd_hva); + + for (i = 0; i < snap->used_pages_indx; i++) { + mc_page = used_pg[i]; + WARN_ON(__pkvm_hyp_donate_host(hyp_phys_to_pfn(mc_page), 1)); + } + + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(used_pg), + snap->num_used_pages)); + + mc_page = snap->mc.head; + for (i = 0; i < snap->mc.nr_pages; i++) { + next_mc_page = *((phys_addr_t *)hyp_phys_to_virt(mc_page)); + WARN_ON(__pkvm_hyp_donate_host(hyp_phys_to_pfn(mc_page), 1)); + mc_page = next_mc_page; + } + + snap->pgtable.mm_ops = NULL; + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(pgd), snap->pgd_pages)); + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(snap), 1)); +} + +static int pkvm_host_stage2_snapshot(struct kvm_pgtable_snapshot *snap) +{ + int ret; + void *pgd; + size_t required_pgd_len; + struct kvm_pgtable_mm_ops mm_ops = {0}; + struct kvm_s2_mmu *mmu; + struct kvm_pgtable *to_pgt, *from_pgt; + + if (snap->used_pages_indx != 0) + return -EINVAL; + + from_pgt = &host_mmu.pgt; + mmu = &host_mmu.arch.mmu; + required_pgd_len = kvm_pgtable_stage2_pgd_size(mmu->vtcr); + if (snap->pgd_pages < (required_pgd_len >> PAGE_SHIFT)) + return -ENOMEM; + + if (snap->mc.nr_pages < host_s2_pgtable_pages()) + return -ENOMEM; + + to_pgt = &snap->pgtable; + pgd = kern_hyp_va(snap->pgd_hva); + + mm_ops.zalloc_page = snap_zalloc_page; + mm_ops.free_pages_exact = snap_free_pages_exact; + mm_ops.phys_to_virt = hyp_phys_to_virt; + mm_ops.virt_to_phys = hyp_virt_to_phys; + mm_ops.page_count = hyp_page_count; + + to_pgt->ia_bits = from_pgt->ia_bits; + to_pgt->start_level = from_pgt->start_level; + to_pgt->flags = from_pgt->flags; + to_pgt->mm_ops = &mm_ops; + + host_lock_component(); + + to_pgt->pgd = pgd; + memcpy(to_pgt->pgd, from_pgt->pgd, required_pgd_len); + ret = kvm_pgtable_stage2_snapshot(to_pgt, &snap->mc); + + host_unlock_component(); + + return ret; +} + +int __pkvm_host_stage2_snapshot(struct kvm_pgtable_snapshot *snap_hva) +{ + int ret; + struct kvm_pgtable_snapshot *snap; + kvm_pteref_t pgd; + + snap = pkvm_setup_snapshot(snap_hva); + if (!snap) + return -EPERM; + + ret = pkvm_host_stage2_snapshot(snap); + if (!ret) { + pgd = snap->pgtable.pgd; + snap->pgtable.pgd = (kvm_pteref_t)__hyp_pa(pgd); + } + pkvm_teardown_snapshot(snap); + return ret; +} +#endif /* CONFIG_NVHE_EL2_DEBUG */ + void reclaim_guest_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc) { void *addr; diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 1966fdee7..82fef9620 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -1598,3 +1598,46 @@ void kvm_pgtable_stage2_free_unlinked(struct kvm_pgtable_mm_ops *mm_ops, void *p WARN_ON(mm_ops->page_count(pgtable) != 1); mm_ops->put_page(pgtable); } + +#ifdef CONFIG_NVHE_EL2_DEBUG +static int snapshot_walker(const struct kvm_pgtable_visit_ctx *ctx, + enum kvm_pgtable_walk_flags visit) +{ + struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops; + void *copy_table, *original_addr; + kvm_pte_t new = ctx->old; + + if (!stage2_pte_is_counted(ctx->old)) + return 0; + + if (kvm_pte_table(ctx->old, ctx->level)) { + copy_table = mm_ops->zalloc_page(ctx->arg); + if (!copy_table) + return -ENOMEM; + + original_addr = kvm_pte_follow(ctx->old, mm_ops); + + memcpy(copy_table, original_addr, PAGE_SIZE); + new = kvm_init_table_pte(copy_table, mm_ops); + } + + *ctx->ptep = new; + + return 0; +} + +int kvm_pgtable_stage2_snapshot(struct kvm_pgtable *to_pgt, void *mc) +{ + struct kvm_pgtable_walker walker = { + .cb = snapshot_walker, + .flags = KVM_PGTABLE_WALK_LEAF | + KVM_PGTABLE_WALK_TABLE_PRE, + .arg = mc + }; + + if (!to_pgt->pgd) + return -ENOMEM; + + return kvm_pgtable_walk(to_pgt, 0, BIT(to_pgt->ia_bits), &walker); +} +#endif /* CONFIG_NVHE_EL2_DEBUG */ From patchwork Mon Dec 18 13:58:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180439 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1257206dyi; Mon, 18 Dec 2023 06:00:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IH4w5bkaG+f/WZMRQvrWbrQ+T0HR6WjQ9F4TEk3wbUmr6+tBl9wVyvQwkwTq/ialb/bi40N X-Received: by 2002:a17:903:234e:b0:1d0:9201:5340 with SMTP id c14-20020a170903234e00b001d092015340mr19829123plh.33.1702908037532; Mon, 18 Dec 2023 06:00:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908037; cv=none; d=google.com; s=arc-20160816; b=d/UAWyfGOi3hvG48MTsLXJOCnpl5LTv/7EdJZgVU+Gn92kl4kJkCnkK1GpAcDUMSrt U/1/KtbSb7EQtdMNRsZLf7OcjEtEolHLliTMKrY7A1+2nQXYPcro9mnMKE57FohvqYuz o+2gNXa6X8cFEIQFRdsN33xoDjWwIdGub1h79ufh96mi7qFC1zuwYSG1SIsJ0QfjKTIo 1L4edeXvN1Rd0muRyypO9JzSik/m9FK0RfTSFk8FsHwDhN2xRXB2naq/68AqI2dqxuiQ z6amVFLrj/8SP9v7fdORkPwUT+xkT5FavqlzMsFf+Tvr3aFY7xUdF/si+2qm2VnkkZFK KOnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=gY15YxDdKSZ0D8Iy0CW6yGr0u1mIBXiISHgw6mfVrxM=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=0OmVCvTjFTb5ihLqTOdWYjOCmPhjSGPT/e0McNm7wcbRLkTiECCu36TkijM55yAGyX dIMAXqWzZboYhvwMHDuzsufLgjMcFaDmuJJNcZaYx/cDrvjf3mgLMwx5emmSjMNumJUz rTUAIl4p/fBFRiTTU2nJ+/NZvoNg+FZK6yPLStWq9BIq65qp5Z04PPcGH2L5mIuAb9Qp f+wDGPypFf7ddvWVoqlXEJrA7T3yBgVlM06W64SnqmZiUhPxV9t4XTFZNukmNaLWZPGx dSfPqI5G1G/cse9EX6lT7djfUAKvZihOsCaRw66ybpOZYMT4x0MHEia7Dvxl1c/mIHt2 rg3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Sgpe9Tkw; spf=pass (google.com: domain of linux-kernel+bounces-3788-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3788-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h12-20020a170902f54c00b001d3c93f2363si776954plf.158.2023.12.18.06.00.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:00:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3788-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Sgpe9Tkw; spf=pass (google.com: domain of linux-kernel+bounces-3788-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3788-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 4188E28370F for ; Mon, 18 Dec 2023 14:00:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 115793A1AF; Mon, 18 Dec 2023 13:59:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Sgpe9Tkw" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F44937862 for ; Mon, 18 Dec 2023 13:59:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5cf4696e202so38357887b3.2 for ; Mon, 18 Dec 2023 05:59:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907991; x=1703512791; darn=vger.kernel.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=gY15YxDdKSZ0D8Iy0CW6yGr0u1mIBXiISHgw6mfVrxM=; b=Sgpe9Tkw1MxJx/Hj1mD6aOLDz9LRjH7ZfzWQJISlJitQgeQhjqtlvBhFSxK/XA/lZY iGqehjauOHgNbPdlXrfdtGmLTuECaSmcJSfoJlI6bwn5MxSYGBB5TtpfxiIeWSE5/EpF UTOhBkQUeNsZePUOdIPY0F9pELqsmoTyRhkKrgcw5YEpaX3TlmubJVZThV4yokhxRF4s 24LutZASDVRqJcXX2c5lOx+6MlMc++Z54FnvBnw9zZo94h9fKg8a0C3zet2CUrUe/Z4r KOLCPryjsZKXkERoIjc8NNxahVIcLP68sF338B7hnn5sWLrOA56bMv1s7hRFfOgnOhyc 45ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907991; x=1703512791; 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=gY15YxDdKSZ0D8Iy0CW6yGr0u1mIBXiISHgw6mfVrxM=; b=haBEdiu3wYz7J1o4lcmu88aaO6t/wVuMIHt1EoyJNKsWC/nUQpkz1G5oNxAPd8Mjq+ OcJ83HNCSDU2XwuCtkN8oDcjk5RrjEAig2G5MqjtTvVuj1SU5fIjjqaqLK/+crzsEoAO NYa9QyUpQ1geINBZs/INlIvsysJxErLvSwyHa1g6zYp5CLmRXATCR2BEu8zVmZcyAnlG F8VTF1mxSnynKnkoJa7hw+zNsvZxPkf5QzhhYi4qwXKRcznguzJw3tmHZdmMVHp34j3S OS7HpGijhK+ivUCtRJ8WP3wT7Y0URQu9cPCYOCXgacO81vlnNQ9iG29gTch7KTgVkur2 i9Gw== X-Gm-Message-State: AOJu0Yz1vxkycVHP7IoGHBFmDYzh9i6eGwom5XzvqslST7MA1ZaxNxBm d3NG1XP9klAF4q5KiUJmNDQoLU8LLzsng6AEyy8= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:690c:385:b0:5d3:a348:b0c0 with SMTP id bh5-20020a05690c038500b005d3a348b0c0mr2399920ywb.5.1702907991281; Mon, 18 Dec 2023 05:59:51 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:52 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-4-sebastianene@google.com> Subject: [PATCH v4 02/10] KVM: arm64: Add ptdump registration with debugfs for the stage-2 pagetables From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785628498176440465 X-GMAIL-MSGID: 1785628498176440465 While arch/*/mem/ptdump handles the kernel pagetable dumping code, introduce KVM/ptdump which deals with the stage-2 pagetables. The separation is necessary because most of the definitions from the stage-2 pagetable reside in the KVM path and we will be invoking functionality **specific** to KVM. This registers a wrapper on top of debugfs_create_file which allows us to hook callbacks on the debugfs open/show/close. The callbacks are used to prepare the display portion of the pagetable dumping code. Guard this functionality under the newly introduced PTDUMP_STAGE2_DEBUGFS. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/Kconfig | 13 +++++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/arm.c | 2 + arch/arm64/kvm/kvm_ptdump.h | 18 +++++++ arch/arm64/kvm/ptdump.c | 96 +++++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 arch/arm64/kvm/kvm_ptdump.h create mode 100644 arch/arm64/kvm/ptdump.c diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 83c1e09be..0014e55e2 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -71,4 +71,17 @@ config PROTECTED_NVHE_STACKTRACE If unsure, or not using protected nVHE (pKVM), say N. +config PTDUMP_STAGE2_DEBUGFS + bool "Present the stage-2 pagetables to debugfs" + depends on PTDUMP_DEBUGFS && KVM + default n + help + Say Y here if you want to show the stage-2 kernel pagetables + layout in a debugfs file. This information is only useful for kernel developers + who are working in architecture specific areas of the kernel. + It is probably not a good idea to enable this feature in a production + kernel. + + If in doubt, say N. + endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index c0c050e53..190eac175 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -23,6 +23,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-its.o vgic/vgic-debug.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o +kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o always-y := hyp_constants.h hyp-constants.s diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index e5f75f1f1..ee8d7cb67 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -2592,6 +2593,7 @@ static __init int kvm_arm_init(void) if (err) goto out_subs; + kvm_ptdump_register_host(); kvm_arm_initialised = true; return 0; diff --git a/arch/arm64/kvm/kvm_ptdump.h b/arch/arm64/kvm/kvm_ptdump.h new file mode 100644 index 000000000..98b595ce8 --- /dev/null +++ b/arch/arm64/kvm/kvm_ptdump.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +// +// Copyright (C) Google, 2023 +// Author: Sebastian Ene + +#ifndef __KVM_PTDUMP_H +#define __KVM_PTDUMP_H + +#include + + +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +void kvm_ptdump_register_host(void); +#else +static inline void kvm_ptdump_register_host(void) { } +#endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ + +#endif /* __KVM_PTDUMP_H */ diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c new file mode 100644 index 000000000..5816fc632 --- /dev/null +++ b/arch/arm64/kvm/ptdump.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0-only +// +// Debug helper used to dump the stage-2 pagetables of the system and their +// associated permissions. +// +// Copyright (C) Google, 2023 +// Author: Sebastian Ene + +#include +#include +#include + +#include +#include + + +struct kvm_ptdump_register { + void *(*get_ptdump_info)(struct kvm_ptdump_register *reg); + void (*put_ptdump_info)(void *priv); + int (*show_ptdump_info)(struct seq_file *m, void *v); + void *priv; +}; + +static int kvm_ptdump_open(struct inode *inode, struct file *file); +static int kvm_ptdump_release(struct inode *inode, struct file *file); +static int kvm_ptdump_show(struct seq_file *m, void *); + +static const struct file_operations kvm_ptdump_fops = { + .open = kvm_ptdump_open, + .read = seq_read, + .llseek = seq_lseek, + .release = kvm_ptdump_release, +}; + +static int kvm_ptdump_open(struct inode *inode, struct file *file) +{ + struct kvm_ptdump_register *reg = inode->i_private; + void *info = NULL; + int ret; + + if (reg->get_ptdump_info) { + info = reg->get_ptdump_info(reg); + if (!info) + return -ENOMEM; + } + + if (!reg->show_ptdump_info) + reg->show_ptdump_info = kvm_ptdump_show; + + ret = single_open(file, reg->show_ptdump_info, info); + if (ret && reg->put_ptdump_info) + reg->put_ptdump_info(info); + + return ret; +} + +static int kvm_ptdump_release(struct inode *inode, struct file *file) +{ + struct kvm_ptdump_register *reg = inode->i_private; + struct seq_file *seq_file = file->private_data; + + if (reg->put_ptdump_info) + reg->put_ptdump_info(seq_file->private); + + return 0; +} + +static int kvm_ptdump_show(struct seq_file *m, void *) +{ + return -EINVAL; +} + +static void kvm_ptdump_debugfs_register(struct kvm_ptdump_register *reg, + const char *name, struct dentry *parent) +{ + debugfs_create_file(name, 0400, parent, reg, &kvm_ptdump_fops); +} + +static struct kvm_ptdump_register host_reg; + +void kvm_ptdump_register_host(void) +{ + if (!is_protected_kvm_enabled()) + return; + + kvm_ptdump_debugfs_register(&host_reg, "host_page_tables", + kvm_debugfs_dir); +} + +static int __init kvm_host_ptdump_init(void) +{ + host_reg.priv = (void *)host_s2_pgtable_pages(); + return 0; +} + +device_initcall(kvm_host_ptdump_init); From patchwork Mon Dec 18 13:58:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180451 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1267874dyi; Mon, 18 Dec 2023 06:12:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IHgyjLxsJBIuAcFShUDDZZmklzjco/pFxKfmGskCVGvDIkl488QGr/B8SKDg7iQkOApN0IY X-Received: by 2002:a17:906:54f:b0:a23:54fe:a9a6 with SMTP id k15-20020a170906054f00b00a2354fea9a6mr899540eja.8.1702908725551; Mon, 18 Dec 2023 06:12:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908725; cv=none; d=google.com; s=arc-20160816; b=BHMKqjYy1Ee+BszLPrSvkRUh8iZ29BfrJrp2s/TKg3UxKmGCcAEdJ/d3N6kGHhIh8M FM5rok0ovxe14tlLjUURZGAhVeIxYx92j4KYL0dAzolmSqhlmCZ1z2dvJv6tebKsl6JA wUPXYmG+nXQp2fMnrAfYxD1+NdJhUaZgaGyi5xADRGK8mzQeqUSVpVF0DygT71V1EJZL 4HKmv6WNsIUuobU3mTsolOdl23udP9hlWNsNlGPax76mMr0K23blemFtVZCWkAwHG+gS C4mvgREnQq6rXNqXiM8Ti8EzsLxrphbh7I7F3LIPANdPtkfn5PaCMRITA22krck9ABhX NKvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=LD/nzFY+bvR1rFs9NYLk8iA9qjOdN0RioRORlh5RIvY=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=AoQOdkCl9lUsE7y5B6U8SkV2SzdXO6ccV/668A6UsS4eubmGE8Fpc1Ddu+92W1JtOJ lFmcKxJ/7SSwimYl8XMP50fhEMUDao0XpV7pRF0OLSIGo12T9av5EzOQ1L/L5GifaNmr 2Tk5T+NSOkJDEqbX5l/AFSFePvMcQfn4T5gM/gmhLk5kRdlKmxd8X3YXxLLl7ApzpEya OaJnS9JYdEuM0YIUa3S/bEFGTYkEjt3u1Q/yt/CEiHR0BVEYaM/uKpQBjuNTrJPRc/L9 deqNvsDqJW/jqCMCk4VoYEUH54e1wLYbLduJODkg9fUlY0zZ27Q6yhC1GAf0cPy93Bki br/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vdpVYKCD; spf=pass (google.com: domain of linux-kernel+bounces-3789-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3789-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a10-20020a1709064a4a00b00a23537017acsi1096107ejv.703.2023.12.18.06.12.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:12:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3789-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vdpVYKCD; spf=pass (google.com: domain of linux-kernel+bounces-3789-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3789-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 59BF91F25CF1 for ; Mon, 18 Dec 2023 14:00:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 797733A1D8; Mon, 18 Dec 2023 13:59:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vdpVYKCD" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B94537884 for ; Mon, 18 Dec 2023 13:59:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3333c009305so2212322f8f.1 for ; Mon, 18 Dec 2023 05:59:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907993; x=1703512793; darn=vger.kernel.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=LD/nzFY+bvR1rFs9NYLk8iA9qjOdN0RioRORlh5RIvY=; b=vdpVYKCDfLhlgKQGMiVZJAA4KWBzuycpZ4G8H68n84/tISFGb/wkhKLxkXNVLrd+cQ lFceHAM7f4jZyQjFkAKtTG9lClSVpDh/GB+jSw3s4mAYSR7x8c/BzTwO764/TOdV1Neu aBHljePq8AR/BBYvbbvp3/FinedUKiO6eFJGpLt1qjNW6iQjnQIrSWbNPP3uEvWKF6Ij NVAOejGCu5DIszaxcXtSsY6DkRscW4lwuJ7i0fo6POG2hdMOI6ChSbPgVAhkw9YLVeCy amjNd+tPG2UnLGD3dgQFXHE/Sf/OuDMGbBzbhcC3cz2auSiVdeKaIhrYgP2WvliNZ9nH ZfkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907993; x=1703512793; 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=LD/nzFY+bvR1rFs9NYLk8iA9qjOdN0RioRORlh5RIvY=; b=ljtdIEQqbo9pactMWU6a05FRT3a3Kubu1ljgrad/q0mT2F2AwwnRZtDNRZO6iJNUtu HFF12UbiiEb2Vv/0VUmk9mblFqEQ1s3oLmS7bFtbKhGb8HpIlAcsvAXR2kn/1EIgnInn rvPDJ6dm0oHTr6IiXTiiGN/puVrYGeaaftfzE659ApGct4cEhE/I2N750GAsI0wnVQ4F qpyib2PoUMUGsYeItSOGObrMRqii2kOIoaw3EJO1nYYKyU6GvU1+GJbMGUvRdonHPEwG jLyl5KF8OU2auW+FitEtdRJ71gTrLjiQUDc4FIX2hdM6CtMO7NhK0zhlt0rFhCUcaZp+ ISkQ== X-Gm-Message-State: AOJu0YxH142tbZ1+Z/1OdSXvE6m/Ez5t0ns31tdEqpWNT9zV3tCiFSG3 KK9B+eE+Ik84rgIepZia7BZZ8MxpXzF8nQ4JKeE= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6000:1ac9:b0:336:4b35:ba52 with SMTP id i9-20020a0560001ac900b003364b35ba52mr32388wry.9.1702907993532; Mon, 18 Dec 2023 05:59:53 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:53 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-5-sebastianene@google.com> Subject: [PATCH v4 03/10] KVM: arm64: Invoke the snapshot interface for the host stage-2 pagetable From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785629219689043319 X-GMAIL-MSGID: 1785629219689043319 Allocate memory for the snapshot by creating a memory cache with empty pages that will be used by the hypervisor during the page table copy. Get the required size of the PGD and allocate physically contiguous memory for it. Allocate contiguous memory for an array that is used to keep track of the pages used from the memcache. Call the snapshot interface and release the memory for the snapshot. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/ptdump.c | 107 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 5816fc632..e99bab427 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -25,6 +25,9 @@ static int kvm_ptdump_open(struct inode *inode, struct file *file); static int kvm_ptdump_release(struct inode *inode, struct file *file); static int kvm_ptdump_show(struct seq_file *m, void *); +static phys_addr_t get_host_pa(void *addr); +static void *get_host_va(phys_addr_t pa); + static const struct file_operations kvm_ptdump_fops = { .open = kvm_ptdump_open, .read = seq_read, @@ -32,6 +35,11 @@ static const struct file_operations kvm_ptdump_fops = { .release = kvm_ptdump_release, }; +static struct kvm_pgtable_mm_ops ptdump_host_mmops = { + .phys_to_virt = get_host_va, + .virt_to_phys = get_host_pa, +}; + static int kvm_ptdump_open(struct inode *inode, struct file *file) { struct kvm_ptdump_register *reg = inode->i_private; @@ -78,11 +86,110 @@ static void kvm_ptdump_debugfs_register(struct kvm_ptdump_register *reg, static struct kvm_ptdump_register host_reg; +static size_t host_stage2_get_pgd_len(void) +{ + u32 phys_shift = get_kvm_ipa_limit(); + u64 vtcr = kvm_get_vtcr(read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1), + read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1), + phys_shift); + return (kvm_pgtable_stage2_pgd_size(vtcr) >> PAGE_SHIFT); +} + +static phys_addr_t get_host_pa(void *addr) +{ + return __pa(addr); +} + +static void *get_host_va(phys_addr_t pa) +{ + return __va(pa); +} + +static void kvm_host_put_ptdump_info(void *snap) +{ + void *mc_page; + size_t i; + struct kvm_pgtable_snapshot *snapshot; + + if (!snap) + return; + + snapshot = snap; + while ((mc_page = pop_hyp_memcache(&snapshot->mc, get_host_va)) != NULL) + free_page((unsigned long)mc_page); + + if (snapshot->pgd_hva) + free_pages_exact(snapshot->pgd_hva, snapshot->pgd_pages); + + if (snapshot->used_pages_hva) { + for (i = 0; i < snapshot->used_pages_indx; i++) { + mc_page = get_host_va(snapshot->used_pages_hva[i]); + free_page((unsigned long)mc_page); + } + + free_pages_exact(snapshot->used_pages_hva, snapshot->num_used_pages); + } + + free_page((unsigned long)snapshot); +} + +static void *kvm_host_get_ptdump_info(struct kvm_ptdump_register *reg) +{ + int i, ret; + void *mc_page; + struct kvm_pgtable_snapshot *snapshot; + size_t memcache_len; + + snapshot = (void *)__get_free_page(GFP_KERNEL_ACCOUNT); + if (!snapshot) + return NULL; + + memset(snapshot, 0, sizeof(struct kvm_pgtable_snapshot)); + + snapshot->pgd_pages = host_stage2_get_pgd_len(); + snapshot->pgd_hva = alloc_pages_exact(snapshot->pgd_pages, GFP_KERNEL_ACCOUNT); + if (!snapshot->pgd_hva) + goto err; + + memcache_len = (size_t)reg->priv; + for (i = 0; i < memcache_len; i++) { + mc_page = (void *)__get_free_page(GFP_KERNEL_ACCOUNT); + if (!mc_page) + goto err; + + push_hyp_memcache(&snapshot->mc, mc_page, get_host_pa); + } + + snapshot->num_used_pages = DIV_ROUND_UP(sizeof(phys_addr_t) * memcache_len, + PAGE_SIZE); + snapshot->used_pages_hva = alloc_pages_exact(snapshot->num_used_pages, + GFP_KERNEL_ACCOUNT); + if (!snapshot->used_pages_hva) + goto err; + + ret = kvm_call_hyp_nvhe(__pkvm_host_stage2_snapshot, snapshot); + if (ret) { + pr_err("ERROR %d snapshot host pagetables\n", ret); + goto err; + } + + snapshot->pgtable.pgd = get_host_va((phys_addr_t)snapshot->pgtable.pgd); + snapshot->pgtable.mm_ops = &ptdump_host_mmops; + + return snapshot; +err: + kvm_host_put_ptdump_info(snapshot); + return NULL; +} + void kvm_ptdump_register_host(void) { if (!is_protected_kvm_enabled()) return; + host_reg.get_ptdump_info = kvm_host_get_ptdump_info; + host_reg.put_ptdump_info = kvm_host_put_ptdump_info; + kvm_ptdump_debugfs_register(&host_reg, "host_page_tables", kvm_debugfs_dir); } From patchwork Mon Dec 18 13:58:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180440 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1258137dyi; Mon, 18 Dec 2023 06:01:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IE5zhWVVqMOO6dnEjqy4wGAqb08TtcFSZECGJZE01dEm5vvSIwSZZ5kzktdyoSMktkFuWcR X-Received: by 2002:a17:902:ecc5:b0:1d3:bc22:17ba with SMTP id a5-20020a170902ecc500b001d3bc2217bamr816263plh.125.1702908090149; Mon, 18 Dec 2023 06:01:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908090; cv=none; d=google.com; s=arc-20160816; b=N8KdddHOsCBMyvoc23ohr++aBaoofBYe4FvIXyaV1BQxY09oVtnn1NlfRjsLC4VUiY d/NjeCG/Zkl6qnCIcdNVoClaHBBdzm3/HYXNGBxxTAmGb0Vop7U046PTsL6YUuzkVOjj lUEd80eBTmvUZ1qT0aFmH48rxQ/BAKFz+YBnoLvq4EnJPHpbNb2WETfEh6nBS59js/8X RJvI8kdpQEfQnxsr1g5yVA50FPdKr/5/BBNH0nBiS+Hw2apCdCZCLYa1JIX/8F3/J8rh C1J7E3Unm+V73cus+h953LMcZWeFMTLEMlXcAxRWF0VROZQsndts2t5CPnJTYztSHba1 P4Yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=/snQWpLs/9aDAfFwDwLoi7Q0iJhDc3rGT80KePC/RNs=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=VPXChjeeIOeuMYkoE5etIrjtVXGzRU2nRYU9dyyr6uUWmzE15cIFlPNU89dVSWy6Ue iHD6KVX9UWkX5y1P5JkN4QhhB3eqYL72XXS9pEjGADqZiZN5gnuieWVw+YzRa+P7ZeiC RQ8Rv3rMs0RRcqwBMNNgP3AnR/6lOnQMhFIEHwNGH+zO3xbqtcRKlmeEf+sJrI8VijJP jBjzIVWbftHge4P67KQn9W/xV+TrjaVyhMoVRS9W/P/BzXQUV0go8KN3FX/XACFo5GJy YMifrNIlBQZrGyRvwetdgL8RBX6nlDmzIKRRwK/Av5kH5lYlWb53zlgO8rQoKDZRgoTm d5FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WPZ7iWSK; spf=pass (google.com: domain of linux-kernel+bounces-3790-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3790-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id ix10-20020a170902f80a00b001d3be63dde9si1566061plb.457.2023.12.18.06.01.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:01:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3790-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WPZ7iWSK; spf=pass (google.com: domain of linux-kernel+bounces-3790-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3790-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C6728283E19 for ; Mon, 18 Dec 2023 14:01:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DACB94236C; Mon, 18 Dec 2023 14:00:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WPZ7iWSK" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 691413A1AC for ; Mon, 18 Dec 2023 13:59:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3335231c733so1378329f8f.0 for ; Mon, 18 Dec 2023 05:59:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907995; x=1703512795; darn=vger.kernel.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=/snQWpLs/9aDAfFwDwLoi7Q0iJhDc3rGT80KePC/RNs=; b=WPZ7iWSKl1JRBaFmrbik+HI9MDrQtPrfCxHSsLNW3sP5Ojjd5v0hICyuupS4XScg8Z iuKMp8JchtduynlhpyGRgRaURWL+0wU3VGMF9xZ2cFES+osl4zD0M0aiICqWjp0fNgnR SX2eO0uLZsbS8ctIqJA8OjKHQpnlsuXh1ZoEoWXQzp7bCP4xbw1aaITnpjfpNKBRppim zlORkhUTIMaBLQbjQ3zio3oZlcGGjA9sdOq5lWNRQCRmV+dHqBoDmcqprH5vFYW1ohpM xrMwqLgRu9vX6C+pnN4nlEFT1Sle+aSEjgES3BQSJxxoA0DQcaLIrFNbY/SUrPJ/gPD2 dNRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907995; x=1703512795; 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=/snQWpLs/9aDAfFwDwLoi7Q0iJhDc3rGT80KePC/RNs=; b=WSz/lUgmGRMBavAqfVVLQRi1+hP85MEcZmesNEm0rHsAiViGY3tKdY42fJ6Viaj4GS dE0G120ieVTD1Y2Qs639CWFQxWpf+GTyITrgw6943A/zqa38M1M66yFK4YuQZEJ1EOL+ GaOmYHhzWDXWETd9rd7CDb+6C9WyfXMmkh9MZO2gOHQE58eeqm7+v42SWcc8qvk8Zpjh z22DPhLV/pqvRP6vFhna08Je3iJJdHAxAdxYPYO46p6xBV1R/Q9Wyq9ZT05/0T3YACJ3 5aUzWMunLGZc7swjj6EPw4WcQzk1RG65vtU1E9MWuzSXeXiGJ+wDv5BJ5fT2OU5SrqfQ 6Fpw== X-Gm-Message-State: AOJu0YySMrer4q9zyD0foRrp7LXq9vEcK5/xChJ93vJ0U91b/833m3b6 pPmi8pxhlGeEI41geQA/PjKK81IayTbx8pu7QRs= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:adf:f548:0:b0:336:68ff:e929 with SMTP id j8-20020adff548000000b0033668ffe929mr7158wrp.5.1702907995808; Mon, 18 Dec 2023 05:59:55 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:54 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-6-sebastianene@google.com> Subject: [PATCH v4 04/10] arm64: ptdump: Expose the attribute parsing functionality From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785628553519963528 X-GMAIL-MSGID: 1785628553519963528 To keep the same output format as the arch specific ptdump and for the sake of reusability, move the parser's state tracking code out of the arch specific. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/ptdump.h | 41 ++++++++++++++++++++++++++++++++- arch/arm64/mm/ptdump.c | 36 ++--------------------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 581caac52..23510be35 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -9,6 +9,8 @@ #include #include +#include + struct addr_marker { unsigned long start_address; @@ -21,15 +23,52 @@ struct ptdump_info { unsigned long base_addr; }; +struct prot_bits { + u64 mask; + u64 val; + const char *set; + const char *clear; +}; + +struct pg_level { + const struct prot_bits *bits; + const char *name; + size_t num; + u64 mask; +}; + +/* + * The page dumper groups page table entries of the same type into a single + * description. It uses pg_state to track the range information while + * iterating over the pte entries. When the continuity is broken it then + * dumps out a description of the range. + */ +struct pg_state { + struct ptdump_state ptdump; + struct seq_file *seq; + const struct addr_marker *marker; + unsigned long start_address; + int level; + u64 current_prot; + bool check_wx; + unsigned long wx_pages; + unsigned long uxn_pages; +}; + void ptdump_walk(struct seq_file *s, struct ptdump_info *info); +void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, + u64 val); #ifdef CONFIG_PTDUMP_DEBUGFS #define EFI_RUNTIME_MAP_END DEFAULT_MAP_WINDOW_64 void __init ptdump_debugfs_register(struct ptdump_info *info, const char *name); #else static inline void ptdump_debugfs_register(struct ptdump_info *info, const char *name) { } -#endif +#endif /* CONFIG_PTDUMP_DEBUGFS */ void ptdump_check_wx(void); +#else +static inline void note_page(void *pt_st, unsigned long addr, + int level, u64 val) { } #endif /* CONFIG_PTDUMP_CORE */ #ifdef CONFIG_DEBUG_WX diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index e305b6593..64127c70b 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -66,31 +66,6 @@ static struct addr_marker address_markers[] = { seq_printf(m, fmt); \ }) -/* - * The page dumper groups page table entries of the same type into a single - * description. It uses pg_state to track the range information while - * iterating over the pte entries. When the continuity is broken it then - * dumps out a description of the range. - */ -struct pg_state { - struct ptdump_state ptdump; - struct seq_file *seq; - const struct addr_marker *marker; - unsigned long start_address; - int level; - u64 current_prot; - bool check_wx; - unsigned long wx_pages; - unsigned long uxn_pages; -}; - -struct prot_bits { - u64 mask; - u64 val; - const char *set; - const char *clear; -}; - static const struct prot_bits pte_bits[] = { { .mask = PTE_VALID, @@ -170,13 +145,6 @@ static const struct prot_bits pte_bits[] = { } }; -struct pg_level { - const struct prot_bits *bits; - const char *name; - size_t num; - u64 mask; -}; - static struct pg_level pg_level[] = { { /* pgd */ .name = "PGD", @@ -248,8 +216,8 @@ static void note_prot_wx(struct pg_state *st, unsigned long addr) st->wx_pages += (addr - st->start_address) / PAGE_SIZE; } -static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, - u64 val) +void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, + u64 val) { struct pg_state *st = container_of(pt_st, struct pg_state, ptdump); static const char units[] = "KMGTPE"; From patchwork Mon Dec 18 13:58:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180441 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1258383dyi; Mon, 18 Dec 2023 06:01:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IGJkaLDeFLhYAnizHzdj9AXqwXyLNqXdIRJZEKsPYpbSgzQJrAUYOZ7H4eX94uNV5lhAIgN X-Received: by 2002:a2e:854e:0:b0:2cc:553a:bc28 with SMTP id u14-20020a2e854e000000b002cc553abc28mr2296058ljj.25.1702908102486; Mon, 18 Dec 2023 06:01:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908102; cv=none; d=google.com; s=arc-20160816; b=AihnUmNXW1LGfXnf43HeXVFvhw8n06SXOJ/TTOJoWXktJjXL/j5q/Owo3DZTdPMxW4 ernPtCldTjePKHUlp0DuVcRcUTjgTCvtg8F0vvPjEqwhnyJixQ3j2oHrnL2gsiyU/sh7 XZOMPF6w1UHGGA+z1Ao9xPxO4gOHHTRbEASffFVJvXLEj22hP16u/G2lECg6h5gTNByK o22alfgo+HEdXstQC+pWm11FGcFfm/pTYG6qj+ZHSWWE6/ue+fjMF2NEOIaU42ArpWuq yfDF2qYcgaGRbLMoBFLmFWUQ82LDFCNurqrC62eAJJf2HKJym8u5yYgdpZU1nUg1PoVe wrYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=mFw1xMXqNfgjkaBSBUL3X2hMTwwdH9z1Qrb9oU+YYCg=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=ybRKxIFR1yP8SfrUsm3Jz9hGVJXIPLfqS3XYF4XK+LblbUfWoJgNSqIovwr1VhbmeU WtG2E2V0E74HB5RyAsseSzQVeV3Omh9WtWtpiqo3zP4ZVs25B+5knORteJxTd7+LB3ul KB/6gyDcFw7s3DQ1Y1IzJ5T8FjbHyKfiL7ILQSI9QCciImAW1W6wIuD+dn8Rwx/C4Mao BjxBJ/K3zj2fOJR8qZRbfa9rHdIvaqqybRLF+zvMQnfpp3BiwagYKItQIQvZeUkgwv2i QPJZf5Csk9Lt7CU8EN3Zusk3MDEhPm7zah4t2C5qKW4ft/A8hwG6ie+wU77HMXCeu7E5 1cVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=TFlF86aa; spf=pass (google.com: domain of linux-kernel+bounces-3792-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3792-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id kh16-20020a170906f81000b00a234b6bc08csi1206254ejb.262.2023.12.18.06.01.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:01:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3792-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=TFlF86aa; spf=pass (google.com: domain of linux-kernel+bounces-3792-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3792-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id D8C521F2624A for ; Mon, 18 Dec 2023 14:01:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7D86C42396; Mon, 18 Dec 2023 14:00:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TFlF86aa" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14FE93A1CF for ; Mon, 18 Dec 2023 13:59:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dbd4a080c0bso103406276.1 for ; Mon, 18 Dec 2023 05:59:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907998; x=1703512798; darn=vger.kernel.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=mFw1xMXqNfgjkaBSBUL3X2hMTwwdH9z1Qrb9oU+YYCg=; b=TFlF86aaxDB2yvv/vLHpZ+mMqZB4dXMTKb1DVlzcFHwKmVZFpPq9uGXWZOGaebS3L6 i3cbc8RvifUbKROS/icpuPoRwcZUzh2amgNlokOx99ZMxxrq1l8n5Mcy1lV5KGaWuvJZ iT/tgEopPb/f8o/0n1+dOOTW/jI2MOHUFUoaBYrzATztpUtVJvhfbs2JZUnE+Apw6eE/ vT4Ly1jZb5iLSBVEYIWsRybVFYx2CAPj9zBKakkI9bHba7BCMmm1pTp9eG+waknfYVwq P0rU4+AP04TchYJ7B3cTFvbTjxyBa7GTBgioKto0hmoLtV6QgpziiYgrqpKCBmYxr12G 5CUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907998; x=1703512798; 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=mFw1xMXqNfgjkaBSBUL3X2hMTwwdH9z1Qrb9oU+YYCg=; b=WTM1GXiJGf4NaPF8tJo6+5t8d+aUv0gVGnbXllZmG31HZiFGChm8QUOhD8HdqwRz1v BeuD+qI3EC8D3fQGhG/wazhoScoLuonZhWSEELW0Oyfog6DYpLebw3KNqjs3diXK9hyg wuBZNaUi8l6/NbtF1Bhsejunc7vp5nz0EVpG9SL1TtnvifaZyPdVnrkfpPFwpfjFM8rI slvyq4kqKTSw9dbgZqyU5MGJJPezLdayGUz7gT9TfgcgFLAWcjfHZ9k+E8cnvGVOen9U A2/0216YinPIxj8J24FculhrGYfAPDs23dHcPQbOWFUWIdcAgsOIB1wRxcw9J4nT8lXP ss3g== X-Gm-Message-State: AOJu0YxLm03QwWTnUKJLN8+zMQyeO4mGYRInk9f4Xr3nbnj1fuNC/tgu G1fVoAPreKaSasoMaWMY49hLQ7evt9ht5hmLemk= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a25:8a09:0:b0:db5:4d59:3b30 with SMTP id g9-20020a258a09000000b00db54d593b30mr1613891ybl.8.1702907998064; Mon, 18 Dec 2023 05:59:58 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:55 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-7-sebastianene@google.com> Subject: [PATCH v4 05/10] arm64: ptdump: Use the mask from the state structure From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785628566489187602 X-GMAIL-MSGID: 1785628566489187602 Printing the descriptor attributes requires accessing a mask which has a different set of attributes for stage-2. In preparation for adding support for the stage-2 pagetables dumping, use the mask from the local context and not from the globally defined pg_level array. Store a pointer to the pg_level array in the ptdump state structure. This will allow us to extract the mask which is wrapped in the pg_level array and use it for descriptor parsing in the note_page. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/ptdump.h | 1 + arch/arm64/mm/ptdump.c | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 23510be35..4e728d2a1 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -45,6 +45,7 @@ struct pg_level { */ struct pg_state { struct ptdump_state ptdump; + struct pg_level *pg_level; struct seq_file *seq; const struct addr_marker *marker; unsigned long start_address; diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 64127c70b..015ed65d3 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -220,11 +220,12 @@ void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val) { struct pg_state *st = container_of(pt_st, struct pg_state, ptdump); + struct pg_level *pg_info = st->pg_level; static const char units[] = "KMGTPE"; u64 prot = 0; if (level >= 0) - prot = val & pg_level[level].mask; + prot = val & pg_info[level].mask; if (st->level == -1) { st->level = level; @@ -250,10 +251,10 @@ void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, unit++; } pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit, - pg_level[st->level].name); - if (st->current_prot && pg_level[st->level].bits) - dump_prot(st, pg_level[st->level].bits, - pg_level[st->level].num); + pg_info[st->level].name); + if (st->current_prot && pg_info[st->level].bits) + dump_prot(st, pg_info[st->level].bits, + pg_info[st->level].num); pt_dump_seq_puts(st->seq, "\n"); if (addr >= st->marker[1].start_address) { @@ -284,6 +285,7 @@ void ptdump_walk(struct seq_file *s, struct ptdump_info *info) st = (struct pg_state){ .seq = s, .marker = info->markers, + .pg_level = &pg_level[0], .level = -1, .ptdump = { .note_page = note_page, @@ -321,6 +323,7 @@ void ptdump_check_wx(void) { 0, NULL}, { -1, NULL}, }, + .pg_level = &pg_level[0], .level = -1, .check_wx = true, .ptdump = { From patchwork Mon Dec 18 13:58:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180442 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1258930dyi; Mon, 18 Dec 2023 06:02:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IGklUnWEz7tAcssXTJ1i01S/9R8u3AAWJe6r1x50rdygutJ3dizsSkeyQLEH0lAOpOfJsBe X-Received: by 2002:ac8:5f94:0:b0:400:a2b8:1c97 with SMTP id j20-20020ac85f94000000b00400a2b81c97mr19961533qta.17.1702908131523; Mon, 18 Dec 2023 06:02:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908131; cv=none; d=google.com; s=arc-20160816; b=GshXDY0OEAirSX7PVVfHEJ7/kLbhf7Ah7cpD3G+5zOftuWZG+Y3mOsiZbZEOeB3xWW sFgqUsbMmMotc4mjmRL6AngN1TBwCS5xr8/gz8BuW8aNecqqI1vp0k8XHrdAbnO82pa8 2lwrpIc50v/egBwLZYJs4z0FYJ2R/cH6THZnggX1zijs1PzHrAMkw/huBU9yvswII3fa lz0Bte8sBgkG+Sm3+fhTxByNJT/eX+vDxsewfaowNkr1/hGJye4CNb6hfr7N8t8M0KVl Qhz1JduYDknY8M4ZyAUosuWLvBVq6GdHYDjRU8LX/YXUZ02ZsdSuTHL2LejSDvkuJUoM XQuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=rOYwKOEiNUEli62sB45rLYKU5arboivvkJa3j/TVfbA=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=e2Lt8NA1lSR3MKjM0bVcKIwjFvIjmnHoWo03BEz1v9Ug67zNlx+erQPw5QmWAWuEY0 vAy9BAoPhUnapvMJtgM6ZK9oMMq2km5jGXdeWE++lrkpYQSRkWcC7G2ZcymfKDy2jvYG l816qv+m2IrX59YVZdmFysizhMw+6500GJirDnCFESpVj1llCgChf0rvAPn2v52WGA/X rq1DYJZUfSyUkK4GLcQoFdw86mpdCvzovUV8eG/iXNKZIRvn1sH633GsmmHfC/9Li5xK 7goeKjUHRG8CYlrjs4AqcK2iRkddHfHNT38dhjTR/pX6WwQfSP5IoRv3fOjkiCocfL73 kr/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=e+ttUet0; spf=pass (google.com: domain of linux-kernel+bounces-3793-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3793-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id gd25-20020a05622a5c1900b00425826ea01bsi26450482qtb.212.2023.12.18.06.02.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:02:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3793-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=e+ttUet0; spf=pass (google.com: domain of linux-kernel+bounces-3793-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3793-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4F5281C24999 for ; Mon, 18 Dec 2023 14:02:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 00A361D136; Mon, 18 Dec 2023 14:00:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="e+ttUet0" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 980C33A1CB for ; Mon, 18 Dec 2023 14:00:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-332ee20a3f0so2752947f8f.0 for ; Mon, 18 Dec 2023 06:00:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908000; x=1703512800; darn=vger.kernel.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=rOYwKOEiNUEli62sB45rLYKU5arboivvkJa3j/TVfbA=; b=e+ttUet07GJyopmWfk4cM1KPq4yTgK+WsfN8R7cNaa2OVNZbk0SjPfPuIINK27E9Ew OzvpC2B4XUEAAWilt/mddKmbUIKwgIAYE7wZrvim7UH94wuTDS9PAde+VFWBojQYEgDV /zrR6VB5cnGDtHGr2URo/nFnTrkqz/LTs63rflIN5wIY6IV4fosXhosqe8kpGu5R8msS 4tE54LxoezVSyPyisyUQpa3adaYkHegS3G/3yahod5QvBQ5mDKUFpcklze8QXLPTD3Tl gaINEkqNABuq/NGYIiTe32C97828jrwAUKBIS2hCRYINiHqUBZrT4xpIinFnIOyCbVn6 dn1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908000; x=1703512800; 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=rOYwKOEiNUEli62sB45rLYKU5arboivvkJa3j/TVfbA=; b=sjhmqUlpEjuLsapRfedBnmxEohWcXwV+Uhc5kUiGr20TFlUNp31SXgnYAfdwPcqzaG 9Xokvqgg96nxxW+GIm8TOgj8NjY5qFfcshegj1jN1Esyz9VsCGMoBcSKz7aycBWVzXS/ 73JfVO6nFrg1cVOZMDohBfEuon0xQuORTvaSlGFXNBp+SAqxFnOObuE7TgxjiprWsoT/ ShCINNb0vWRDDqjmI/Krys/RUDqFF42aJs58aCGBc8VtZb3nXO6Wtpx7aAP4Ha0P2PE2 +EntM7SWmBe8G+CUc09QNdHTHxBQfAFK+cmzKfDG8G8nMH04ntvDqmfkp6dpVZZ82fUB OyVA== X-Gm-Message-State: AOJu0YzlILUTS3dnaJ5KyMXxBExweQiyxgWG7r90GjhUAjarNEgIHOTZ aj8hsVgufTsFQtrKrhnZmPdDtsHIR/0CdvlL0Ho= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6000:144d:b0:336:6ccb:c32b with SMTP id v13-20020a056000144d00b003366ccbc32bmr3131wrx.9.1702908000205; Mon, 18 Dec 2023 06:00:00 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:56 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-8-sebastianene@google.com> Subject: [PATCH v4 06/10] KVM: arm64: Move pagetable definitions to common header From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785628596967382889 X-GMAIL-MSGID: 1785628596967382889 In preparation for using the stage-2 definitions in ptdump, move some of these macros in the common header. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/kvm_pgtable.h | 42 ++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 42 ---------------------------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index f73efd8a8..37f2a8532 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -45,6 +45,48 @@ typedef u64 kvm_pte_t; #define KVM_PHYS_INVALID (-1ULL) +#define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2) + +#define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2) +#define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6) +#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO \ + ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 2 : 3; }) +#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW \ + ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 0 : 1; }) +#define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8) +#define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3 +#define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10) + +#define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2) +#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6) +#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7) +#define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8) +#define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3 +#define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10) + +#define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 50) + +#define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55) + +#define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54) + +#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) + +#define KVM_PTE_LEAF_ATTR_HI_S1_GP BIT(50) + +#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ + KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ + KVM_PTE_LEAF_ATTR_HI_S2_XN) + +#define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2) +#define KVM_MAX_OWNER_ID 1 + +/* + * Used to indicate a pte for which a 'break-before-make' sequence is in + * progress. + */ +#define KVM_INVALID_PTE_LOCKED BIT(10) + static inline bool kvm_pte_valid(kvm_pte_t pte) { return pte & KVM_PTE_VALID; diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 82fef9620..ccd46bf56 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -17,48 +17,6 @@ #define KVM_PTE_TYPE_PAGE 1 #define KVM_PTE_TYPE_TABLE 1 -#define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2) - -#define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2) -#define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6) -#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO \ - ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 2 : 3; }) -#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW \ - ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 0 : 1; }) -#define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8) -#define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3 -#define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10) - -#define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2) -#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6) -#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7) -#define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8) -#define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3 -#define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10) - -#define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 50) - -#define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55) - -#define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54) - -#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) - -#define KVM_PTE_LEAF_ATTR_HI_S1_GP BIT(50) - -#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ - KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ - KVM_PTE_LEAF_ATTR_HI_S2_XN) - -#define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2) -#define KVM_MAX_OWNER_ID 1 - -/* - * Used to indicate a pte for which a 'break-before-make' sequence is in - * progress. - */ -#define KVM_INVALID_PTE_LOCKED BIT(10) - struct kvm_pgtable_walk_data { struct kvm_pgtable_walker *walker; From patchwork Mon Dec 18 13:58:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1259023dyi; Mon, 18 Dec 2023 06:02:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzB3M1/QRCk0yALz8mZ6L2AWLskZnQwuSJe3HiqYm8O5/5laJQOSd3vDtmZYjzf8YPDx6s X-Received: by 2002:a17:906:14d:b0:a1c:837c:e4a0 with SMTP id 13-20020a170906014d00b00a1c837ce4a0mr8680875ejh.29.1702908136319; Mon, 18 Dec 2023 06:02:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908136; cv=none; d=google.com; s=arc-20160816; b=RNhYUUr/aG1BTXxljxXQL00RZl6+BtO/NmBQOzALldroMZGGO5wdOR8GPBiaUGdrIb TyAKrZKh/LT+CoBGQTPbDzcKL7TorfbL6L/M8dX34AhvODMb0pVYhTioFTBDEpaRhU0l /jUtCb5uhPGeGLVY8EcMOAYaQm1NgTX4GdK06yJiPyYQix40B6eX4+5sJtE9Ce2t40vj 5hTuhZEN7v4RQH1S5ZY8ZZl+eQ86+unZDjxpfjY+a/St9L5mKtueSsiO7ZFEugupVilL 82l5wINcEZ78DiZlpc8gbqrZ4vjzNbXqzn4fXl2ZyjSaJeRWnakpnhYNUpsnu0gA41XR NhKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=EaxQbDI0z91PmFyzqzSN6rlfHYYOk4E9JSBlc3pa6ug=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=hS5Z4tMsP1Plx1PIKC4YRTqoplC1e9lw9UPP5KvIwmGI9xz2ErUd4RBw1E5UHQXhjZ Ko0+zh4IUQh7cyWDu/wQNeeozJhPM2plaWH0mQnNH1pwTt++4owQ1Q1KK2tA2EEFqsi8 Bhdt8r53PxjW7HtHI+1gKv2LPKlrP4n4FvXl8e94aZvGOSgiPGQFE3oCB2nwRy9ud/+R w+pD8lSMhzvdcdEAXYk85G+J9BvqNyZA0CJ7fK+4cGAL18yHMoq42mg2L8MAbl1zk6Kj Tz8rNzWFwj74/fnLi+H1Sj4FxZNBWXu6GV9mNLbPuuM2yiBI3anl5+GLnhsjdBcJ0vyA Rq+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4FrIEjvL; spf=pass (google.com: domain of linux-kernel+bounces-3794-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3794-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id k14-20020a17090666ce00b00a1db99887f2si9812616ejp.192.2023.12.18.06.02.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:02:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3794-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4FrIEjvL; spf=pass (google.com: domain of linux-kernel+bounces-3794-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3794-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id ACF521F2620C for ; Mon, 18 Dec 2023 14:02:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0F8944FF63; Mon, 18 Dec 2023 14:00:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4FrIEjvL" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1A8B3D558 for ; Mon, 18 Dec 2023 14:00:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5cf4696e202so38360597b3.2 for ; Mon, 18 Dec 2023 06:00:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908002; x=1703512802; darn=vger.kernel.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=EaxQbDI0z91PmFyzqzSN6rlfHYYOk4E9JSBlc3pa6ug=; b=4FrIEjvLeTIHrtK29j+9GyEuxlhjS1nWcfEm73ttYKSTcqS+oqHW68b41m6CpofbM+ B03diqU77gw79A+u+iLVBWAr/KyYo2+Z6w3/UghA9GjnUVXGSXmJSNvO+bCjxsnXaPbL x90MynyVjocMkyGF07+xuqmsG9NUqxniyKofTDds3bcjm0uxbKskWeO2jA03jTSYNgLr AhUDT9MsY1RQm21VLg7u4zzegZpwMsaUkPqC+CXnZmyMjUE5z+kbcTCsMnOMyWqtKpbW jBcOA7Ccwwg5C9jsFp4liHWXN5SfVRZHWuFVOcDWIBSTcsYW5ss999AH13MYkC4iFLqy LTcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908002; x=1703512802; 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=EaxQbDI0z91PmFyzqzSN6rlfHYYOk4E9JSBlc3pa6ug=; b=QkWNa89O6D/C4yTpllpqYW3PHrVTFzf1i1Qnn7bxRfIXUNthnh6wPqOHZXBqFd6nZB hFLpB2RHxd0X4scwiyAppkIzyiEA5TDFxu2sFzWtB3PonaTKY6JGE5Kysm8g5MTRVZcO /BRY/8uQl31DOEV33MYRecGn0qsXj+DKaBUcY8pdEmNrbDH60StyQIBE9hWnj97Oy2NH HEolSbiDh78D9moNc4DqyI6/kDO0keUPLQtb4u4UPwbD1ErzKKSUnKB3cocR9R/UElVd TkxeThzPjq3w9I26MqciDRIlY/TXma0nYrkKNFwO7HL/ZBfmNUdl6YS/coef+EeMbX/X gPrA== X-Gm-Message-State: AOJu0Yw9rELfGm73iBeYnq+4O9nLiacOT0a4CmKdyquHQn/Og0ez8Wxr P6VtYgTj3vpKlHi30IlgWOqspRhDK5TN/adoG8I= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:690c:891:b0:5d3:40f3:56bf with SMTP id cd17-20020a05690c089100b005d340f356bfmr2377671ywb.1.1702908002759; Mon, 18 Dec 2023 06:00:02 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:57 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-9-sebastianene@google.com> Subject: [PATCH v4 07/10] KVM: arm64: Walk the pagetable snapshot and parse the ptdump descriptors From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785628601836682934 X-GMAIL-MSGID: 1785628601836682934 Define the attributes for the stage-2 pagetables that will be used by the ptdump parser code to interepret and show the descriptors. Build the pagetable level description dynamically and use the KVM pagetable walker to visit the PTEs. Display the number of the bits used by the IPA address space and the start level. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/ptdump.c | 135 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index e99bab427..80c338e03 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -40,6 +40,66 @@ static struct kvm_pgtable_mm_ops ptdump_host_mmops = { .virt_to_phys = get_host_pa, }; +static const struct prot_bits stage2_pte_bits[] = { + { + .mask = PTE_VALID, + .val = PTE_VALID, + .set = " ", + .clear = "F", + }, { + .mask = KVM_PTE_LEAF_ATTR_HI_S2_XN, + .val = KVM_PTE_LEAF_ATTR_HI_S2_XN, + .set = "XN", + .clear = " ", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R, + .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R, + .set = "R", + .clear = " ", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W, + .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W, + .set = "W", + .clear = " ", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_AF, + .val = KVM_PTE_LEAF_ATTR_LO_S2_AF, + .set = "AF", + .clear = " ", + }, { + .mask = PTE_NG, + .val = PTE_NG, + .set = "FnXS", + .clear = " ", + }, { + .mask = PTE_CONT, + .val = PTE_CONT, + .set = "CON", + .clear = " ", + }, { + .mask = PTE_TABLE_BIT, + .val = PTE_TABLE_BIT, + .set = " ", + .clear = "BLK", + }, { + .mask = KVM_PGTABLE_PROT_SW0, + .val = KVM_PGTABLE_PROT_SW0, + .set = "SW0", /* PKVM_PAGE_SHARED_OWNED */ + }, { + .mask = KVM_PGTABLE_PROT_SW1, + .val = KVM_PGTABLE_PROT_SW1, + .set = "SW1", /* PKVM_PAGE_SHARED_BORROWED */ + }, { + .mask = KVM_PGTABLE_PROT_SW2, + .val = KVM_PGTABLE_PROT_SW2, + .set = "SW2", + }, { + .mask = KVM_PGTABLE_PROT_SW3, + .val = KVM_PGTABLE_PROT_SW3, + .set = "SW3", + }, +}; + static int kvm_ptdump_open(struct inode *inode, struct file *file) { struct kvm_ptdump_register *reg = inode->i_private; @@ -73,9 +133,82 @@ static int kvm_ptdump_release(struct inode *inode, struct file *file) return 0; } +static int kvm_ptdump_build_levels(struct pg_level *level, unsigned int start_level) +{ + static const char * const level_names[] = {"PGD", "PUD", "PMD", "PTE"}; + int i, j, name_index; + + if (start_level > 2) { + pr_err("invalid start_level %u\n", start_level); + return -EINVAL; + } + + for (i = start_level; i < KVM_PGTABLE_MAX_LEVELS; i++) { + name_index = i - start_level; + name_index += name_index * start_level; + + level[i].name = level_names[name_index]; + level[i].num = ARRAY_SIZE(stage2_pte_bits); + level[i].bits = stage2_pte_bits; + + for (j = 0; j < level[i].num; j++) + level[i].mask |= level[i].bits[j].mask; + } + + return 0; +} + +static int kvm_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx, + enum kvm_pgtable_walk_flags visit) +{ + struct pg_state *st = ctx->arg; + struct ptdump_state *pt_st = &st->ptdump; + + note_page(pt_st, ctx->addr, ctx->level, ctx->old); + return 0; +} + static int kvm_ptdump_show(struct seq_file *m, void *) { - return -EINVAL; + u64 ipa_size; + char ipa_description[32]; + struct pg_state st; + struct addr_marker ipa_addr_markers[3] = {0}; + struct pg_level pg_level_descr[KVM_PGTABLE_MAX_LEVELS] = {0}; + struct kvm_pgtable_snapshot *snapshot = m->private; + struct kvm_pgtable *pgtable = &snapshot->pgtable; + struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) { + .cb = kvm_ptdump_visitor, + .arg = &st, + .flags = KVM_PGTABLE_WALK_LEAF, + }; + + if (kvm_ptdump_build_levels(pg_level_descr, pgtable->start_level) < 0) + return -EINVAL; + + snprintf(ipa_description, sizeof(ipa_description), + "IPA bits %2u start lvl %1u", pgtable->ia_bits, + pgtable->start_level); + + ipa_size = BIT(pgtable->ia_bits); + ipa_addr_markers[0].name = ipa_description; + ipa_addr_markers[1].start_address = ipa_size; + + st = (struct pg_state) { + .seq = m, + .marker = &ipa_addr_markers[0], + .level = -1, + .pg_level = &pg_level_descr[0], + .ptdump = { + .note_page = note_page, + .range = (struct ptdump_range[]) { + {0, ipa_size}, + {0, 0}, + }, + }, + }; + + return kvm_pgtable_walk(pgtable, 0, ipa_size, &walker); } static void kvm_ptdump_debugfs_register(struct kvm_ptdump_register *reg, From patchwork Mon Dec 18 13:58:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180444 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1259825dyi; Mon, 18 Dec 2023 06:03:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IEtqPkHHRYe4rec9J13IhR5xEu8F1Egse00e+w0R5alhlTva81CwmPoKvbNbw0w0IHIrgDR X-Received: by 2002:a17:90b:4a12:b0:28b:2aa5:6003 with SMTP id kk18-20020a17090b4a1200b0028b2aa56003mr1740427pjb.69.1702908184021; Mon, 18 Dec 2023 06:03:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908184; cv=none; d=google.com; s=arc-20160816; b=0sLCYtvqjndGv04+cQ7FIfbPHMELYgM6QV/VTLuOi9rrIbDvtJbnZlES6QSINT0UR4 dFQQdDMd4uSDklfE9pBvq3oU2eLizGwUo1oCk+QKdNtSkWu/+B8noUfNtGEoY/qa+nb1 3LIJoj3RjIqhdglQvyVjUdEK6quJtdtOlKuZJ/QJglnuJ7n38eEN6vdL2wC+C5r5tuFD BydSUdsqor/UYRnrPAQmK1PqSWU/tw3ddmGr/U9NGg6W+xV8skyRChTOiVkb/gndK3cZ CImKJgzC/WKSheFNydOL9v82KH/ewP/9qnPyvLAD5AFodQtmdqpVw0y2j3RsQhgECrk1 IdiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=/DzJPItky+7MIXN0FJdMX89fRzHYJTFATAafmjRyPww=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=DuBleL7TRu6TuzIdWz75syBYp7qTNc/DN7bGJARXjxSp5TCHWBmz4S/TXB39jKoxnr KlSjdvmzlUXh/WgXiiVuD34OhzcIeXplWoeP5Y+A8k3kVUXoNqNNhMET5MayRpJY/CKL Mh+B8OKT3YBnM9hPYAMsLYqfw/DsS9gomqquGriyTEPErwcV0WD4OOUFEWJcJFNX8QMd 7gD7bkbfoQiD+jkcz1Y7sks/1moTHLfjqx4I2Zmn1w2ktNaW34wUBMxfLkymztIqXAX6 dY0vWIOzhEp3qNK4CXeckd9cHVg1pJfg4zxqprX3xG0ACkDOqEHPJFnIlcNUppBZ2Pum tkoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="ir/j0z0d"; spf=pass (google.com: domain of linux-kernel+bounces-3796-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3796-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id sh3-20020a17090b524300b0028a30507ff6si18121118pjb.150.2023.12.18.06.03.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:03:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3796-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="ir/j0z0d"; spf=pass (google.com: domain of linux-kernel+bounces-3796-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3796-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 9B8E7283189 for ; Mon, 18 Dec 2023 14:02:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A544E5A84D; Mon, 18 Dec 2023 14:00:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ir/j0z0d" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD948498AD for ; Mon, 18 Dec 2023 14:00:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-336695c701eso527279f8f.0 for ; Mon, 18 Dec 2023 06:00:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908005; x=1703512805; darn=vger.kernel.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=/DzJPItky+7MIXN0FJdMX89fRzHYJTFATAafmjRyPww=; b=ir/j0z0dfCwLrjL6VlNGzyXRJSCry/oaieIvElgp7D+iblDakg8S2AOLx946fLvW9i Wfl2oEZsllrUKn9dsDmwPBGLnz/7HZ/x75q2Mokzxt9zw+MetyaHbcXHlgipaLDf5Rk6 4YOnas1WTwhaVDvCK41ckx2Nk+dCmiUKrJ0Zt3Nc6iXhuFl3bDm/zbQJsDwKz2i1ieEu dbswrlHceEeICRQuTbsDthdsnNXo92DxDnMZqFz52HbSx2f6zvAAk6Z8ei3Z5mqXlJQI v/1pp23zZRtEDmVnr3V8O0w7Q0QDWsYFjeozLp7sSuCrd8tyrCjNkj4bHFswJBn7amOW 8oLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908005; x=1703512805; 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=/DzJPItky+7MIXN0FJdMX89fRzHYJTFATAafmjRyPww=; b=X2pc8V7a5/aYlrJGDMh94FlMg44JaL6xyz2WMpnxaReO/NaiL9yYm/CXRkheUMUp2r IbLLVSOl8vChLggCwmXg1kH9iC1/FnEVRl9w9k1Nv4adtIr8GkedwfSYnR9vcUE9WYzg aFCtHbbyEKL5c3BMHhGVZrwcL0m/G86gfBsWFcP0UUHNYZoka23mCTZnIhlCKb2BBTEf WTrV1RX9S4JKn6la3A+fsekvFAJ53GRO59PYmNtiDy2z9F6mA3NJ1ntYuK5D9Nr6Zkj1 Hg+J5TF8DYKzxVXOdQH9uiUkRAqDk0GKvmqlhNLvrH6o5KBINVWUFXyb7Q4+UjhiSyUg BIHw== X-Gm-Message-State: AOJu0YzD7ugyZpElR2Wu45c1DCEp3X1H11MIykdpGU5bqZjok4bK2APB V52PqpnKlAi8YzscCLv2T4W+ktJ1CCqLBa3Zzi0= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:adf:d1cf:0:b0:333:5232:d7f5 with SMTP id b15-20020adfd1cf000000b003335232d7f5mr91065wrd.8.1702908005046; Mon, 18 Dec 2023 06:00:05 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:58 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-10-sebastianene@google.com> Subject: [PATCH v4 08/10] arm64: ptdump: Interpret memory attributes based on the runtime config From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785628651844156570 X-GMAIL-MSGID: 1785628651844156570 Introduce two callbacks that verify the current runtime configuration before parsing the attribute fields. This is used to check when FWB is enabled which changes the interpretation of the descriptor bits. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/ptdump.h | 7 +++++++ arch/arm64/kvm/ptdump.c | 32 ++++++++++++++++++++++++++++++++ arch/arm64/mm/ptdump.c | 6 ++++++ 3 files changed, 45 insertions(+) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 4e728d2a1..e150fc21f 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -23,11 +23,18 @@ struct ptdump_info { unsigned long base_addr; }; +/* Forward declaration */ +struct pg_state; + struct prot_bits { u64 mask; u64 val; const char *set; const char *clear; + /* bit ignored if the callback returns false */ + bool (*feature_on)(const struct pg_state *ctxt); + /* bit ignored if the callback returns true */ + bool (*feature_off)(const struct pg_state *ctxt); }; struct pg_level { diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 80c338e03..0ad7944e5 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -40,6 +40,18 @@ static struct kvm_pgtable_mm_ops ptdump_host_mmops = { .virt_to_phys = get_host_pa, }; +static bool is_fwb_enabled(const struct pg_state *m) +{ + struct kvm_pgtable_snapshot *snapshot = m->seq->private; + struct kvm_pgtable *pgtable = &snapshot->pgtable; + bool fwb_enabled = false; + + if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) + fwb_enabled = !(pgtable->flags & KVM_PGTABLE_S2_NOFWB); + + return fwb_enabled; +} + static const struct prot_bits stage2_pte_bits[] = { { .mask = PTE_VALID, @@ -81,6 +93,26 @@ static const struct prot_bits stage2_pte_bits[] = { .val = PTE_TABLE_BIT, .set = " ", .clear = "BLK", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | PTE_VALID, + .val = PTE_S2_MEMATTR(MT_S2_DEVICE_nGnRE) | PTE_VALID, + .set = "DEVICE/nGnRE", + .feature_off = is_fwb_enabled, + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | PTE_VALID, + .val = PTE_S2_MEMATTR(MT_S2_FWB_DEVICE_nGnRE) | PTE_VALID, + .set = "DEVICE/nGnRE FWB", + .feature_on = is_fwb_enabled, + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | PTE_VALID, + .val = PTE_S2_MEMATTR(MT_S2_NORMAL) | PTE_VALID, + .set = "MEM/NORMAL", + .feature_off = is_fwb_enabled, + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | PTE_VALID, + .val = PTE_S2_MEMATTR(MT_S2_FWB_NORMAL) | PTE_VALID, + .set = "MEM/NORMAL FWB", + .feature_on = is_fwb_enabled, }, { .mask = KVM_PGTABLE_PROT_SW0, .val = KVM_PGTABLE_PROT_SW0, diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 015ed65d3..6c7208f66 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -177,6 +177,12 @@ static void dump_prot(struct pg_state *st, const struct prot_bits *bits, for (i = 0; i < num; i++, bits++) { const char *s; + if (bits->feature_on && !bits->feature_on(st)) + continue; + + if (bits->feature_off && bits->feature_off(st)) + continue; + if ((st->current_prot & bits->mask) == bits->val) s = bits->set; else From patchwork Mon Dec 18 13:58:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180446 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1260321dyi; Mon, 18 Dec 2023 06:03:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IEOUiJVVdm/B68ngnMXFpWqz7agI8jcH/XxuSjliiTK3Irq5jjgXOOY+TmCK4VRmn+Q9yaO X-Received: by 2002:a05:6a20:158b:b0:18f:d156:5b16 with SMTP id h11-20020a056a20158b00b0018fd1565b16mr9371915pzj.104.1702908211368; Mon, 18 Dec 2023 06:03:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908211; cv=none; d=google.com; s=arc-20160816; b=OC34bPf8gjVmGx3K+Zk57iyI6wMA8ApCWgA7C+MBJidw3qcX4tlqZfidu4XFmj+lQw g61wwVRAe2yBz7o020NARO6ApHY/+N8wCUc9taH9b7CYWKi574h8QrTRqnhgtL6zCyY8 ubG0oXDoEYwlzkm49E078ZmiI209HcYpw42DMZ2hRm9Vhi36uffh208WGXeLMC/dqv/k bfuREJA5nCC2fRItXho77/OuYQPkm43UscL7X1rUf6dNfiCJS36rjI0gkRP1xRRNxDC3 xgcCQMDPHh/knTqxNsxzgL8/vMNFF4YMfDMiVy/lQjJ/oLtyYkwyLslfMI5wU6+8YaBM 8GJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=RVeSPgnrVagCch9Gfw7vmBqjH7Htt5Ae7divhOxYmU8=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=0DYkNUz+Szj7wD9Tak980Cb1LEmGr4e+80nRcAZpvtwIu2au2fSQNiyoKKc8vaulgt Yp4W2PHlmUg6HBXkxrxzE8+E0xcyHVrd68Vbua1Zfo8Pv11V8y/Bfi36n9pXFNsmrcQE Y2JzjssbGSbQSUMbSv7uUNBvMeMYGyVEiNHbeB7PubC1cqhGiHqEmavWs3stJbxcrGRb 8H2CtOqGjR5HU1CASWkdewC300utZQYgaOHz7gJWJetS1tB9uAqEw/hqFrqrliDC968R V0UJAeVnuX/F/oLtm9015ApUKHRXXuwKqBTiiq4f66oF3qMzWGXn8ESRR/kwwjbBZ1r8 1lWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=l6UDF0WQ; spf=pass (google.com: domain of linux-kernel+bounces-3797-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3797-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id x16-20020a634a10000000b005b96d038729si16792274pga.728.2023.12.18.06.03.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:03:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3797-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=l6UDF0WQ; spf=pass (google.com: domain of linux-kernel+bounces-3797-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3797-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 22102283F41 for ; Mon, 18 Dec 2023 14:03:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DD07E5A873; Mon, 18 Dec 2023 14:00:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="l6UDF0WQ" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC8BA3D558 for ; Mon, 18 Dec 2023 14:00:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-33661476cf9so1208531f8f.1 for ; Mon, 18 Dec 2023 06:00:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908007; x=1703512807; darn=vger.kernel.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=RVeSPgnrVagCch9Gfw7vmBqjH7Htt5Ae7divhOxYmU8=; b=l6UDF0WQ+SFAGSsUAKY8WIDmv+O9wC0MSpMcB9coK77ya6OoUQLVeQ6rkf8GbhNy6V IxI1/2C4lMJPLj8VIsF8Jhq1BeR5oKYxQo/GC+ShsJk/afbigXyXoS22pSWNZ2YRQeJN kZ/DH8QwcM4+xTBP+ZhfT1ewZtCYWpBiskZgYOp9a2+9+gl1Q6ZB9zdO02JEESq+R84A XPcBAnlHLomo6BSuSTZuuwrWe5oRrQ142bdTlpk1atTgn5ylGwNNQCz4/4whBlrXoHjm 9P65/fj2K3KNKv/A+iQDRQq55T5tHc80n4BXOxt/q5LpNjBAc9JZgolaY4DHaWfcdKmc bb1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908007; x=1703512807; 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=RVeSPgnrVagCch9Gfw7vmBqjH7Htt5Ae7divhOxYmU8=; b=hwc3vczHAXo6lKn30cDshcmd8xtElc8+XQeMI/V4wxWioaTYfIzkMBIaek5IDqMhw2 +Lx+KvFuCgO1ZwzLIxMz104MkDuQrVJtHacb3UxJheWLQZdXQS/lJQMg8Pzna2Qfdvya 5H1oRyV9NsNNYxFaIVI6BtBdnueRnHuJvO2LNFq2wLsS+aCUBfkSDVm0KEQiYiXclwZz AZrhFkvduxns83hP0fXngEU0NEnVT2a/HDDZFwwaeUyJXGffH6s0OtqZWrB7YaACGpKx ynh/4G6H3q2dOpXjtw5x9N8+UhdzxaljA8avU9aT3L0BqLn5ombSd1/pt6thDgKPtGIx I6JQ== X-Gm-Message-State: AOJu0YyKHvJCjzRpIq3IH0+cqAEM8RICi2QzHxC5/hVv0jUnIvctzoLX JvEQupiuD04+hn88YoiSyLorQZ5jb+q5eABqIrI= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6000:400f:b0:336:6611:7936 with SMTP id cp15-20020a056000400f00b0033666117936mr7753wrb.11.1702908007241; Mon, 18 Dec 2023 06:00:07 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:59 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-11-sebastianene@google.com> Subject: [PATCH v4 09/10] arm64: ptdump: Interpret pKVM ownership annotations From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785628680837797125 X-GMAIL-MSGID: 1785628680837797125 When pKVM is enabled the software bits are used to keep track of the page sharing state. Interepret these fields when pKVM is enabled and print the sharing state. Move the definitions to common pagetable header. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/kvm_pgtable.h | 26 ++++++++++ arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 26 ---------- arch/arm64/kvm/ptdump.c | 47 +++++++++++++++++-- 3 files changed, 69 insertions(+), 30 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index 37f2a8532..7f654d4aa 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -87,6 +87,13 @@ typedef u64 kvm_pte_t; */ #define KVM_INVALID_PTE_LOCKED BIT(10) +/* This corresponds to page-table locking order */ +enum pkvm_component_id { + PKVM_ID_HOST, + PKVM_ID_HYP, + PKVM_ID_FFA, +}; + static inline bool kvm_pte_valid(kvm_pte_t pte) { return pte & KVM_PTE_VALID; @@ -230,6 +237,25 @@ enum kvm_pgtable_prot { KVM_PGTABLE_PROT_SW3 = BIT(58), }; +/* + * SW bits 0-1 are reserved to track the memory ownership state of each page: + * 00: The page is owned exclusively by the page-table owner. + * 01: The page is owned by the page-table owner, but is shared + * with another entity. + * 10: The page is shared with, but not owned by the page-table owner. + * 11: Reserved for future use (lending). + */ +enum pkvm_page_state { + PKVM_PAGE_OWNED = 0ULL, + PKVM_PAGE_SHARED_OWNED = KVM_PGTABLE_PROT_SW0, + PKVM_PAGE_SHARED_BORROWED = KVM_PGTABLE_PROT_SW1, + __PKVM_PAGE_RESERVED = KVM_PGTABLE_PROT_SW0 | + KVM_PGTABLE_PROT_SW1, + + /* Meta-states which aren't encoded directly in the PTE's SW bits */ + PKVM_NOPAGE, +}; + #define KVM_PGTABLE_PROT_RW (KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W) #define KVM_PGTABLE_PROT_RWX (KVM_PGTABLE_PROT_RW | KVM_PGTABLE_PROT_X) diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h index ca8f76915..677686b86 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -14,25 +14,6 @@ #include #include -/* - * SW bits 0-1 are reserved to track the memory ownership state of each page: - * 00: The page is owned exclusively by the page-table owner. - * 01: The page is owned by the page-table owner, but is shared - * with another entity. - * 10: The page is shared with, but not owned by the page-table owner. - * 11: Reserved for future use (lending). - */ -enum pkvm_page_state { - PKVM_PAGE_OWNED = 0ULL, - PKVM_PAGE_SHARED_OWNED = KVM_PGTABLE_PROT_SW0, - PKVM_PAGE_SHARED_BORROWED = KVM_PGTABLE_PROT_SW1, - __PKVM_PAGE_RESERVED = KVM_PGTABLE_PROT_SW0 | - KVM_PGTABLE_PROT_SW1, - - /* Meta-states which aren't encoded directly in the PTE's SW bits */ - PKVM_NOPAGE, -}; - #define PKVM_PAGE_STATE_PROT_MASK (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1) static inline enum kvm_pgtable_prot pkvm_mkstate(enum kvm_pgtable_prot prot, enum pkvm_page_state state) @@ -53,13 +34,6 @@ struct host_mmu { }; extern struct host_mmu host_mmu; -/* This corresponds to page-table locking order */ -enum pkvm_component_id { - PKVM_ID_HOST, - PKVM_ID_HYP, - PKVM_ID_FFA, -}; - extern unsigned long hyp_nr_cpus; int __pkvm_prot_finalize(void); diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 0ad7944e5..4296e739f 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -52,6 +52,11 @@ static bool is_fwb_enabled(const struct pg_state *m) return fwb_enabled; } +static bool is_pkvm_enabled(const struct pg_state *m) +{ + return is_protected_kvm_enabled(); +} + static const struct prot_bits stage2_pte_bits[] = { { .mask = PTE_VALID, @@ -113,22 +118,56 @@ static const struct prot_bits stage2_pte_bits[] = { .val = PTE_S2_MEMATTR(MT_S2_FWB_NORMAL) | PTE_VALID, .set = "MEM/NORMAL FWB", .feature_on = is_fwb_enabled, + }, { + .mask = KVM_INVALID_PTE_OWNER_MASK | PTE_VALID, + .val = FIELD_PREP_CONST(KVM_INVALID_PTE_OWNER_MASK, + PKVM_ID_HYP), + .set = "HYP", + }, { + .mask = KVM_INVALID_PTE_OWNER_MASK | PTE_VALID, + .val = FIELD_PREP_CONST(KVM_INVALID_PTE_OWNER_MASK, + PKVM_ID_FFA), + .set = "FF-A", + }, { + .mask = __PKVM_PAGE_RESERVED | PTE_VALID, + .val = PKVM_PAGE_OWNED | PTE_VALID, + .set = "PKVM_PAGE_OWNED", + .feature_on = is_pkvm_enabled, + }, { + .mask = __PKVM_PAGE_RESERVED | PTE_VALID, + .val = PKVM_PAGE_SHARED_OWNED | PTE_VALID, + .set = "PKVM_PAGE_SHARED_OWNED", + .feature_on = is_pkvm_enabled, + }, { + .mask = __PKVM_PAGE_RESERVED | PTE_VALID, + .val = PKVM_PAGE_SHARED_BORROWED | PTE_VALID, + .set = "PKVM_PAGE_SHARED_BORROWED", + .feature_on = is_pkvm_enabled, + }, { + .mask = PKVM_NOPAGE | PTE_VALID, + .val = PKVM_NOPAGE, + .set = "PKVM_NOPAGE", + .feature_on = is_pkvm_enabled, }, { .mask = KVM_PGTABLE_PROT_SW0, .val = KVM_PGTABLE_PROT_SW0, - .set = "SW0", /* PKVM_PAGE_SHARED_OWNED */ + .set = "SW0", + .feature_off = is_pkvm_enabled, }, { - .mask = KVM_PGTABLE_PROT_SW1, + .mask = KVM_PGTABLE_PROT_SW1, .val = KVM_PGTABLE_PROT_SW1, - .set = "SW1", /* PKVM_PAGE_SHARED_BORROWED */ + .set = "SW1", + .feature_off = is_pkvm_enabled, }, { - .mask = KVM_PGTABLE_PROT_SW2, + .mask = KVM_PGTABLE_PROT_SW2, .val = KVM_PGTABLE_PROT_SW2, .set = "SW2", + .feature_off = is_pkvm_enabled, }, { .mask = KVM_PGTABLE_PROT_SW3, .val = KVM_PGTABLE_PROT_SW3, .set = "SW3", + .feature_off = is_pkvm_enabled, }, }; From patchwork Mon Dec 18 13:59:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 180445 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1260046dyi; Mon, 18 Dec 2023 06:03:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IEmmHijk4qmjKlcLHOZgDETW/8eniHSlyz95l1OYeksXQ+ZdK//CTiTMHoTJtHKN1feGsDq X-Received: by 2002:a05:6402:2152:b0:54c:4837:7d0a with SMTP id bq18-20020a056402215200b0054c48377d0amr5715421edb.73.1702908196966; Mon, 18 Dec 2023 06:03:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702908196; cv=none; d=google.com; s=arc-20160816; b=PZjffgztavtGbccEeeHkNEdua5p74LMitZV6J9GT6gUP3miF6IS83bMX77qa6XnQ5h lx+/P9KK39tsm+0T+yFgnmcH0GiVLWm4tci06QmbZ0k86jmDNar92xYgScoQt9NcGDna yRlb0cn94vdz9ChPZ4fA4lFVutWejdLAReQ9PXnvzaqB8r2IeOvAGMlFs3ZJ+Ggs8hTC XETMP1lLxqJRQ+tu0mzyESsECd8dp8F0FW+X5F3uRW8R0NZbWNpeiyNhlXzocFwUeErV U2hgNJmk9m8fk482/47Q4/ta/EY8lwGF+JBU6ZoaZBvxJCdxN/gc4gX3tIPgxG/F+GYb LLMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=5rhLwUQ1ywrBkBuwEQIDGD2rpJ8EK3ecZScs+lwiHfY=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=bgNx7LZBFIOKn4mwxCP/qS8KT1AaC9FmUfebmmYEe7UO/yMahC6vEbnlY/GAtghdo5 1ro+fTbzkFxteNNRkVG+grkiLYrAJLR2wbPUsXsfAqn4euJbxvWYwoXsiMUkRtsZ4Oo1 ZXA6495UPMncBUUJUONt4kanzJRojtrFpoJ+XsFw9JAIHLa0Em5/S7xrUWxXrmmWaypD X+PPoPsjmDxHU2+B93b6uuv6VdiukihnKdDoHacEPiisAE6n3USz+959ptshvmcGUsK5 vqxqHEig9WdE7qi1XEfZJwgQtBsJLVyY/qz/Nd8JfOGRweKGPHDRkz8bafAVkIDZLvcp +qgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jPU6vkoG; spf=pass (google.com: domain of linux-kernel+bounces-3798-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3798-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id e2-20020a50d4c2000000b005536a182c03si443113edj.569.2023.12.18.06.03.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 06:03:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3798-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jPU6vkoG; spf=pass (google.com: domain of linux-kernel+bounces-3798-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3798-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 489AF1F26257 for ; Mon, 18 Dec 2023 14:03:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D4ED8129EC8; Mon, 18 Dec 2023 14:00:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jPU6vkoG" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2785F5A846 for ; Mon, 18 Dec 2023 14:00:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-40c2c144e60so25228105e9.0 for ; Mon, 18 Dec 2023 06:00:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908009; x=1703512809; darn=vger.kernel.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=5rhLwUQ1ywrBkBuwEQIDGD2rpJ8EK3ecZScs+lwiHfY=; b=jPU6vkoGAADAxyYkA4LZSSorgWHYuJVLsk3Lk5lWKH1W+p1cpHvihFYtLb4p0In0a1 olCJ/G/YMY4tBeJC67T8tVqTQqV9q5d8dPjtTVAqGVG//alBGK2unY5xF5NbLFiQw12A adxUHtko0H2mvUG+NEpgbMM/t/78+CHsMIjWUYfM0iopTrkfjOIQJL3ip2p0GSqqIq+4 yrjRE72lsz+D8mAKOGF9QJ4k+9wp4vMHNonqsXJOs692F+axdPgrO6cTa3FzE8g17FNF JUQMm5IvKzlLvgAIUo20hCi2aVIEn17DZy3nIgnutoJ+ZibC/USginz9bkLGrl04YR45 tD6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908009; x=1703512809; 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=5rhLwUQ1ywrBkBuwEQIDGD2rpJ8EK3ecZScs+lwiHfY=; b=gcpa71mfXoSy+oCz/mP+MCTffSgm856tNIjdX2Ujg9nxCgEvTSIV8jIovgZU740vtt l5tQu3eV3bwVSbD/7SI8MDsi8W82QICM1P05UDg/5ZBWfIrSgrbH37qD362+l5ukrfSG g5fFcNhlWAr5yHoqmYQyD2yqVXuakVMDGY96TsiZfsp/OUMpose47nj6G7xvhzZ71Flw deP2ukyrX/KF56vzQuzTKAfVUjVTr/S+9+aatxwzTJnDYIiN/4u1P9w/+Uz/W3ejYxyo Dmwf6mx4dyMWfgDcRNiyPzHFAXfcKbje02SLeq0u65f9iBwz9E/aYrFnUViKxzQBnGJ/ EGBQ== X-Gm-Message-State: AOJu0YzgrRzkuwq7XEHv0M0qY/OLdJ+8DkZNJJIvjP8VBVjOjZnAEUd9 knY8ybLeNJY0M+4Ml3X+6CPz3cZGcLNZsmdtqQI= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:600c:3b95:b0:407:8ee2:998c with SMTP id n21-20020a05600c3b9500b004078ee2998cmr167649wms.3.1702908009341; Mon, 18 Dec 2023 06:00:09 -0800 (PST) Date: Mon, 18 Dec 2023 13:59:00 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-12-sebastianene@google.com> Subject: [PATCH v4 10/10] arm64: ptdump: Add guest stage-2 pagetables dumping From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, qperret@google.com, smostafa@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785628665470216850 X-GMAIL-MSGID: 1785628665470216850 Register a debugfs file on guest creation to be able to view their second translation tables with ptdump. This assumes that the host is in control of the guest stage-2 and has direct access to the pagetables. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/debug.c | 6 ++++++ arch/arm64/kvm/kvm_ptdump.h | 3 +++ arch/arm64/kvm/ptdump.c | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index 8725291cb..7c4c2902d 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "trace.h" @@ -342,3 +343,8 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE); vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE); } + +int kvm_arch_create_vm_debugfs(struct kvm *kvm) +{ + return kvm_ptdump_register_guest(kvm); +} diff --git a/arch/arm64/kvm/kvm_ptdump.h b/arch/arm64/kvm/kvm_ptdump.h index 98b595ce8..5f5a455d0 100644 --- a/arch/arm64/kvm/kvm_ptdump.h +++ b/arch/arm64/kvm/kvm_ptdump.h @@ -6,13 +6,16 @@ #ifndef __KVM_PTDUMP_H #define __KVM_PTDUMP_H +#include #include #ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS void kvm_ptdump_register_host(void); +int kvm_ptdump_register_guest(struct kvm *kvm); #else static inline void kvm_ptdump_register_host(void) { } +static inline int kvm_ptdump_register_guest(struct kvm *kvm) { return -1; } #endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ #endif /* __KVM_PTDUMP_H */ diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 4296e739f..62a753d6b 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -181,6 +181,8 @@ static int kvm_ptdump_open(struct inode *inode, struct file *file) info = reg->get_ptdump_info(reg); if (!info) return -ENOMEM; + } else { + info = inode->i_private; } if (!reg->show_ptdump_info) @@ -239,15 +241,14 @@ static int kvm_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx, return 0; } -static int kvm_ptdump_show(struct seq_file *m, void *) +static int kvm_ptdump_show_common(struct seq_file *m, + struct kvm_pgtable *pgtable) { u64 ipa_size; char ipa_description[32]; struct pg_state st; struct addr_marker ipa_addr_markers[3] = {0}; struct pg_level pg_level_descr[KVM_PGTABLE_MAX_LEVELS] = {0}; - struct kvm_pgtable_snapshot *snapshot = m->private; - struct kvm_pgtable *pgtable = &snapshot->pgtable; struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) { .cb = kvm_ptdump_visitor, .arg = &st, @@ -282,6 +283,26 @@ static int kvm_ptdump_show(struct seq_file *m, void *) return kvm_pgtable_walk(pgtable, 0, ipa_size, &walker); } +static int kvm_host_ptdump_show(struct seq_file *m, void *) +{ + struct kvm_pgtable_snapshot *snapshot = m->private; + + return kvm_ptdump_show_common(m, &snapshot->pgtable); +} + +static int kvm_ptdump_show(struct seq_file *m, void *) +{ + struct kvm *guest_kvm = m->private; + struct kvm_s2_mmu *mmu = &guest_kvm->arch.mmu; + int ret; + + write_lock(&guest_kvm->mmu_lock); + ret = kvm_ptdump_show_common(m, mmu->pgt); + write_unlock(&guest_kvm->mmu_lock); + + return ret; +} + static void kvm_ptdump_debugfs_register(struct kvm_ptdump_register *reg, const char *name, struct dentry *parent) { @@ -393,11 +414,19 @@ void kvm_ptdump_register_host(void) host_reg.get_ptdump_info = kvm_host_get_ptdump_info; host_reg.put_ptdump_info = kvm_host_put_ptdump_info; + host_reg.show_ptdump_info = kvm_host_ptdump_show; kvm_ptdump_debugfs_register(&host_reg, "host_page_tables", kvm_debugfs_dir); } +int kvm_ptdump_register_guest(struct kvm *kvm) +{ + debugfs_create_file("stage2_page_tables", 0400, kvm->debugfs_dentry, + kvm, &kvm_ptdump_fops); + return 0; +} + static int __init kvm_host_ptdump_init(void) { host_reg.priv = (void *)host_s2_pgtable_pages();