From mboxrd@z Thu Jan 1 00:00:00 1970 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 D9F0BC3DA4A for ; Tue, 20 Aug 2024 14:10:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=6ioDMW4q/n+REoe6r2Lpqex9Ea3PyoRpd3/0n/Az01I=; b=dj37S0bPxZJ/12UbgTBtVney1c No1iK9LR3z0gPKdwS8Fxj3+EMglQCJ+d55ed7ZDNlSTjJ9UrELQAd5ST3zNhoXEcJwjYB+OReKNmR ojGCV40XIy/e5IQzx5K+cGyoVPaRh1Oh/tVenFuVNhfdpnaVPa8xdPLbMe7sWaG98MVW4cNDtnxNO tOStvsWAj7yZXHus3Q2b6rmY/0ruZM5IuG/46sz4vBPZK1KkST/zinfUzk06rCrVLkS5n9CFn+5wt 1MtHZ4gJNKUljmNyeuDHo9bD9G+bJe79138+/YqjUNoWqZvZJrZ4vQYHfq1HCw1KX2TMaZhyqeei/ /weJC94Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgPYT-00000005UJw-3mvt; Tue, 20 Aug 2024 14:09:49 +0000 Received: from sin.source.kernel.org ([2604:1380:40e1:4800::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgPVc-00000005TNU-1YEi for linux-arm-kernel@lists.infradead.org; Tue, 20 Aug 2024 14:06:54 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 0A6AECE09CD; Tue, 20 Aug 2024 14:06:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C15B3C4AF0F; Tue, 20 Aug 2024 14:06:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724162808; bh=EM4p/wmqIOkPlzWiX5O0ERZflz0MZ3+ngnfORf2TNRc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=CxQ+HFQOLA70vUPJqQuU0tZlXYMRSE356mjRzcbRLxSU9vr/8YC7nCtnnu1oaMfGf 5lZX6nti8ys5MQkrq9FSFCurjgKM8eP+pYPMeZG+woZ1h1un/aGXlK5qsp4sW5jILF VPqbPOGQD4quZfe0WdRAJvxwC+XC1k8sRGEVEzZ+h6DZCUE5kN6OeC6rA8T4ietbXC FeB/lzNnYkXEJ4fTb+HopCfMQtlJ2vlH/Ri+vPjvm2EvXO1tF8S4JPXlAHgne8t8UA Cbc5cRRPmldL3FYLRO6KcqeEZdMZKgK5sCpit1ireFz1WolL3qKMdsXz/16ZVAZtAj /9NjuUO/JTTEw== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1sgPVW-005I9X-AR; Tue, 20 Aug 2024 15:06:46 +0100 Date: Tue, 20 Aug 2024 15:06:45 +0100 Message-ID: <86r0ajxp8q.wl-maz@kernel.org> From: Marc Zyngier To: Sebastian Ene Cc: akpm@linux-foundation.org, alexghiti@rivosinc.com, ankita@nvidia.com, ardb@kernel.org, catalin.marinas@arm.com, christophe.leroy@csgroup.eu, james.morse@arm.com, vdonnefort@google.com, mark.rutland@arm.com, oliver.upton@linux.dev, rananta@google.com, ryan.roberts@arm.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v8 4/6] KVM: arm64: Register ptdump with debugfs on guest creation In-Reply-To: <20240816123906.3683425-5-sebastianene@google.com> References: <20240816123906.3683425-1-sebastianene@google.com> <20240816123906.3683425-5-sebastianene@google.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/29.4 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: sebastianene@google.com, akpm@linux-foundation.org, alexghiti@rivosinc.com, ankita@nvidia.com, ardb@kernel.org, catalin.marinas@arm.com, christophe.leroy@csgroup.eu, james.morse@arm.com, vdonnefort@google.com, mark.rutland@arm.com, oliver.upton@linux.dev, rananta@google.com, ryan.roberts@arm.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240820_070652_785343_21CA016F X-CRM114-Status: GOOD ( 42.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 On Fri, 16 Aug 2024 13:39:04 +0100, Sebastian Ene wrote: > > While arch/*/mem/ptdump handles the kernel pagetable dumping code, > introduce KVM/ptdump which shows the guest stage-2 pagetables. The > separation is necessary because most of the definitions from the > stage-2 pagetable reside in the KVM path and we will be invoking > functionality **specific** to KVM. Drop the ** emphasis. > > When a guest is created, register a new file entry under the guest > debugfs dir which allows userspace to show the contents of the guest > stage-2 pagetables when accessed. > > Signed-off-by: Sebastian Ene > --- > arch/arm64/kvm/Kconfig | 14 ++++++ > arch/arm64/kvm/Makefile | 1 + > arch/arm64/kvm/arm.c | 2 + > arch/arm64/kvm/kvm_ptdump.h | 20 ++++++++ > arch/arm64/kvm/ptdump.c | 91 +++++++++++++++++++++++++++++++++++++ > 5 files changed, 128 insertions(+) > create mode 100644 arch/arm64/kvm/kvm_ptdump.h > create mode 100644 arch/arm64/kvm/ptdump.c > > diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig > index 8304eb342be9..fcc41e58ede6 100644 > --- a/arch/arm64/kvm/Kconfig > +++ b/arch/arm64/kvm/Kconfig > @@ -66,4 +66,18 @@ 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 KVM > + select PTDUMP_CORE This looks wrong. Looking at PTDUMP_DEBUGFS, it has the following constraints: depends on DEBUG_KERNEL depends on DEBUG_FS depends on GENERIC_PTDUMP select PTDUMP_CORE I don't see why the Stage-2 version should have anything different. > + 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. nit: try to keep the formatting within 80 columns. More importantly, I find it very strange to expose the configuration option so early in the series, while the support code isn't there yet. You can perfectly introduce code that is conditional on a config option and only add it at the end. > + > + If in doubt, say N. > + > endif # VIRTUALIZATION > diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile > index 86a629aaf0a1..e4233b323a73 100644 > --- a/arch/arm64/kvm/Makefile > +++ b/arch/arm64/kvm/Makefile > @@ -27,6 +27,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ > > kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o > kvm-$(CONFIG_ARM64_PTR_AUTH) += pauth.o > +kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o > > always-y := hyp_constants.h hyp-constants.s > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 9bef7638342e..60fed2146763 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -45,6 +45,7 @@ > #include > #include > #include > +#include > > static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT; > > @@ -228,6 +229,7 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) > void kvm_arch_create_vm_debugfs(struct kvm *kvm) > { > kvm_sys_regs_create_debugfs(kvm); > + kvm_ptdump_guest_register(kvm); Consider using a name that is homogeneous with what we already have (kvm_s2_ptdump_create_debugfs?). > } > > static void kvm_destroy_mpidr_data(struct kvm *kvm) > diff --git a/arch/arm64/kvm/kvm_ptdump.h b/arch/arm64/kvm/kvm_ptdump.h > new file mode 100644 > index 000000000000..0a62b0e2908c > --- /dev/null > +++ b/arch/arm64/kvm/kvm_ptdump.h > @@ -0,0 +1,20 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Copyright (C) Google, 2024 > + * Author: Sebastian Ene > + */ > + > +#ifndef __KVM_PTDUMP_H > +#define __KVM_PTDUMP_H > + > +#include > +#include > + > + > +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS > +void kvm_ptdump_guest_register(struct kvm *kvm); > +#else > +static inline void kvm_ptdump_guest_register(struct kvm *kvm) {} > +#endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ > + > +#endif /* __KVM_PTDUMP_H */ Please don't add new include files that contain so little stuff. These things may as well be added either to asm/kvm_host.h or asm/ptdump.h. > diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c > new file mode 100644 > index 000000000000..52483d56be2e > --- /dev/null > +++ b/arch/arm64/kvm/ptdump.c > @@ -0,0 +1,91 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Debug helper used to dump the stage-2 pagetables of the system and their > + * associated permissions. > + * > + * Copyright (C) Google, 2024 > + * Author: Sebastian Ene > + */ > +#include > +#include > +#include > + > +#include > +#include > + > + > +static int kvm_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx, > + enum kvm_pgtable_walk_flags visit) > +{ > + struct ptdump_pg_state *st = ctx->arg; > + struct ptdump_state *pt_st = &st->ptdump; > + > + note_page(pt_st, ctx->addr, ctx->level, ctx->old); > + return 0; > +} > + > +static int kvm_ptdump_show_common(struct seq_file *m, What does "common" mean here? You have exactly *one* caller, so why isn't that inlined in kvm_ptdump_guest_show()? > + struct kvm_pgtable *pgtable, > + struct ptdump_pg_state *parser_state) > +{ > + struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) { > + .cb = kvm_ptdump_visitor, > + .arg = parser_state, > + .flags = KVM_PGTABLE_WALK_LEAF, > + }; > + > + parser_state->level = -1; > + parser_state->start_address = 0; > + > + return kvm_pgtable_walk(pgtable, 0, BIT(pgtable->ia_bits), &walker); > +} > + > +static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) > +{ > + struct kvm *kvm = m->private; > + struct kvm_s2_mmu *mmu = &kvm->arch.mmu; > + struct ptdump_pg_state parser_state = {0}; nit: the common idiom is "parser_state = {}". > + int ret; > + > + write_lock(&kvm->mmu_lock); > + ret = kvm_ptdump_show_common(m, mmu->pgt, &parser_state); > + write_unlock(&kvm->mmu_lock); > + > + return ret; > +} > + > +static int kvm_ptdump_guest_open(struct inode *m, struct file *file) > +{ > + struct kvm *kvm = m->i_private; > + int ret; > + > + if (!kvm_get_kvm_safe(kvm)) > + return -ENOENT; > + > + ret = single_open(file, kvm_ptdump_guest_show, m->i_private); > + if (ret < 0) > + kvm_put_kvm(kvm); > + > + return ret; > +} > + > +static int kvm_ptdump_guest_close(struct inode *m, struct file *file) > +{ > + struct kvm *kvm = m->i_private; > + > + kvm_put_kvm(kvm); > + return single_release(m, file); > +} > + > +static const struct file_operations kvm_ptdump_guest_fops = { > + .open = kvm_ptdump_guest_open, > + .read = seq_read, > + .llseek = seq_lseek, > + .release = kvm_ptdump_guest_close, > +}; > + > +void kvm_ptdump_guest_register(struct kvm *kvm) > +{ > + debugfs_create_file("stage2_page_tables", 0400, kvm->debugfs_dentry, > + kvm, &kvm_ptdump_guest_fops); > +} > -- > 2.46.0.184.g6999bdac58-goog Overall, I have a hard time understanding what this does. It walks the page tables, but doesn't do anything useful. I have the feeling that this patch would be better squashed together with patch #5, since it significantly reworks what patch #4 does. Thanks, M. -- Without deviation from the norm, progress is not possible.