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())