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: 165520 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp2687388vqg; Wed, 15 Nov 2023 09:17:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IHLSys3+1fe8NAO+AJZTtOpSyfObypaZRZJhK2o3mIoHU+lfiU/NxPS3ObXGyhTfnmY+kIh X-Received: by 2002:a05:6a20:9183:b0:160:97a3:cae9 with SMTP id v3-20020a056a20918300b0016097a3cae9mr15669959pzd.54.1700068671873; Wed, 15 Nov 2023 09:17:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700068671; cv=none; d=google.com; s=arc-20160816; b=uNTxQwGl7E+K+15AM1gHR8k8Lxf3YbsJnuOYd7VS6Iit0v4PKfQlM7sG0sBAraTQ+n mrXi9AVa4NpI5hkHo5oDFKAlDKv5LV+tJtU8LVLXDoWTBFAt7hBTfxaptlEilZBrRAp2 zr9MABbPqMy1MpzhIoKAJkQGmrzI01nSiMOmPQ5fOCZcg2rpuYddWvezC1GsvNq3J4W/ 59NpTiFgHzBWG3WEOuHVvO1banba2ylpR3BfIjIi2PNpP8kIW0XnYLAiRlUtkYX5IZDb dJ7NDOOQstTdpzl7XH9p28H4Xn6CF1Jt6RJT6VR17mh23invAtqskD3A+mnr2Wd+W1H5 dm+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=MrwZCdoLZEaizVhQil7JgumATiKs+C+flcugI0w/XAk=; fh=4RXM1FFjr/2FDx2f50K7N78eRirWaG15JMQkQwGVT00=; b=ZKghhsHWCndDdWLsh41caZq6FF3ja9WSKKnxaby4QB8QCzeDqWtxkWeWSmauXWTYUg orAIjZ/lw6fWED5+Fn2A0x1TmQFzVxxv1QCrXh/tG1aSWuwubp3YylpSPSs5I9NJRC/G hWVcU3rG9Bahaa7Y84EwrATLGerjWGlzu3/aILRJFd92Rlsm29ENOWiFlu6SatdwPNSN xlkTD7nlbGQdt+JRhm9OoA1bNpldweVJ56cpPeD035cj3MUdwFkpAxDKTjpIRRXBTAAf aNcsfJD27a6IanTF/gWpQi4rcNwZqmofNaUIh/V4fHNrWnP0vol+jiPgNaxB1J+GtAfo XXBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ilaHSqDQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id n4-20020a62e504000000b0068a590d8043si10064112pff.375.2023.11.15.09.17.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 09:17:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ilaHSqDQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id C1F5A8218E6D; Wed, 15 Nov 2023 09:17:50 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232718AbjKORRr (ORCPT + 29 others); Wed, 15 Nov 2023 12:17:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232882AbjKORRa (ORCPT ); Wed, 15 Nov 2023 12:17:30 -0500 Received: from mail-ej1-x64a.google.com (mail-ej1-x64a.google.com [IPv6:2a00:1450:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2DA11B5 for ; Wed, 15 Nov 2023 09:17:19 -0800 (PST) Received: by mail-ej1-x64a.google.com with SMTP id a640c23a62f3a-9e5e190458dso38646566b.1 for ; Wed, 15 Nov 2023 09:17:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068637; x=1700673437; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MrwZCdoLZEaizVhQil7JgumATiKs+C+flcugI0w/XAk=; b=ilaHSqDQ5bh0Ymcf6aEog7jgYtJflF17F5Juuav/YzOzQEn1y7Z5C/2bDzQPbsgFWD TXapbLxAth4ruLN5P7kBZezRU5Io6lMb83N+ed/48kKSNCRvq0PM65DWNTzTOvQSpkWA 6vu7ftwE6fOnLXU9VfdHFQeZgfSTnn8oBVFGRG1XkO1yaPN39pOaZLqK3Z/ut3kXoLqe 1LEFbKSxPsZsgcGV/dQcoNboMiwqdgHq4v+ELt0akb6HQTfVISixxyq3nS+HKRRzcwiW /G2sTi4GR5YT9x0zZWzuK1knRARU5TLSwRn6iwD60h6PBCSNchMXObMGKEf2jTyU/FaZ a79A== 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=oKG8Yyovtcde3w8zPgvDZUSZJ4Wn76dltr8kCSSoLRsVmtiKqFMWao+lp8wlqWdXCP 9OI4tkb3pHGtziY0PoQs1dJSDOrrpavXuQFYGtV1xwl2hqAfRpiu8clc9ZpmdPzp1WPR wVwFfZfJ2xjcUju9IkL/UBRWSEO/tvHcoSn5pDhCMmO3dg7//bUysCgKzRVY7Ie3OVk9 RBZLuXiQUdRHbdZwd8pJNCjDfANvNQpwpwPtp9RcBQcDWuYN/iFGoTJmwPSGhPyBYnXf SKgdsiC3tX91PUfqLz8EmT24/eCjLroAxf/wXEz0nfFd6gjXt3c+EV+8ke7QIF1dybOk z5hw== X-Gm-Message-State: AOJu0YzrEw+Wag1KmSxd18KBNosEMZoCQ1Gu8EKFv/9uf//Nil+lMjTU QMPVGCR7qgohNXqn0kESrgrDxjqbU9O/WyX2Mjs= 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-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 15 Nov 2023 09:17:50 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782651207354480844 X-GMAIL-MSGID: 1782651207354480844 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())