From patchwork Wed Nov 15 17:16:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457133 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 54B9AC2BB3F for ; Wed, 15 Nov 2023 17:17:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=AfEmyTHi0yHJ4l4Yvhuf75Dq4ghWZq5R7IuJhLpcEuI=; b=ZzRGCSYeCllAKvv/RNQfSP82Iz 1nwRKJkQeX6caNX/qCICOa93Zotvok8QpuIZ9ON7yqpMPShm5iZ4D48nNYZSx6OihCDDyqx3sw8Bq HGhfKVHw2CupiOe8ckqllJtBXqrB6Y5FwIdsNirTZ6gZZHBKKf5kA0qX7t+HOtDe9Itww1DWIcsuq aKzKAQfGHStuHSth//wZNVKpgYfia6QAi3bAh3sbcBQ9+8vh0lvV+v6OvZNV2WHL7eHPkrmqOcLYk JKkyZeh6JoW1awYGmBJWYVc7SBgczocB+yPCkNjefewr9+sZIEl2C85aUrs7G4tmpSOvXWrs9KE5z 8Yiqww7Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVc-001PRL-00; Wed, 15 Nov 2023 17:17:00 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVZ-001PQM-03 for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:16:58 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da0ccfc4fc8so8898721276.2 for ; Wed, 15 Nov 2023 09:16:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068616; x=1700673416; darn=lists.infradead.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=f5DtLWt2DkvfdlATtfwZgDQwlHPVgdWgrcJSLVV+crY=; b=wKtCFIUKtYvYqWHSBpUephyJTvlnglTqAeZNIyZLMaHHPZpyh6/cd6IclxoiPjDueR 8EQeI7ek9Hboxjkr1Jnq+ndR/gT2lj4uWbKdtTXZR36r226x4xARoCT1cEps7eg9uqzb 8VjRDhAfD9wywFtFrKrP9YFoHd3Yn86h5nfIzk95NiJxwQZAVy+KJwWODEHFNYHi7IuR kuaAmlQcdkfbW2dcdbdVMCVEOq0AUOl8hcWuURvwAk67Ef6wUqnWhH3Ax3iMrUfE+JNJ x7DkfXYCUlbws6+ob5u1ifFWuD8/URVvzUbT8rIJ9HpRwwFtAO3BCp7wJYUILIst3xBR eHlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068616; x=1700673416; 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=f5DtLWt2DkvfdlATtfwZgDQwlHPVgdWgrcJSLVV+crY=; b=FaW+NfL12LLJwsJYaGZ7sGn2+vNAXAn1cb80dBOznb/djj2Ewgw3iEMKgbycr1MMwz n2XMNO0Zu3uWHryu6QAE0+sVeM3cq8f0DWD4XJ9weTXg5Da0Ex54ATVOiAtW7gFpZTlj oL6Uxw1ELl2nNV+p1wSumkYFc+YdLTAUETMMVVTrDoHxVN7KWffJlcjVzzGDW7wGrxNT JEO8CPiN8NXfoQwFJ8j9m0N+0UNeDgVkJv+Aokk+tPJxsxlp10PD3OcNE1uwb60243Gq I10ta6DzuF2iVBN9sHP4hVyARtmOtE2yOBlbqLJkWQeAkonsKQDmBq439TXxt4b9EtJm b7Zw== X-Gm-Message-State: AOJu0YzY5OtxS3C13QMLm0FW6yXGi/LTkyj73hXgRZ0SqfvhhW27yToU M8hW/autc3YTNA9HwRAUoqXsMRHW6lYULGvr8iM= X-Google-Smtp-Source: AGHT+IFPAdfzxoe3HTWk2Br3fnX/ejb8TGG8XL881PzRrCeQNX6maVryFyEGTW9OnwypADg+2RbHapnA2CGQUO1chO0= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6902:182:b0:d9a:ec95:9687 with SMTP id t2-20020a056902018200b00d9aec959687mr332837ybh.11.1700068615713; Wed, 15 Nov 2023 09:16:55 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:31 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-3-sebastianene@google.com> Subject: [PATCH v3 01/10] KVM: arm64: Add snap shooting the host 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091657_054986_7C01C896 X-CRM114-Status: GOOD ( 22.72 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Introduce a new HVC that allows the caller to snap shoot the stage-2 pagetables under NVHE debug configuration. The caller specifies the location where the pagetables are copied and must ensure that the memory is accessible by the hypervisor. The memory where the pagetables are copied has to be allocated by the caller and shared with the hypervisor. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_pgtable.h | 36 +++++++ arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 20 ++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 102 ++++++++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 56 ++++++++++ 6 files changed, 216 insertions(+) diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h index 24b5e6b23417..99145a24c0f6 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_copy_host_stage2, }; #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 d3e354bb8351..be615700f8ac 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,21 @@ 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_len: The size of the phyisically contiguous buffer in bytes. + */ +struct kvm_pgtable_snapshot { + struct kvm_pgtable pgtable; + struct kvm_hyp_memcache mc; + void *pgd_hva; + size_t pgd_len; +}; + /** * kvm_pgtable_hyp_init() - Initialise a hypervisor stage-1 page-table. * @pgt: Uninitialised page-table structure to initialise. @@ -756,4 +772,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_copy() - Snapshot the pagetable + * + * @to_pgt: Destination pagetable + * @from_pgt: Source pagetable. The caller must lock the pagetables first + * @mc: The memcache where we allocate the destination pagetables from + */ +int kvm_pgtable_stage2_copy(struct kvm_pgtable *to_pgt, + const struct kvm_pgtable *from_pgt, + void *mc); +#else +static inline int kvm_pgtable_stage2_copy(struct kvm_pgtable *to_pgt, + const struct kvm_pgtable *from_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 0972faccc2af..9cfb35d68850 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_prepare_copy(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 2385fd03ed87..98646cc67497 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -314,6 +314,25 @@ 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_copy_host_stage2(struct kvm_cpu_context *host_ctxt) +{ +#ifdef CONFIG_NVHE_EL2_DEBUG + int ret = -EPERM; + DECLARE_REG(struct kvm_pgtable_snapshot *, snapshot, host_ctxt, 1); + kvm_pteref_t pgd; + + snapshot = kern_hyp_va(snapshot); + ret = __pkvm_host_stage2_prepare_copy(snapshot); + if (!ret) { + pgd = snapshot->pgtable.pgd; + snapshot->pgtable.pgd = (kvm_pteref_t)__hyp_pa(pgd); + } + cpu_reg(host_ctxt, 1) = ret; +#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 +367,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_copy_host_stage2), }; 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 8d0a5834e883..1c3ab5ac9110 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -266,6 +266,108 @@ int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd) return 0; } +#ifdef CONFIG_NVHE_EL2_DEBUG +static struct hyp_pool snapshot_pool = {0}; +static DEFINE_HYP_SPINLOCK(snapshot_pool_lock); + +static void *snapshot_zalloc_pages_exact(size_t size) +{ + void *addr = hyp_alloc_pages(&snapshot_pool, get_order(size)); + + hyp_split_page(hyp_virt_to_page(addr)); + + /* + * The size of concatenated PGDs is always a power of two of PAGE_SIZE, + * so there should be no need to free any of the tail pages to make the + * allocation exact. + */ + WARN_ON(size != (PAGE_SIZE << get_order(size))); + + return addr; +} + +static void snapshot_get_page(void *addr) +{ + hyp_get_page(&snapshot_pool, addr); +} + +static void *snapshot_zalloc_page(void *mc) +{ + struct hyp_page *p; + void *addr; + + addr = hyp_alloc_pages(&snapshot_pool, 0); + if (addr) + return addr; + + addr = pop_hyp_memcache(mc, hyp_phys_to_virt); + if (!addr) + return addr; + + memset(addr, 0, PAGE_SIZE); + p = hyp_virt_to_page(addr); + memset(p, 0, sizeof(*p)); + p->refcount = 1; + + return addr; +} + +static void snapshot_s2_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_and_test(p); + } +} + +int __pkvm_host_stage2_prepare_copy(struct kvm_pgtable_snapshot *snapshot) +{ + size_t required_pgd_len; + struct kvm_pgtable_mm_ops mm_ops = {0}; + struct kvm_s2_mmu *mmu = &host_mmu.arch.mmu; + struct kvm_pgtable *to_pgt, *from_pgt = &host_mmu.pgt; + struct kvm_hyp_memcache *memcache = &snapshot->mc; + int ret; + void *pgd; + + required_pgd_len = kvm_pgtable_stage2_pgd_size(mmu->vtcr); + if (snapshot->pgd_len < required_pgd_len) + return -ENOMEM; + + to_pgt = &snapshot->pgtable; + pgd = kern_hyp_va(snapshot->pgd_hva); + + hyp_spin_lock(&snapshot_pool_lock); + hyp_pool_init(&snapshot_pool, hyp_virt_to_pfn(pgd), + required_pgd_len / PAGE_SIZE, 0); + + mm_ops.zalloc_pages_exact = snapshot_zalloc_pages_exact; + mm_ops.zalloc_page = snapshot_zalloc_page; + mm_ops.free_pages_exact = snapshot_s2_free_pages_exact; + mm_ops.get_page = snapshot_get_page; + 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(); + ret = kvm_pgtable_stage2_copy(to_pgt, from_pgt, memcache); + host_unlock_component(); + + hyp_spin_unlock(&snapshot_pool_lock); + + 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 1966fdee740e..46b15d74118f 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -1598,3 +1598,59 @@ 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 stage2_copy_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_copy(struct kvm_pgtable *to_pgt, + const struct kvm_pgtable *from_pgt, + void *mc) +{ + int ret; + size_t pgd_sz; + struct kvm_pgtable_mm_ops *mm_ops = to_pgt->mm_ops; + struct kvm_pgtable_walker walker = { + .cb = stage2_copy_walker, + .flags = KVM_PGTABLE_WALK_LEAF | + KVM_PGTABLE_WALK_TABLE_PRE, + .arg = mc + }; + + pgd_sz = kvm_pgd_pages(to_pgt->ia_bits, to_pgt->start_level) * + PAGE_SIZE; + to_pgt->pgd = (kvm_pteref_t)mm_ops->zalloc_pages_exact(pgd_sz); + if (!to_pgt->pgd) + return -ENOMEM; + + memcpy(to_pgt->pgd, from_pgt->pgd, pgd_sz); + + ret = kvm_pgtable_walk(to_pgt, 0, BIT(to_pgt->ia_bits), &walker); + mm_ops->free_pages_exact(to_pgt->pgd, pgd_sz); + + return ret; +} +#endif /* CONFIG_NVHE_EL2_DEBUG */ From patchwork Wed Nov 15 17:16:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457134 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 60507C072A2 for ; Wed, 15 Nov 2023 17:17:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=CaALFya7i019RnQ1BcLy3yBL97WpuKEd4xGYEPQnQGg=; b=qNaQD1/xCFKwpNxj7Y/Xih8QLq ki7C5STCSUu8qMtGeCBi+ci/1jKF9eKnGmsOvB2bMDMufcUTRzXhAwzIgP5WndmHH4AKfm99vXetO Lq1eorM+JohWXcv0dUCXcr+5/tHYa+A7I9Z41PuEAq0oqgFsdkABLFPmQ8ohWZtdVgtdA8DDP1pZo fk1wYOkv3Piv9BVXiaCd3RKBOWEN5AaEHEbKLPSYyi2Y3XtomkyjDZJR5c1r6dQrESCIFs+T+a7Se K+ZagdUPHrGTUhJlEy2n+Lu8C5jqnQ/w99Ibdpeukg+j1a6xq9ZloWzinSJT2VVEfXp3Qki+G7VMR 3r5E5J5A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVh-001PTf-2b; Wed, 15 Nov 2023 17:17:05 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVb-001PRB-38 for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:01 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dae71322ed4so8914471276.1 for ; Wed, 15 Nov 2023 09:16:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068618; x=1700673418; darn=lists.infradead.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=y0A/JzsGt28jdcT80BavCxCwKvAhE81Bu7nGud0PArE=; b=d/7WfDIdmBe+RqJyCLjqw/lblKMY1pkeqA0v0+6OvZQgZaU+TAMciL7u8YS8EWtJWk crNMotk18j9Xo550lBX4RLLrc6hIKzBPmxS2dD3qStAPL/ojLBeZx8HDnFE8jPg6P5oB 2+H1xl+ZvuLmljKSslLen5ovVnQZqULVFgForn/sgN4g5hXPmMKBiENylNOYTYQnAA08 /wZdZHNN9WLJz0IUNKz1XPBLrR6SWabRhabP8HNa6uAuhQq1PlUA/nGbgfStjkyAMvGT nPtsNlHwnWEXkko5dY3GUGj1qPMTtErQFfqiwRhDYvoVSeYYFgKna2yuxTg8HbL5doq3 Tr5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068618; x=1700673418; 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=y0A/JzsGt28jdcT80BavCxCwKvAhE81Bu7nGud0PArE=; b=DPE0PdSVuoDxMVVk2ypt0LznoauKdG87lP0gTyMoIi9BwbeiolVodxky/dnubye9Gu Mh/4rJCCUev5RYLB6hEazS9Zsaojlyt+Yor471e/wbngdODM4cUgHhw91YzkkYHjdYjP gqZ+cLZAS1TTDKYCet77Uq4DBVOnSmfuwtD06erLuhha8U9kazQDCjY317vKlzGkziX1 Zg+NCvdJ5XGAJcXPJMZ74FlNOenaDqvTEYIDBHiQqCNJVZFb8lnwQ2XujMOSv6njym/G QKPb7uwAO+aIb6I5QXHTu9YepQ3GXWfXZzXZsNEcptCuJDqjMdqNM28DwoGFHWTTaiKY a8dw== X-Gm-Message-State: AOJu0YzBbfyp8M1g++DA4QKkMLA5krZgd/60u63vaFis/0KU5ln01xnG u+njUnnqEWGkG7nsyfXrfrp2F+eYuc+F+sTI9Nc= X-Google-Smtp-Source: AGHT+IHvlCAUAQ7rynEQW7WXWvObMDPO+YBhC2cqr0pMjotcdPVLuo899EzI/e2mZHuLUOn3sEwympOD+yO+Oto2Btk= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a25:3206:0:b0:da0:3da9:ce08 with SMTP id y6-20020a253206000000b00da03da9ce08mr324716yby.10.1700068618426; Wed, 15 Nov 2023 09:16:58 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:32 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-4-sebastianene@google.com> Subject: [PATCH v3 02/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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091700_023142_253923C2 X-CRM114-Status: GOOD ( 18.64 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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/mm/ptdump.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index e305b6593c4e..8761a70f916f 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -75,6 +75,7 @@ static struct addr_marker address_markers[] = { struct pg_state { struct ptdump_state ptdump; struct seq_file *seq; + struct pg_level *pg_level; const struct addr_marker *marker; unsigned long start_address; int level; @@ -252,11 +253,12 @@ static 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; @@ -282,10 +284,10 @@ static 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) { @@ -316,6 +318,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, @@ -353,6 +356,7 @@ void ptdump_check_wx(void) { 0, NULL}, { -1, NULL}, }, + .pg_level = &pg_level[0], .level = -1, .check_wx = true, .ptdump = { From patchwork Wed Nov 15 17:16:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457135 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 929FEC072A2 for ; Wed, 15 Nov 2023 17:17:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=4iQ1GZr9dXdTB++DbfWA9D8ffwu7Vjr/aSEbwsUTx+k=; b=1P01DqCCJuVEuguaukW/5ZtIxR NwwiTsfLiE+J1w8kRYar6r8DvZ0+SjEJjisoGo2RAaJG+vRHmg0bqZ9P8wMmB4JTekJaJ0/snmb2r WhOFFaul4aFWBcecuqpmoSGbS7ugtiwNttip2YCzKt30wYCczAqqEAxitTAHZGxoHKlOesLlK23wX s6gjBy3hROQnOgG13640TYfpZ1PdGaeLOIm+bEoXbb/hoxtqNkVB0FxElQsHDjM6+maJLlR2Njua7 6h6SkOy8rYIi8gkZaXf7JpWBkpQW9DZgJZi9z1jtzJhMqO+fuEhDM8nH7J79kK5L3CQGLvVVFbS9e pV8eeODg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVi-001PTs-1A; Wed, 15 Nov 2023 17:17:06 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVd-001PRz-2h for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:03 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9a541b720aso8346157276.0 for ; Wed, 15 Nov 2023 09:17:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068620; x=1700673420; darn=lists.infradead.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=ancG5BntywgLYEtvolLfMGCRB6TUL9+Qsz3r5K1PW+Y=; b=xG/yrxyfYKIeVQub1AH5FyfO5KLd8HsRvZGeWe/B4ZIyrOlLcoUvfO2z7OpzurmLwp IvLC/7l1yI2malNQYMHLXjRmL7s0TjuemoV0YFfN8JnZYbRQaRrbQ8sxV/9udLYntQFN HYnyzKl/0LSxbrkiZn43sMyVs+BkT2LfOw5FVzf+3S+Z37M4uKptOftpQMDM9V0CaJP4 FybzpcctIMKsDEIEiuFSKphlPDHsoQB0tuM0XKUk1VTGBx1/D5D0nHEd4g4noqKofIcg HWFPzItO9wH1t4RSFwtee81nPWfIVh9I4wFOlxfj4qd+2LD27S4Bem6pWtZIYvG1zkP/ 1cCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068620; x=1700673420; 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=ancG5BntywgLYEtvolLfMGCRB6TUL9+Qsz3r5K1PW+Y=; b=Gn2+aVl7HRq/3/4PgHxVdDZ5lSSsMnjjrdehk0shVn/jDbypn5bvnMBpsILPvRscrL oq+d+O8uM/8JqLmUhXyuwM7cnPsuXTGq404gK1uz/ak6nPiY52cVZJ6saBH/GNa/ZVzQ LWim2qbK3UtW/IwCSZT5PG1/bmvCgvvykMfVa9RRVbJdZgX7GGU5sxfHmi35k6ZtaVwZ kj5ggSH+RiItj02ViuYBEgboJsKrkpzUFjGODEqwVjx+6Fod+4r5EdZ/CFZBEJ+GJbrN NbcSjrhLLd9gHiP2RGebPJZ4PhAWKRbJZyzwCKDgzuqO388sUVUKS3kmYdyP70Xrzfll wnxQ== X-Gm-Message-State: AOJu0Yy45u3D2DieM7URY2IPsEVsdfhFHi491/vQutRVJth5uZY30eJQ ghhZpWLsoLUhriWPfJ04D3z2Avdn7uiRXVzyu40= X-Google-Smtp-Source: AGHT+IGj2sqWbMdL/6D9g7CRzAYqqT3mXvwK0Hse86EHkE+8SGctzk4vkst4vCcjMP9s+i6Vnuewpw1qh5ywF521RY0= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a25:dcc3:0:b0:d9a:4fb2:16a2 with SMTP id y186-20020a25dcc3000000b00d9a4fb216a2mr310123ybe.12.1700068620695; Wed, 15 Nov 2023 09:17:00 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:33 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-5-sebastianene@google.com> Subject: [PATCH v3 03/10] arm64: ptdump: Add the walker function to the ptdump info 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091701_880347_AD50E02B X-CRM114-Status: GOOD ( 15.90 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Stage-2 needs a dedicated walk function to be able to parse concatenated pagetables. The ptdump info structure is used to hold different configuration options for the walker. This structure is registered with the debugfs entry and is stored in the argument for the debugfs file. Hence, in preparation for parsing the stage-2 pagetables add the walk function as an argument for the debugfs file. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/ptdump.h | 1 + arch/arm64/mm/ptdump.c | 1 + arch/arm64/mm/ptdump_debugfs.c | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 581caac525b0..1f6e0aabf16a 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -19,6 +19,7 @@ struct ptdump_info { struct mm_struct *mm; const struct addr_marker *markers; unsigned long base_addr; + void (*ptdump_walk)(struct seq_file *s, struct ptdump_info *info); }; void ptdump_walk(struct seq_file *s, struct ptdump_info *info); diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 8761a70f916f..d531e24ea0b2 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -346,6 +346,7 @@ static struct ptdump_info kernel_ptdump_info = { .mm = &init_mm, .markers = address_markers, .base_addr = PAGE_OFFSET, + .ptdump_walk = &ptdump_walk, }; void ptdump_check_wx(void) diff --git a/arch/arm64/mm/ptdump_debugfs.c b/arch/arm64/mm/ptdump_debugfs.c index 68bf1a125502..7564519db1e6 100644 --- a/arch/arm64/mm/ptdump_debugfs.c +++ b/arch/arm64/mm/ptdump_debugfs.c @@ -10,7 +10,8 @@ static int ptdump_show(struct seq_file *m, void *v) struct ptdump_info *info = m->private; get_online_mems(); - ptdump_walk(m, info); + if (info->ptdump_walk) + info->ptdump_walk(m, info); put_online_mems(); return 0; } From patchwork Wed Nov 15 17:16:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457136 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A64F9C54FB9 for ; Wed, 15 Nov 2023 17:17:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=D9MUcB360Zyog+vMBZ0igr9EMEzK9d3TQiBRWMkJ74w=; b=2U3iGtYUIvPUwICLLn33rwd2Tu 7CKmyO6Ki0mjMdcP/ASmldcj7bZnv9sw7JV+mY9GBSZb9uySe4xYNy6udg52dftxAvg8/8ZKR4nub Y0kZ4Gwa/ovZ7ie0SRoIo0i+ZxkYU1n3wbT/iLYYApiRk2dQqE4z2u+CivJyAtDF/iRTOOUbsEw46 MyWzfjJRUzbmx6S802YNCDYaWcW1GkiSLVbgZEkvAZrCgiaRouUWMHpoIuKgpurmLsTzMqc1EDzyj RnHy+Hjo+KyuLXCQ/ReJh4qsQm5iJu8GwuHjyHmCpPWJJIAi/NIVIwwJB+erUVv7Wu0nVdi6643Dm rDqf4iYw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVo-001PXL-2W; Wed, 15 Nov 2023 17:17:12 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVh-001PSu-2A for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:07 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da3b6438170so8169084276.1 for ; Wed, 15 Nov 2023 09:17:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068623; x=1700673423; darn=lists.infradead.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=tpT/Xyb5t7NIpC61c+cv5rhl4HgoVPgdS5jOtCH656U=; b=Xq0bxv+rX1OutwadqdrCUnEfpxMi+tkiWFNEVLlb60hy+om4biC+fvO83A26WIshJ1 S2HdxFh11JHA7eULb0jXFWyjdu7YAPb2ZWdAqwtr65fO+P/vvc6XqNwv7VdnKRY7us58 EMuD3xlCAZ2knODAlr81GffnaXiClVVlZyhWRd+jdwcuR2MeOYzIaKXBvD/Cr2OMcLpP O7ABGNrmGxnGSY+ye0c8HFfcADVG4icllqIV80+D968oa72nS/MCASmKsu5d5ZCObVt4 wd7IGYLELN+Hmp35SQZ7NVJQrm2Pspfw3g4yQROzUxMDp4hD339OjKoRiyPNQa8hJdwi dbRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068623; x=1700673423; 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=tpT/Xyb5t7NIpC61c+cv5rhl4HgoVPgdS5jOtCH656U=; b=bcgDFbEGbu3PzNi20M5s7M01NtdBulYB+mBm0E0+fTRCV94mR5ZahkRW6j88Xqh0Qh 6kZCfuvJrQqnM2xC6w8p3418erhCWuRoKyPYI9eMgoQc7XUHqfLjVfWEkHUPOpNJK4ch uDs4ZlKMP4qtX/Z/c1cfuCOPuHb6hHOVKVn/JZaSlyjv/1A0YC2Y+kz5iDg/3SISEuP6 8lZzn3CfQRSCRPL9iy/vD1pdrJyd7akEvwTRANcSE+vYIOwbm5chwYb3qLl34pK4bX0S gnsfzY4znQY1wzKCWVfCock+6apdcRcS6zjBDizdkbkT5iS3KJudb8+9OkZFhsUJnlQG S78A== X-Gm-Message-State: AOJu0Yz0dwIg4R07txmubwhqtjtXwrWYfY+py50OfnBnhF5h2yvEKfD3 8KQ+Rx62tuEJVT9BU875qkZajIi3jnu+v4Z7pnc= X-Google-Smtp-Source: AGHT+IEjAaqN2vEMMYz3zOcteQGHljOj2t7gTKVbX+G4QhSTd9t3YXrXpiUwzT1eJe2S3iwbycnJp+CxQJB3T+aIiIs= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a25:4016:0:b0:da3:b880:180e with SMTP id n22-20020a254016000000b00da3b880180emr311035yba.2.1700068623070; Wed, 15 Nov 2023 09:17:03 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:34 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-6-sebastianene@google.com> Subject: [PATCH v3 04/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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091705_711827_398C24B4 X-CRM114-Status: GOOD ( 12.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 be615700f8ac..913f34d75b29 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 46b15d74118f..65643320a6bb 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 Wed Nov 15 17:16:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457138 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 464BDC2BB3F for ; Wed, 15 Nov 2023 17:17:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=xxTPTK1P88NqHnRgV+Vox3vdDznghjtqNbUFHIY0Mas=; b=PjIO0HxTnAq2o43FxZnwO+lXdk v08tLxt0swhR/DC9YUyXD1lSiYK/ySe62YC5eVeG0LCjLEJWU8D8da8ieiqBYC4pEc97Bs39ZagZl 1yEle1NyEK3K/MkJjhivWHw2JjdxYeINAJpsA0FyVGuKNjZltsJWiyNcf4qwBeauzTj7QjFfZ99kP o4coHKT4GPleY6sw34Et6E/JJWZ2OCqip9g6Psr895qynZSAmSb97WtOm++yhrNuqCT28VLcDO0Nb 4rQtmzdYIpuq+PqQarszZmG68eT3XIauHPHwTBjfyO7XnX/5qPJf1+HJP1yTzlHoqNN6KSI8CdF92 3FGHJQ0g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVp-001PXh-16; Wed, 15 Nov 2023 17:17:13 +0000 Received: from mail-ed1-x54a.google.com ([2a00:1450:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVk-001PTj-1u for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:09 +0000 Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-54366567af4so1439872a12.1 for ; Wed, 15 Nov 2023 09:17:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068625; x=1700673425; darn=lists.infradead.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=jRY8TG/l5J/FMDXcrN4aaEldbJZIXmAxT86BWF/4hG4=; b=FGx+ThrrRZ61SfirpHCCOBxlAjJYK6NU1OCUnzP6NZMDMriKpMjEEJshIlN0hoMCsR /Z59vWAQ1qC62N+EEPb9+OTG7EfZ5xcw9CknFpXbi/1ytqnY3phyXrN2eNZhhlpPpzu3 bHUrvU14w6CJJRAsAmTD00JnILoQkrbgBJGm/FjhAxBR3hg8l9GLxW4NFIm7YOc+f1Hf eKo8gtFnHkPukJGvsic7ZYdMVo0rHRILDtBi8FsKLl8DrW1FXToEUc17hhuH7xVQWuVF g22pUNIz5aRJJGySbuoASodyITAiUAzzx6WLeUSzcB6dC05OFK373PfK8WNIcsgH+tY9 SPTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068625; x=1700673425; 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=jRY8TG/l5J/FMDXcrN4aaEldbJZIXmAxT86BWF/4hG4=; b=XU6eONvO0PMnKZ6jwKmJ2JrHVAX7tBnXpB1NZHGdArUD3ZTSfUyir6fsO9RpAmm4tC CuS+09xGd7KVv/eJbZnekNiP6BFdxkO9L2aqG1RF1/ohRLf9/HER8KDXjKzWOZFJTVYB 4DntKIWhT3yR54FPGMxUEJ9jmZxUjVmeY7dKAwIV9JbLoLWw9FeNUQfZ969ZdvwOn4jm RIWr4Dytf6PUXtK6Lrq7BtLYhbsFmoBonboy8eXH5dUDMrBVFZBCL8eB8XDkFbCQCOrj gwxdKxqXY1dX2wT+1oWZ62M5pMU1qOqgziDn3SLqzvj9liCa69nt3dfmS1L/KcakIAw/ 6BUA== X-Gm-Message-State: AOJu0YyUZw0Jv9HougjCCGBw7fnfiw/b/vIb1RRoC+hDdrHb9/gPqwWb TWZLu/9TVOmGIU+6eUq48DUmz3yFV/EG703rTr0= X-Google-Smtp-Source: AGHT+IEOG5AerWV4vOx8AuRc42o64gL06+fbFGWcKCfeIT2GBbZFlEcOhn/Yr4W2MhCZYOUvUqW8pWEYcTbHukKLs3o= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6402:156:b0:53d:ad8a:b0bd with SMTP id s22-20020a056402015600b0053dad8ab0bdmr104536edu.3.1700068625280; Wed, 15 Nov 2023 09:17:05 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:35 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-7-sebastianene@google.com> Subject: [PATCH v3 05/10] arm64: ptdump: Add hooks on debugfs file operations 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091708_628901_C9FCF07D X-CRM114-Status: GOOD ( 16.20 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Introduce callbacks invoked when the debugfs entry is accessed from userspace. This hooks will allow us to allocate and prepare the memory resources used by ptdump when the debugfs file is opened/closed. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/ptdump.h | 7 +++++ arch/arm64/mm/ptdump_debugfs.c | 53 +++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 1f6e0aabf16a..9b2bebfcefbe 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -20,9 +20,16 @@ struct ptdump_info { const struct addr_marker *markers; unsigned long base_addr; void (*ptdump_walk)(struct seq_file *s, struct ptdump_info *info); + int (*ptdump_prepare_walk)(void *file_priv); + void (*ptdump_end_walk)(void *file_priv); }; void ptdump_walk(struct seq_file *s, struct ptdump_info *info); + +struct ptdump_info_file_priv { + struct ptdump_info info; + void *file_priv; +}; #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); diff --git a/arch/arm64/mm/ptdump_debugfs.c b/arch/arm64/mm/ptdump_debugfs.c index 7564519db1e6..3bf5de51e8c3 100644 --- a/arch/arm64/mm/ptdump_debugfs.c +++ b/arch/arm64/mm/ptdump_debugfs.c @@ -7,7 +7,8 @@ static int ptdump_show(struct seq_file *m, void *v) { - struct ptdump_info *info = m->private; + struct ptdump_info_file_priv *f_priv = m->private; + struct ptdump_info *info = &f_priv->info; get_online_mems(); if (info->ptdump_walk) @@ -15,7 +16,55 @@ static int ptdump_show(struct seq_file *m, void *v) put_online_mems(); return 0; } -DEFINE_SHOW_ATTRIBUTE(ptdump); + +static int ptdump_open(struct inode *inode, struct file *file) +{ + int ret; + struct ptdump_info *info = inode->i_private; + struct ptdump_info_file_priv *f_priv; + + f_priv = kzalloc(sizeof(struct ptdump_info_file_priv), GFP_KERNEL); + if (!f_priv) + return -ENOMEM; + + memcpy(&f_priv->info, info, sizeof(*info)); + + ret = single_open(file, ptdump_show, f_priv); + if (ret) { + kfree(f_priv); + return ret; + } + + if (info->ptdump_prepare_walk) { + ret = info->ptdump_prepare_walk(f_priv); + if (ret) + kfree(f_priv); + } + + return ret; +} + +static int ptdump_release(struct inode *inode, struct file *file) +{ + struct seq_file *f = file->private_data; + struct ptdump_info_file_priv *f_priv = f->private; + struct ptdump_info *info = &f_priv->info; + + if (info->ptdump_end_walk) + info->ptdump_end_walk(f_priv); + + kfree(f_priv); + + return single_release(inode, file); +} + +static const struct file_operations ptdump_fops = { + .owner = THIS_MODULE, + .open = ptdump_open, + .read = seq_read, + .llseek = seq_lseek, + .release = ptdump_release, +}; void __init ptdump_debugfs_register(struct ptdump_info *info, const char *name) { From patchwork Wed Nov 15 17:16:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457137 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7AC11C072A2 for ; Wed, 15 Nov 2023 17:17:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=1/MQqqsXYpitw2JguOvy8Y9goVD6jK1jrV1U3HyQ14k=; b=BaGcU7ll+O0R8jAc/YDSY5uFBW F9IaFZUHhFah19BvBeuqcbVKU/emxGKQTWU7IPXPgO3LnJvmAW1hDvXKw9xl1BJ1On6I4Y6VY5yNA S8AsnEB5CA53/WOe4jlSEpjJ2j6IYlqQZyji/EmlMbiWEI+geTzK4cnTDsQ5BBdE/Vbyc7CO9Lwos D2s75bU14NBdk4srcBKPuwUubeQBqMS7mhTj7IQiGeWabFAYnykmBWBVb7s99qsjF7uejDBa5Shqx tsVJI+hDO1s5U0FXcOJw4eC4/H7DeGm3syEhZaDy5/n+ZCP15AyRNB2Ndv+I/QM4FcF4GvtEeHA6V rioz4OcQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVq-001PYS-0K; Wed, 15 Nov 2023 17:17:14 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVl-001PV8-0t for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:11 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9ab79816a9so8325418276.3 for ; Wed, 15 Nov 2023 09:17:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068628; x=1700673428; darn=lists.infradead.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=YQSSCNEgEIVwDuGH6x1IyDnQkcpfK9nBZRdIMr0v9JQ=; b=IugI7Mbtgm6g8+YZTrsjffYcSQ0EPrqddqc0zPiUDvl/0sG2iIcSlg/CtyR9umX2sM rhHBGlezCUmJI1SM3FtxR6i6xToYm1c/+9x1tipX7qUmcq2b7W8vd7apfKimYgw6HEOI GKUeTZb5U674B53rmxfzNvnaMbyT75ZbeD2nrAK4vw3owKJoJm7CUm5tp25tlPM2SHMd yE/P74W8AtddfVBEV/v7da28zJhhuhLl9Qr9TPei8uCjkr1doV5VMwCCbqGntGpaEFX4 jOeDdVuRd2yof7nSR7ro2BRDaYFxQn/ZZmC9cw5n1SHzEtR+jvPNFBtiiBNEQsczUUvk 1LdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068628; x=1700673428; 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=YQSSCNEgEIVwDuGH6x1IyDnQkcpfK9nBZRdIMr0v9JQ=; b=aUrDNV9bU+UshhWQB+fIcru4gD5k7mWoz7MLNgrPW82nAIzDLfshqMm3VAQ5Uyb+8k hAJqckVVLXHtQDnEPH2Z/0nC8Hvj0VX9lFQBMMmgLy2Ovab6nGeVQZ/nTuv4WtZ0PXcF 7davvLTfsxmzIQGJHlzv7FWgfXJ6129FOICOgbGm9v8m2azoaoqVQFHJ26qAIEl4dorq 9T+qo+UVqS+L5refoeTy3u0iOYVWo3qQKoxyo1zfhG2rYZROosFn/8WzB2vw0pkGzpgv PU2KtLOWuH2DaU0DHsgpv0jdf0igm6/LvQ22q/xScfsqcyHZi1QNo25/yGE+rmj5OKL6 +btQ== X-Gm-Message-State: AOJu0Yz1xO8ihsw/k+9Co755crwWd0etDEaRYncgD3LgnCzbkwVAN+g5 ILrtg2fFNJlcDWlIZSmocpezq6UTqLLS8H2s3ac= X-Google-Smtp-Source: AGHT+IErn3bzCF0DX9XJB6QzS6DBzs7DLSSORQ60b11cbij8ap/mm1M6uedlPRtG3MZC6j1NjkHThkYCWTUAznPoE6k= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6902:4:b0:d9a:68de:16a1 with SMTP id l4-20020a056902000400b00d9a68de16a1mr369425ybh.0.1700068628077; Wed, 15 Nov 2023 09:17:08 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:36 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-8-sebastianene@google.com> Subject: [PATCH v3 06/10] arm64: ptdump: Register a debugfs entry for the host stage-2 tables 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091709_318391_CDC708E6 X-CRM114-Status: GOOD ( 27.68 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Initialize a structures used to keep the state of the host stage-2 ptdump walker when pKVM is enabled. Create a new debugfs entry for the host stage-2 pagetables and hook the callbacks invoked when the entry is accessed. When the debugfs file is opened, allocate memory resources which will be shared with the hypervisor for saving the pagetable snapshot. On close release the associated memory and we unshare it from the hypervisor. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/ptdump.h | 12 +++ arch/arm64/kvm/Kconfig | 13 +++ arch/arm64/kvm/arm.c | 2 + arch/arm64/mm/ptdump.c | 168 ++++++++++++++++++++++++++++++++ arch/arm64/mm/ptdump_debugfs.c | 8 +- 5 files changed, 202 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 9b2bebfcefbe..de5a5a0c0ecf 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -22,6 +22,7 @@ struct ptdump_info { void (*ptdump_walk)(struct seq_file *s, struct ptdump_info *info); int (*ptdump_prepare_walk)(void *file_priv); void (*ptdump_end_walk)(void *file_priv); + size_t mc_len; }; void ptdump_walk(struct seq_file *s, struct ptdump_info *info); @@ -33,13 +34,24 @@ struct ptdump_info_file_priv { #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); +void ptdump_debugfs_kvm_register(struct ptdump_info *info, const char *name, + struct dentry *d_entry); #else static inline void ptdump_debugfs_register(struct ptdump_info *info, const char *name) { } +static inline void ptdump_debugfs_kvm_register(struct ptdump_info *info, + const char *name, + struct dentry *d_entry) { } #endif void ptdump_check_wx(void); #endif /* CONFIG_PTDUMP_CORE */ +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +void ptdump_register_host_stage2(void); +#else +static inline void ptdump_register_host_stage2(void) { } +#endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ + #ifdef CONFIG_DEBUG_WX #define debug_checkwx() ptdump_check_wx() #else diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 83c1e09be42e..cf5b7f06b152 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 NVHE_EL2_DEBUG && 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/arm.c b/arch/arm64/kvm/arm.c index e5f75f1f1085..987683650576 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -2592,6 +2593,7 @@ static __init int kvm_arm_init(void) if (err) goto out_subs; + ptdump_register_host_stage2(); kvm_arm_initialised = true; return 0; diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index d531e24ea0b2..0b4cb54e43ff 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include enum address_markers_idx { @@ -378,6 +381,170 @@ void ptdump_check_wx(void) pr_info("Checked W+X mappings: passed, no W+X pages found\n"); } +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +static struct ptdump_info stage2_kernel_ptdump_info; + +static phys_addr_t ptdump_host_pa(void *addr) +{ + return __pa(addr); +} + +static void *ptdump_host_va(phys_addr_t phys) +{ + return __va(phys); +} + +static size_t stage2_get_pgd_len(void) +{ + u64 mmfr0, mmfr1, vtcr; + u32 phys_shift = get_kvm_ipa_limit(); + + mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); + mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); + vtcr = kvm_get_vtcr(mmfr0, mmfr1, phys_shift); + + return kvm_pgtable_stage2_pgd_size(vtcr); +} + +static int stage2_ptdump_prepare_walk(void *file_priv) +{ + struct ptdump_info_file_priv *f_priv = file_priv; + struct ptdump_info *info = &f_priv->info; + struct kvm_pgtable_snapshot *snapshot; + int ret, pgd_index, mc_index, pgd_pages_sz; + void *page_hva; + phys_addr_t pgd; + + snapshot = alloc_pages_exact(PAGE_SIZE, GFP_KERNEL_ACCOUNT); + if (!snapshot) + return -ENOMEM; + + memset(snapshot, 0, PAGE_SIZE); + ret = kvm_call_hyp_nvhe(__pkvm_host_share_hyp, virt_to_pfn(snapshot)); + if (ret) + goto free_snapshot; + + snapshot->pgd_len = stage2_get_pgd_len(); + pgd_pages_sz = snapshot->pgd_len / PAGE_SIZE; + snapshot->pgd_hva = alloc_pages_exact(snapshot->pgd_len, + GFP_KERNEL_ACCOUNT); + if (!snapshot->pgd_hva) { + ret = -ENOMEM; + goto unshare_snapshot; + } + + for (pgd_index = 0; pgd_index < pgd_pages_sz; pgd_index++) { + page_hva = snapshot->pgd_hva + pgd_index * PAGE_SIZE; + ret = kvm_call_hyp_nvhe(__pkvm_host_share_hyp, + virt_to_pfn(page_hva)); + if (ret) + goto unshare_pgd_pages; + } + + for (mc_index = 0; mc_index < info->mc_len; mc_index++) { + page_hva = alloc_pages_exact(PAGE_SIZE, GFP_KERNEL_ACCOUNT); + if (!page_hva) { + ret = -ENOMEM; + goto free_memcache_pages; + } + + push_hyp_memcache(&snapshot->mc, page_hva, ptdump_host_pa); + ret = kvm_call_hyp_nvhe(__pkvm_host_share_hyp, + virt_to_pfn(page_hva)); + if (ret) { + pop_hyp_memcache(&snapshot->mc, ptdump_host_va); + free_pages_exact(page_hva, PAGE_SIZE); + goto free_memcache_pages; + } + } + + ret = kvm_call_hyp_nvhe(__pkvm_copy_host_stage2, snapshot); + if (ret) + goto free_memcache_pages; + + pgd = (phys_addr_t)snapshot->pgtable.pgd; + snapshot->pgtable.pgd = phys_to_virt(pgd); + f_priv->file_priv = snapshot; + return 0; + +free_memcache_pages: + page_hva = pop_hyp_memcache(&snapshot->mc, ptdump_host_va); + while (page_hva) { + ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, + virt_to_pfn(page_hva)); + WARN_ON(ret); + free_pages_exact(page_hva, PAGE_SIZE); + page_hva = pop_hyp_memcache(&snapshot->mc, ptdump_host_va); + } +unshare_pgd_pages: + pgd_index = pgd_index - 1; + for (; pgd_index >= 0; pgd_index--) { + page_hva = snapshot->pgd_hva + pgd_index * PAGE_SIZE; + ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, + virt_to_pfn(page_hva)); + WARN_ON(ret); + } + free_pages_exact(snapshot->pgd_hva, snapshot->pgd_len); +unshare_snapshot: + WARN_ON(kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, + virt_to_pfn(snapshot))); +free_snapshot: + free_pages_exact(snapshot, PAGE_SIZE); + f_priv->file_priv = NULL; + return ret; +} + +static void stage2_ptdump_end_walk(void *file_priv) +{ + struct ptdump_info_file_priv *f_priv = file_priv; + struct kvm_pgtable_snapshot *snapshot = f_priv->file_priv; + void *page_hva; + int pgd_index, ret, pgd_pages_sz; + + if (!snapshot) + return; + + page_hva = pop_hyp_memcache(&snapshot->mc, ptdump_host_va); + while (page_hva) { + ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, + virt_to_pfn(page_hva)); + WARN_ON(ret); + free_pages_exact(page_hva, PAGE_SIZE); + page_hva = pop_hyp_memcache(&snapshot->mc, ptdump_host_va); + } + + pgd_pages_sz = snapshot->pgd_len / PAGE_SIZE; + for (pgd_index = 0; pgd_index < pgd_pages_sz; pgd_index++) { + page_hva = snapshot->pgd_hva + pgd_index * PAGE_SIZE; + ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, + virt_to_pfn(page_hva)); + WARN_ON(ret); + } + + free_pages_exact(snapshot->pgd_hva, snapshot->pgd_len); + WARN_ON(kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, + virt_to_pfn(snapshot))); + free_pages_exact(snapshot, PAGE_SIZE); + f_priv->file_priv = NULL; +} + +void ptdump_register_host_stage2(void) +{ + if (!is_protected_kvm_enabled()) + return; + + stage2_kernel_ptdump_info = (struct ptdump_info) { + .mc_len = host_s2_pgtable_pages(), + .ptdump_prepare_walk = stage2_ptdump_prepare_walk, + .ptdump_end_walk = stage2_ptdump_end_walk, + }; + + ptdump_debugfs_kvm_register(&stage2_kernel_ptdump_info, + "host_stage2_page_tables", + kvm_debugfs_dir); +} +#endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ + static int __init ptdump_init(void) { address_markers[PAGE_END_NR].start_address = PAGE_END; @@ -386,6 +553,7 @@ static int __init ptdump_init(void) #endif ptdump_initialize(); ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables"); + return 0; } device_initcall(ptdump_init); diff --git a/arch/arm64/mm/ptdump_debugfs.c b/arch/arm64/mm/ptdump_debugfs.c index 3bf5de51e8c3..4821dbef784c 100644 --- a/arch/arm64/mm/ptdump_debugfs.c +++ b/arch/arm64/mm/ptdump_debugfs.c @@ -68,5 +68,11 @@ static const struct file_operations ptdump_fops = { void __init ptdump_debugfs_register(struct ptdump_info *info, const char *name) { - debugfs_create_file(name, 0400, NULL, info, &ptdump_fops); + ptdump_debugfs_kvm_register(info, name, NULL); +} + +void ptdump_debugfs_kvm_register(struct ptdump_info *info, const char *name, + struct dentry *d_entry) +{ + debugfs_create_file(name, 0400, d_entry, info, &ptdump_fops); } From patchwork Wed Nov 15 17:16:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457139 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 24B70C2BB3F for ; Wed, 15 Nov 2023 17:17:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=K1LTAITZ1jATOBDnjj20qIPjPeTn1G4sigYS9fa1AWI=; b=NpsixC/OeDxKfiaYsYyZSK9fNz Y8qK4GUIhIMwtAiYxBrbyJ4g5h4oenUws8Tem1vmJgZ4GjKLvDV7MOyke3PrkpG83uV72EWu128/b ejf0Ta6rREd5U0jh4Z4pUQphFQAnoMmjK/dBskVCID8r5kn10MbgYuhMo/lRWuhJP5xHqkDvFt7IG 1xcMYK3JGN0RlyKNoN0X3vuysbdKN6Arh/ebf/V4wJ5P/LOVaDI2H3l7AAqnYbO6pujdoAPYVUTc5 gbma+jjRIONikM+ZrmRwHlSIgw+/3ByT5QUH3xVFAnV03v8718tNYzoTDGEwf+DvQhc/xQswNn3fe IUUhaDrg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVy-001Pdl-28; Wed, 15 Nov 2023 17:17:22 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVp-001PWc-1K for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:15 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5bc3be6a91bso8956347b3.1 for ; Wed, 15 Nov 2023 09:17:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068630; x=1700673430; darn=lists.infradead.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=zCmrW40F76i5JztVQV1SbSI81TwYB1s24Z+PnC9WnV4=; b=QLso3DkwanHJZRn6XDoSmcC76rxP7wrRooIvChCAGt6mqXJhAfjyuiuIkSyI+DbflG bxFZY/uVKHi/MRVrW6hZLEFRK7XzUmMv0QaneQkcYMXYTSZ5+HG/55tPBfnnWZZXSSMg dYYBZXbQOJR9SezrpjNJN+/Uvuzu7HOPVw90HdJYqlySr76HOzxDNFW1t4NPfW0U1XXA QiQ79JPaUkbdJgLMNAxuU5UWJCv3BtmrZXzoNJ1wdlBojYgiDjloIe7tN/GKIAqxcvVv xPLzoh9cJjZJ9CBbefRHJcgGcrOvTmhh224//F+E27Ui/2qwS2IoacFvzdVeWPVgso6Y edxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068630; x=1700673430; 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=zCmrW40F76i5JztVQV1SbSI81TwYB1s24Z+PnC9WnV4=; b=neWxi7KKHDcx/PKjlKb8jrj2iHube5AKWXE3N49rbtUvWftZc3R2jBecbL/YgVrDIF 1PkCwHQKnGBIcE6P5sqHFLf0weYROcjhffJXfN63ocD0Un1Ww6Fh+tjRaXfd3xk68d1C uPUVxDqPLXsW27J33cyLZLiXdj8enk6bqVyqlcWpHUXUQ3FaR4noKXkVo4pBdT32/qaw JM0SuNlHgpeI5XrDuF8QfQ0HjFAttpc10tjqqYccBnAAZrCFrlV5P9wnTWygERCEQEp5 qregOwvDCurOzT90skOcn971rW910cJBRwQgJSJYoUUJ7NpLSTZN8CVD6lJOahq8wuaa SWkw== X-Gm-Message-State: AOJu0YzoN71BMDMVRiqVvx1T9pmCy0+m+gUy1ebbPHTxXW+kiQ0NEddc 7WGeEnf17Y1P7F+NbSyuoTRRs5dkysYhQ5EC7iU= X-Google-Smtp-Source: AGHT+IELfUQoq3/g4kbIGiBRU7VsMlUAs35EiaE420fomxFsRN5Mm1Zx4hWdYOb+MaGSa4SgHhjdvgBzYWcJ8schaZk= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a0d:c046:0:b0:5be:9742:cc3e with SMTP id b67-20020a0dc046000000b005be9742cc3emr200344ywd.2.1700068630474; Wed, 15 Nov 2023 09:17:10 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:37 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-9-sebastianene@google.com> Subject: [PATCH v3 07/10] arm64: ptdump: Parse the host stage-2 page-tables from the snapshot 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091713_454016_9D2B239F X-CRM114-Status: GOOD ( 18.51 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a walker function which configures ptdump to parse the page-tables from the snapshot. Define the attributes used by the stage-2 parser and build a description of an array which holds the name of the level. Walk the entire address space configured in the pagetable and parse the attribute descriptors. Signed-off-by: Sebastian Ene --- arch/arm64/mm/ptdump.c | 157 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 0b4cb54e43ff..9f88542d5312 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -86,6 +86,7 @@ struct pg_state { bool check_wx; unsigned long wx_pages; unsigned long uxn_pages; + struct ptdump_info_file_priv *f_priv; }; struct prot_bits { @@ -174,6 +175,66 @@ static const struct prot_bits pte_bits[] = { } }; +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", + }, +}; + struct pg_level { const struct prot_bits *bits; const char *name; @@ -286,6 +347,7 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, delta >>= 10; unit++; } + pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit, pg_info[st->level].name); if (st->current_prot && pg_info[st->level].bits) @@ -394,6 +456,11 @@ static void *ptdump_host_va(phys_addr_t phys) return __va(phys); } +static struct kvm_pgtable_mm_ops host_mmops = { + .phys_to_virt = ptdump_host_va, + .virt_to_phys = ptdump_host_pa, +}; + static size_t stage2_get_pgd_len(void) { u64 mmfr0, mmfr1, vtcr; @@ -528,6 +595,95 @@ static void stage2_ptdump_end_walk(void *file_priv) f_priv->file_priv = NULL; } +static int stage2_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; + + pt_st->note_page(pt_st, ctx->addr, ctx->level, ctx->old); + + return 0; +} + +static void stage2_ptdump_build_levels(struct pg_level *level, + size_t num_levels, + unsigned int start_level) +{ + static const char * const lvl_names[] = {"PGD", "PUD", "PMD", "PTE"}; + int i, j, name_index; + + if (num_levels > KVM_PGTABLE_MAX_LEVELS && start_level > 2) { + pr_warn("invalid configuration %lu levels start_lvl %u\n", + num_levels, start_level); + return; + } + + for (i = start_level; i < num_levels; i++) { + name_index = i - start_level; + name_index = name_index * start_level + name_index; + + level[i].name = lvl_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; + } +} + +static void stage2_ptdump_walk(struct seq_file *s, struct ptdump_info *info) +{ + struct ptdump_info_file_priv *f_priv = + container_of(info, struct ptdump_info_file_priv, info); + struct kvm_pgtable_snapshot *snapshot = f_priv->file_priv; + struct pg_state st; + struct kvm_pgtable *pgtable; + u64 start_ipa = 0, end_ipa; + struct addr_marker ipa_address_markers[3]; + struct pg_level stage2_pg_level[KVM_PGTABLE_MAX_LEVELS] = {0}; + struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) { + .cb = stage2_ptdump_visitor, + .arg = &st, + .flags = KVM_PGTABLE_WALK_LEAF, + }; + + if (snapshot == NULL || !snapshot->pgtable.pgd) + return; + + pgtable = &snapshot->pgtable; + pgtable->mm_ops = &host_mmops; + end_ipa = BIT(pgtable->ia_bits) - 1; + + memset(&ipa_address_markers[0], 0, sizeof(ipa_address_markers)); + + ipa_address_markers[0].start_address = start_ipa; + ipa_address_markers[0].name = "IPA start"; + + ipa_address_markers[1].start_address = end_ipa; + ipa_address_markers[1].name = "IPA end"; + + stage2_ptdump_build_levels(stage2_pg_level, KVM_PGTABLE_MAX_LEVELS, + pgtable->start_level); + + st = (struct pg_state) { + .seq = s, + .marker = &ipa_address_markers[0], + .level = -1, + .pg_level = &stage2_pg_level[0], + .f_priv = f_priv, + .ptdump = { + .note_page = note_page, + .range = (struct ptdump_range[]) { + {start_ipa, end_ipa}, + {0, 0}, + }, + }, + }; + + kvm_pgtable_walk(pgtable, start_ipa, end_ipa, &walker); +} + void ptdump_register_host_stage2(void) { if (!is_protected_kvm_enabled()) @@ -537,6 +693,7 @@ void ptdump_register_host_stage2(void) .mc_len = host_s2_pgtable_pages(), .ptdump_prepare_walk = stage2_ptdump_prepare_walk, .ptdump_end_walk = stage2_ptdump_end_walk, + .ptdump_walk = stage2_ptdump_walk, }; ptdump_debugfs_kvm_register(&stage2_kernel_ptdump_info, From patchwork Wed Nov 15 17:16:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457140 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7B684C54FB9 for ; Wed, 15 Nov 2023 17:17:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=D/NXJafVjrgzDN3joKxXqMhdlwIfducwvk0rW+JIXhk=; b=MAL2S6cHEqqKOdT9Al2sSU3Gxt rYqHxU/458YuvlO/xubGqXXf3QQktOFxoGK/jFHQe/vOw1URExmY6sA7rbXBbT/04vAhZrCAsvClr 4PM5cwZwPzGC/YqPDIwhnh8p5kvOPpVNZrixT3131UeaZAuoQat1UvZ3CCeLXQ9KcTZ84+w6hpjrC 40P6+55UwGrljy/xGfFOq0al02t9yas/CIGqSBeOPCFRWXoJY5k26/n/rx9j77AzAzyisOUS24zZu OuAAkNpO5+8mhGzmgTKrpqiWCfuRYMGxSe2ozJ4I1FmNBiLUlR26tO/R73LC4SQctY9XsHPp55BAs w32WHA/g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVz-001PeR-0w; Wed, 15 Nov 2023 17:17:23 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVq-001PXl-0t for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:15 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a8d9dcdd2bso148758297b3.2 for ; Wed, 15 Nov 2023 09:17:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068633; x=1700673433; darn=lists.infradead.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=Qy3zX0s6ALtK+DaG4wUXBjQ8F6aHRpWpiFcWV1cr/U0=; b=dBAKMRC4rvg8M47TUdixus4hW3dO9mtMKShyNyJoo5JxDLXbSjqg8LzEwJWhjIXgUJ 0Sk4uzLfv6E86VZybQ8Wa5Z6uZSgKxNm9CuldVjvFXNGccDckeHP204LpgBVdEobKm1Y ioc1/Zj92STDfaDoaq5hhdhAYa9NOXEHelJ+Q83UiYBZM8buW86vyqpQECy/5EQ3VKA5 Li3/jGmv8/7uiDxwovmPNOALAHf6K9X05ZuooTWr56VZPHCjZaGTIKhz3dzEF31/g2CH fIMYm5/ua7/p4OeZIs9ccERNVHLGWbpTkXysU1v5+lBJ9oXICANiIcamXEkYYQGhW9BL hGIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068633; x=1700673433; 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=Qy3zX0s6ALtK+DaG4wUXBjQ8F6aHRpWpiFcWV1cr/U0=; b=KKZb11DZiYBAskYyS3b9WQEY/9MIzC9cnOH9G3yFlweEH1tuf7QitxxTDjt4Dq5qPs RFEIlSkKlIeS+hZCt37ISpvLg7F3rtvtC55xHAGpZw6j/dM8znYrcP1+ELTM2+/SrOtu CAfAh1h+O5fCTmHnrQugMymgcDmar5xHbEwlZPb8UXK9E73smqHqitrwoh1j/3BilvRv F550roXLNjo7faZgGAtTeY2q7uEu6MUyWMov4OY8HwfOMv4O3rb4B4TNV72b4HG1VDxs PsepnnykckytrUt19e/h2isNaDJHEzBPw89NfpADEH9zhYibPIAw5K6aUHy5Ubc4iVv5 c5Xg== X-Gm-Message-State: AOJu0YykIB9Lhbd3pB9+g1RMWYe9Ly2u4QlWwaLEb1ngwTZdl4Ko9Sb8 yN439fqFMyvBey/kdCMp46Lvfai5btKdU0cBomQ= X-Google-Smtp-Source: AGHT+IH15zotRvXqa3gGL+erJN8kJ8qtR4AedLWdqyhduoqwJwWiS5LNpUVQnjgpQUblW7SZJCRmmCsMHRZC6boVABk= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a0d:d7c3:0:b0:5a7:b672:4d88 with SMTP id z186-20020a0dd7c3000000b005a7b6724d88mr356731ywd.0.1700068633096; Wed, 15 Nov 2023 09:17:13 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:38 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-10-sebastianene@google.com> Subject: [PATCH v3 08/10] arm64: ptdump: Interpret memory attributes based on runtime configuration 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091714_322956_E9048EC1 X-CRM114-Status: GOOD ( 19.04 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org When FWB is used the memory attributes stored in the descriptors have a different bitfield layout. Introduce two callbacks that verify the current runtime configuration before parsing the attribute fields. Add support for parsing the memory attribute fields from the page table descriptors. Signed-off-by: Sebastian Ene --- arch/arm64/mm/ptdump.c | 65 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 9f88542d5312..ec7f6430f6d7 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -89,11 +89,19 @@ struct pg_state { struct ptdump_info_file_priv *f_priv; }; +/* + * This callback checks the runtime configuration before interpreting the + * attributes defined in the prot_bits. + */ +typedef bool (*is_feature_cb)(const void *ctx); + struct prot_bits { u64 mask; u64 val; const char *set; const char *clear; + is_feature_cb feature_on; /* bit ignored if the callback returns false */ + is_feature_cb feature_off; /* bit ignored if the callback returns true */ }; static const struct prot_bits pte_bits[] = { @@ -175,6 +183,34 @@ static const struct prot_bits pte_bits[] = { } }; +static bool is_fwb_enabled(const void *ctx) +{ + const struct pg_state *st = ctx; + struct ptdump_info_file_priv *f_priv = st->f_priv; + struct kvm_pgtable_snapshot *snapshot = f_priv->file_priv; + 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 bool is_table_bit_ignored(const void *ctx) +{ + const struct pg_state *st = ctx; + + if (!(st->current_prot & PTE_VALID)) + return true; + + if (st->level == CONFIG_PGTABLE_LEVELS) + return true; + + return false; +} + static const struct prot_bits stage2_pte_bits[] = { { .mask = PTE_VALID, @@ -216,6 +252,27 @@ static const struct prot_bits stage2_pte_bits[] = { .val = PTE_TABLE_BIT, .set = " ", .clear = "BLK", + .feature_off = is_table_bit_ignored, + }, { + .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, @@ -267,13 +324,19 @@ static struct pg_level pg_level[] = { }; static void dump_prot(struct pg_state *st, const struct prot_bits *bits, - size_t num) + size_t num) { unsigned i; 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 Wed Nov 15 17:16:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457141 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 46168C5AD4C for ; Wed, 15 Nov 2023 17:17:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=f7BTHhEpBv3sSU0RjCovbwWSM3TY3bbMx7S51qWHF4I=; b=uMKskM9+PsSzFB01n21LDeURDf 4KOXoIZJ2dtYt91DXuaVa0F2GmGcwLNkEAJrSWCv1uWy8HCDmJ/viJCI69GNx94T+HNPj9iPOGA5W gujBYyzeT+aGDE0HG/PEkWbr1pGBwVNiFY7/mssB3+ad99YNKNyd2uxZAdqtQkpAON/GdXr/q9yMH wWonNSVTRDw508rd+hNqoHzdrw+dmljJi7Wk+ORjwr6O1xR0140mk0rXo828XcI3B7OPB2GF+fYu+ SLv8ZZF9ETGiHevVeVqGQWlHemN67ZHlDqe45V6bQat86+QfbNcPOQuwfFZRDcadkjcQ9aRhFermi K9TslqhQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVz-001Pf7-2y; Wed, 15 Nov 2023 17:17:23 +0000 Received: from mail-ej1-x649.google.com ([2a00:1450:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVu-001PZj-1t for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:20 +0000 Received: by mail-ej1-x649.google.com with SMTP id a640c23a62f3a-9e27cc6dbf0so471126966b.1 for ; Wed, 15 Nov 2023 09:17:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068635; x=1700673435; darn=lists.infradead.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=cTRruPNWl9dydwy1RolnY7MN//AZ8WOLTxliFM7mi1c=; b=v1FvhD8UPG7sWF1cQZ/T5OvKh5H54c2LQOkDZ48N3n9MbWfWwL/lpIjPa5U7v8Du1c a5bzmRak7xo9qQiyduAgZidfYnA/ZcUMwBVfEk1tsh0iapIGxJ+ORJi+0m6JZOfBuspp ug+4pafKPa8EyfnUHDZ5KGdZViwLZ4PzEMdaxgf9OmOATVBwnJYRiob3ZMnBNxnEVDLK t+LKVNjrx/9pRIJUWF+vNWkH63LeDNOnAx62CFdqcWdHWFuZ/BJBspUloG9KbNimTILn Gcrtj6cdU7axqZz4OGB4t7h6kRDNvFxaP0PqCJgDP8TGJM5ZcSkUCOO8ZRc9gs8cLyR5 JVCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068635; x=1700673435; 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=cTRruPNWl9dydwy1RolnY7MN//AZ8WOLTxliFM7mi1c=; b=lLRDz5mnNScz+aOJnnfSQ3Xtmrg2yucqfM3Yh9nSMNwUrtU0aZW6IC6A3ZjThpMP+t UHIcL6AKHey21NRzMxjjwrLKsTvtBb9io0wIwyBL5fRK03mFFWUy53GhloLu7z1XjwYc Vx1T9E/IyiASRqkIJzRCoboD/Shf/0xUyTNM4YqKDy4NXlZCw7iijQiCFopFSqXNW1Ap XZQZVSSbbftfVeEy62b46MGfvfmntDU0u5j73auNT7HQvCSC6nNppRAC+fMywvNvl7M6 9ey+G5wMOyceUmKbgbbF455NiD1fhrH0HGuS6uvnanJhR9dOBm4s1yUyiAaqAJDCd0mI HI8g== X-Gm-Message-State: AOJu0Yz7rpRmAX5YCmcXUNJKkNCTFr2qLbkPcydQItcJnnoAur1tFuHw P9iwR9YifNo+O/uSTLrzyzjqHqOq6CZAJfEPQWk= X-Google-Smtp-Source: AGHT+IGsNbGDrA60Xa1dYe9ILtVmHQ514rV3j3OHq1AV69tXNThGqDwNIJ6SRVp7ouHANt4Icfonac0rBfGADpZZyaY= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a17:906:6bc9:b0:9c4:1dfa:de9 with SMTP id t9-20020a1709066bc900b009c41dfa0de9mr121012ejs.10.1700068635336; Wed, 15 Nov 2023 09:17:15 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:39 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-11-sebastianene@google.com> Subject: [PATCH v3 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091718_637298_315C4289 X-CRM114-Status: GOOD ( 12.81 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add support for interpretting pKVM invalid stage-2 descriptors that hold ownership information. We use these descriptors to keep track of the memory donations from the host side. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/kvm_pgtable.h | 7 +++++++ arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 7 ------- arch/arm64/mm/ptdump.c | 10 ++++++++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index 913f34d75b29..938baffa7d4d 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; diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h index 9cfb35d68850..cc2c439ffe75 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -53,13 +53,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/mm/ptdump.c b/arch/arm64/mm/ptdump.c index ec7f6430f6d7..ffb87237078b 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -273,6 +273,16 @@ 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 = KVM_PGTABLE_PROT_SW0, .val = KVM_PGTABLE_PROT_SW0, From patchwork Wed Nov 15 17:16:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457142 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9DAF3C072A2 for ; Wed, 15 Nov 2023 17:17:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=DsrDe8C6+R50hWkT3zJ+hI5ZJXOTz8fkGiVkYBAPh1M=; b=B+JBclgg865XT/Pn//x5EkOu4j 7zRBPAVTvYEar3X6Bo8kHpVJ6xkgFrzq5QqqtO90gBQ0VwoWginCVlyMAqlvNXcdRJchighgvsk1c WvMKN/BSHes+exWX+leIhtMdjiB4uMzmzyA6x+hXGa5A7lv+tBPZWJt7D3ACZ2FKbNt1tIOfy/upc mFirKOB6hX9ndQitHtVqs5GRWa9+URyvU02bbpjBMjcA/BsVZflyJ84FjGvtsOOaF1XlIqRqRNxdX D274sqOILsuyxgf0yAvGCCQAyXY9i7HK2V3G0pSEpkdAeo+EXznb/ywsexci8LIMjQCvlXqlejog1 7sk738oQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JW0-001Pfu-25; Wed, 15 Nov 2023 17:17:24 +0000 Received: from mail-ej1-x649.google.com ([2a00:1450:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVv-001Pad-1p for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:21 +0000 Received: by mail-ej1-x649.google.com with SMTP id a640c23a62f3a-9e644d94d85so141381966b.0 for ; Wed, 15 Nov 2023 09:17:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068637; x=1700673437; darn=lists.infradead.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=MrwZCdoLZEaizVhQil7JgumATiKs+C+flcugI0w/XAk=; b=mEI+h/TFq3hu9Q2uq4LAWDNcBc9/FC4MdvjOId2pHJ2ENPM0T5pUdjJV/f1xnlEG5i Yev6idiQg1KTJDTDAtSVnRvJ7a8eK9eM0fpzt4HSZpEbh0s73kXYIodVrpB87W4PGKF/ aqkbdniPxBpckDTJ2d3IDB5ozIS1AB9sHyn2RqvoMCIQ4x3xHZ1vpuFmaOlPMmXq6JWj GfVX8B4jAAhVuKX14WkVqaUU1pvnhNgegYWyRjivyp1FuxiHpKPNXsUrgqywJuyaEjja 1o3/sJv8ZmWMYkxP/rF6VM8vTOaBuH6G+ErY3MIVEHGDvdQa6ASVYeKvlipGzDJl6ZVe 0Ckg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068637; x=1700673437; 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=MrwZCdoLZEaizVhQil7JgumATiKs+C+flcugI0w/XAk=; b=sy62TfkDw0ZrLToq1tj2b4xGM1Budck9rUisa037oemlQ8p8uEVDJ1mV7a16t2m2w3 O2A17iJuWTN+j6TUUTXrHzIO2XVdlv3c/IButpW3yGJYa3dJqjaOu/Q0XLCYnECOTNE4 +ZVcmrp6ed1PjPObXnMwTsdqBC7Ewfuae14rc2EkcaM/04YobdznfyYs6UecZHmxcJRM 7pFu4dxu9QpxxjiTXOWAnKApv/T7r3A59sR5E9jmPSuBpy/M6+QrHDbVbc+rHIHbK+yB 2TGyHxH44ojNFf9jnnpwIDBtbMdbGpTneLEKnWTk32PaybeiJIOQpu4bn5OqVIrlADiW DyrQ== X-Gm-Message-State: AOJu0YzMVn/8OtVj8/wmmXoeYNElZaFvF0MWdf4Edwv3xmeMlAXZ0rZK 1G0fNwXuUOLL6HTRqfNfr26203SZeHmWPRomZQ4= X-Google-Smtp-Source: AGHT+IEfryolzdoKiOEYL1phXIKFgud/D2FyZuB7+BdH9E+NUl2GoRL9m1zLD32m+IOZddXrx7GWlUokfDnIsB2IKDc= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a17:906:a00f:b0:9f3:6556:bc79 with SMTP id p15-20020a170906a00f00b009f36556bc79mr19663ejy.0.1700068637537; Wed, 15 Nov 2023 09:17:17 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:40 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-12-sebastianene@google.com> Subject: [PATCH v3 10/10] arm64: ptdump: Add support for 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091719_605147_445E1052 X-CRM114-Status: GOOD ( 17.67 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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/include/asm/ptdump.h | 7 ++++ arch/arm64/kvm/debug.c | 6 +++ arch/arm64/kvm/mmu.c | 2 + arch/arm64/mm/ptdump.c | 68 +++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index de5a5a0c0ecf..21b281715d27 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -5,6 +5,8 @@ #ifndef __ASM_PTDUMP_H #define __ASM_PTDUMP_H +#include + #ifdef CONFIG_PTDUMP_CORE #include @@ -23,6 +25,7 @@ struct ptdump_info { int (*ptdump_prepare_walk)(void *file_priv); void (*ptdump_end_walk)(void *file_priv); size_t mc_len; + void *priv; }; void ptdump_walk(struct seq_file *s, struct ptdump_info *info); @@ -48,8 +51,12 @@ void ptdump_check_wx(void); #ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS void ptdump_register_host_stage2(void); +int ptdump_register_guest_stage2(struct kvm *kvm); +void ptdump_unregister_guest_stage2(struct kvm_pgtable *pgt); #else static inline void ptdump_register_host_stage2(void) { } +static inline int ptdump_register_guest_stage2(struct kvm *kvm) { return 0; } +static inline void ptdump_unregister_guest_stage2(struct kvm_pgtable *pgt) { } #endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ #ifdef CONFIG_DEBUG_WX diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index 8725291cb00a..555d022f8ad9 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 ptdump_register_guest_stage2(kvm); +} diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index d87c8fcc4c24..da45050596e6 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -1021,6 +1022,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) write_unlock(&kvm->mmu_lock); if (pgt) { + ptdump_unregister_guest_stage2(pgt); kvm_pgtable_stage2_destroy(pgt); kfree(pgt); } diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index ffb87237078b..741764cff105 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -27,6 +27,7 @@ #include #include #include +#include enum address_markers_idx { @@ -519,6 +520,16 @@ void ptdump_check_wx(void) #ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS static struct ptdump_info stage2_kernel_ptdump_info; +struct ptdump_registered_guest { + struct list_head reg_list; + struct ptdump_info info; + struct kvm_pgtable_snapshot snapshot; + rwlock_t *lock; +}; + +static LIST_HEAD(ptdump_guest_list); +static DEFINE_MUTEX(ptdump_list_lock); + static phys_addr_t ptdump_host_pa(void *addr) { return __pa(addr); @@ -757,6 +768,63 @@ static void stage2_ptdump_walk(struct seq_file *s, struct ptdump_info *info) kvm_pgtable_walk(pgtable, start_ipa, end_ipa, &walker); } +static void guest_stage2_ptdump_walk(struct seq_file *s, + struct ptdump_info *info) +{ + struct ptdump_info_file_priv *f_priv = + container_of(info, struct ptdump_info_file_priv, info); + struct ptdump_registered_guest *guest = info->priv; + + f_priv->file_priv = &guest->snapshot; + + read_lock(guest->lock); + stage2_ptdump_walk(s, info); + read_unlock(guest->lock); +} + +int ptdump_register_guest_stage2(struct kvm *kvm) +{ + struct ptdump_registered_guest *guest; + struct kvm_s2_mmu *mmu = &kvm->arch.mmu; + struct kvm_pgtable *pgt = mmu->pgt; + + guest = kzalloc(sizeof(struct ptdump_registered_guest), GFP_KERNEL); + if (!guest) + return -ENOMEM; + + memcpy(&guest->snapshot.pgtable, pgt, sizeof(struct kvm_pgtable)); + guest->info = (struct ptdump_info) { + .ptdump_walk = guest_stage2_ptdump_walk, + }; + + guest->info.priv = guest; + guest->lock = &kvm->mmu_lock; + mutex_lock(&ptdump_list_lock); + + ptdump_debugfs_kvm_register(&guest->info, "stage2_page_tables", + kvm->debugfs_dentry); + + list_add(&guest->reg_list, &ptdump_guest_list); + mutex_unlock(&ptdump_list_lock); + + return 0; +} + +void ptdump_unregister_guest_stage2(struct kvm_pgtable *pgt) +{ + struct ptdump_registered_guest *guest; + + mutex_lock(&ptdump_list_lock); + list_for_each_entry(guest, &ptdump_guest_list, reg_list) { + if (guest->snapshot.pgtable.pgd == pgt->pgd) { + list_del(&guest->reg_list); + kfree(guest); + break; + } + } + mutex_unlock(&ptdump_list_lock); +} + void ptdump_register_host_stage2(void) { if (!is_protected_kvm_enabled())