From patchwork Thu Oct 19 14:40: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: 13429348 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 913EFCDB482 for ; Thu, 19 Oct 2023 14:41: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=q0OIuVN19bLdBmsEFDp/llnMZyXJlz1S8y67tv/syhc=; b=2YyssJdOJBKC9N4LZZc3yXmLTa 4RndKKbLlvBJS4cUlkFS5X/0Ajf9In5jmZ0F1xyq+dairZ/nT54cKFdqkHiDCDNE+yTwiq1VAGG9O foYi/nNfRXSQjORVLw1k87EO59272+Zkv+PZVhlFCnvwuSb8vmqkeVKDHbyqwgbDPcccKheB8RFmv q7kT2QrUt58uU5l/7uCHwU/7+8MyYW5otePwLSjxMI2mdHvJwozaFmlX0UgQyHXGnG8vi+mSvzSmO 6qfzzKmJ1cMxEMb7GKpSw7mZUMXUoonS8xhs8h6r49mkd8oIapHkZqVtndVoW/hLp4fZQpzLdWHYV jvmPBamw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qtUDH-0006aN-2O; Thu, 19 Oct 2023 14:41:27 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qtUD1-0006Ow-34 for linux-arm-kernel@lists.infradead.org; Thu, 19 Oct 2023 14:41:13 +0000 Received: by mail-wr1-x44a.google.com with SMTP id ffacd0b85a97d-32d9751ca2cso4203292f8f.3 for ; Thu, 19 Oct 2023 07:41:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697726470; x=1698331270; 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=2ZQaWaMMDpRzuXNNy9jw27XP1P5ogxswbyoIw1t3Xt4=; b=kFkJV7J2VyXaqXKsttjyv+CAz1ykx50OrWn0T8X123S9hkQlppjJ7FZr9jCfkNzyWc aE7tDJrABPGsMiTXIlzWM1YVFn3DvJOk5jfIZEscXAIBi8HNhFk5dm+NODRBgqrfzT3p bsS7SWAaDKwU8gI66OHwyXgFoXg5oCAiLAqR1XPvrAea/bSydh8jppDoPqUoe2hMoCJ8 JiI4z2VqGmJG8x+PZ0EAC2nSNi/f3KD8hVxoBHg1LA62AjpLU806VYxguh8cqz0/2Lws 4eEz98h+2rKxcmkVUjmveR1ZvCjMHwihADyRUcIMH9qvr0ZPQMDSY6gC959b3EkJKLGl vypw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697726470; x=1698331270; 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=2ZQaWaMMDpRzuXNNy9jw27XP1P5ogxswbyoIw1t3Xt4=; b=qe3xFqJgnBXhMmTB07C7KRPDKcuyB4vc1liTIVKyp294Q/YO2OXdAj6/dUsSbJ8NM8 kjWn1Jva7rAbaZI+oBFy0VQMLMO4A+zPUoQsivyNRxQZIF7o8wjMWk1nB0AfNvIz27zQ G3houltmJywT22vSYXK11VdYjkK/UuwRtQe5+qI0kzC1nHQRL2WSpeAsvZnG4reXLPV7 J2OJEKOwiTORVKChEwEqU2tIHDY4rd/RvYj9+T7Oyk7xX9NRZZ4HBy+7olcQQSv1Wkh/ sk1G7rz6hxPjBSImHjZ1rXLKlphQmzJN6zSIg3RidhLwtbw28Is8cF5Xc6Jcl7kgZ7RC fLmA== X-Gm-Message-State: AOJu0YxribVC6ZXmIpKIvIDoVXFCwWdk1kH3+OPL1b89bNE8a4pJvaww 2rHJNWfruZp78DQOIPy7RtTB3wXuH23yA5u0Hoc= X-Google-Smtp-Source: AGHT+IHm/YPb6RDXSSOgrhEsZk0YOvHrgtDHnuFNWAhiH6G8cDKgjYCVmKppyzugBPhyHZ6uqpnOMhmCTRKSAsqjgiw= X-Received: from seb.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:31bd]) (user=sebastianene job=sendgmr) by 2002:a05:6000:1b08:b0:32d:6e24:79e8 with SMTP id f8-20020a0560001b0800b0032d6e2479e8mr20116wrz.12.1697726470227; Thu, 19 Oct 2023 07:41:10 -0700 (PDT) Date: Thu, 19 Oct 2023 14:40:33 +0000 In-Reply-To: <20231019144032.2943044-1-sebastianene@google.com> Mime-Version: 1.0 References: <20231019144032.2943044-1-sebastianene@google.com> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog Message-ID: <20231019144032.2943044-13-sebastianene@google.com> Subject: [PATCH v2 11/11] arm64: ptdump: Add support for guest stage-2 pagetables dumping From: Sebastian Ene To: will@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com, akpm@linux-foundation.org, maz@kernel.org Cc: 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-20231019_074111_993454_A1715487 X-CRM114-Status: GOOD ( 21.37 ) 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 | 21 +++++++-- arch/arm64/kvm/mmu.c | 3 ++ arch/arm64/mm/ptdump.c | 84 +++++++++++++++++++++++++++++++++ arch/arm64/mm/ptdump_debugfs.c | 5 +- 4 files changed, 108 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 35b883524462..be86244d532b 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 @@ -30,14 +32,27 @@ struct ptdump_info { void ptdump_walk(struct seq_file *s, struct ptdump_info *info); #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); +struct dentry *ptdump_debugfs_register(struct ptdump_info *info, + const char *name); #else -static inline void ptdump_debugfs_register(struct ptdump_info *info, - const char *name) { } +static inline struct dentry *ptdump_debugfs_register(struct ptdump_info *info, + const char *name) +{ + return NULL; +} #endif void ptdump_check_wx(void); #endif /* CONFIG_PTDUMP_CORE */ +#ifdef CONFIG_NVHE_EL2_PTDUMP_DEBUGFS +void ptdump_register_guest_stage2(struct kvm_pgtable *pgt, void *lock); +void ptdump_unregister_guest_stage2(struct kvm_pgtable *pgt); +#else +static inline void ptdump_register_guest_stage2(struct kvm_pgtable *pgt, + void *lock) { } +static inline void ptdump_unregister_guest_stage2(struct kvm_pgtable *pgt) { } +#endif /* CONFIG_NVHE_EL2_PTDUMP_DEBUGFS */ + #ifdef CONFIG_DEBUG_WX #define debug_checkwx() ptdump_check_wx() #else diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 482280fe22d7..e47988dba34d 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -908,6 +909,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t if (err) goto out_free_pgtable; + ptdump_register_guest_stage2(pgt, &kvm->mmu_lock); mmu->last_vcpu_ran = alloc_percpu(typeof(*mmu->last_vcpu_ran)); if (!mmu->last_vcpu_ran) { err = -ENOMEM; @@ -1021,6 +1023,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 4687840dcb69..facfb15468f5 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -26,6 +26,7 @@ #include #include #include +#include enum address_markers_idx { @@ -543,6 +544,22 @@ void ptdump_check_wx(void) #ifdef CONFIG_NVHE_EL2_PTDUMP_DEBUGFS static struct ptdump_info stage2_kernel_ptdump_info; +#define GUEST_NAME_LEN (32U) + +struct ptdump_registered_guest { + struct list_head reg_list; + struct ptdump_info info; + struct mm_struct mem; + struct kvm_pgtable_snapshot snapshot; + struct dentry *dentry; + rwlock_t *lock; + char reg_name[GUEST_NAME_LEN]; +}; + +static LIST_HEAD(ptdump_guest_list); +static DEFINE_MUTEX(ptdump_list_lock); +static u16 guest_no; + static phys_addr_t ptdump_host_pa(void *addr) { return __pa(addr); @@ -740,6 +757,73 @@ 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 kvm_pgtable_snapshot *snapshot = info->priv; + struct ptdump_registered_guest *guest; + + guest = container_of(snapshot, struct ptdump_registered_guest, + snapshot); + read_lock(guest->lock); + stage2_ptdump_walk(s, info); + read_unlock(guest->lock); +} + +void ptdump_register_guest_stage2(struct kvm_pgtable *pgt, void *lock) +{ + struct ptdump_registered_guest *guest; + struct dentry *d; + + if (pgt == NULL || lock == NULL) + return; + + guest = kzalloc(sizeof(struct ptdump_registered_guest), GFP_KERNEL); + if (!guest) + return; + + memcpy(&guest->snapshot.pgtable, pgt, sizeof(struct kvm_pgtable)); + guest->info = (struct ptdump_info) { + .ptdump_walk = guest_stage2_ptdump_walk, + .priv = &guest->snapshot + }; + + mutex_init(&guest->info.file_lock); + guest->lock = lock; + mutex_lock(&ptdump_list_lock); + snprintf(guest->reg_name, GUEST_NAME_LEN, + "%u_guest_stage2_page_tables", guest_no++); + d = ptdump_debugfs_register(&guest->info, guest->reg_name); + if (!d) { + mutex_unlock(&ptdump_list_lock); + goto free_entry; + } + + guest->dentry = d; + list_add(&guest->reg_list, &ptdump_guest_list); + mutex_unlock(&ptdump_list_lock); + return; + +free_entry: + kfree(guest); +} + +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); + debugfs_remove(guest->dentry); + kfree(guest); + break; + } + } + mutex_unlock(&ptdump_list_lock); +} #endif /* CONFIG_NVHE_EL2_PTDUMP_DEBUGFS */ static void __init ptdump_register_host_stage2(void) diff --git a/arch/arm64/mm/ptdump_debugfs.c b/arch/arm64/mm/ptdump_debugfs.c index 14619452dd8d..356753e27dee 100644 --- a/arch/arm64/mm/ptdump_debugfs.c +++ b/arch/arm64/mm/ptdump_debugfs.c @@ -49,7 +49,8 @@ static const struct file_operations ptdump_fops = { .release = ptdump_release, }; -void __init ptdump_debugfs_register(struct ptdump_info *info, const char *name) +struct dentry *ptdump_debugfs_register(struct ptdump_info *info, + const char *name) { - debugfs_create_file(name, 0400, NULL, info, &ptdump_fops); + return debugfs_create_file(name, 0400, NULL, info, &ptdump_fops); }