Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2790868rdb; Mon, 12 Feb 2024 17:41:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX3taokdBOAgfJtfeV/3wwg9jUr+Pgk5ztc2VUJFTrQcQdIlpBsmFjUn/zyekh8/eNCy9Lp3m4zguGHjKuq/ofcom2iodE/YyWMOINhow== X-Google-Smtp-Source: AGHT+IGuZYGFp7SeRCYLZ5UFIcYy6nGP0zQu7zgSklije+vCtEpEk7ws9lJeuXWs+LqMcMJgtavs X-Received: by 2002:a17:902:b48e:b0:1da:1cdf:1cca with SMTP id y14-20020a170902b48e00b001da1cdf1ccamr6629950plr.51.1707788476961; Mon, 12 Feb 2024 17:41:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707788476; cv=pass; d=google.com; s=arc-20160816; b=WDeQKIAgQxWiXkppD7qVKIYSLpUHTAWJtEzsK8TTb56VzbSgz9VOyVGUxrKjLxDpNS dvb/VAbVaK9zHPtWTQak7+H3Yj/oWX2s6GoRHyF+AtP8S2UtomjL5/gdCmzBFE3naMKR cJapqLsh3E7NiUQ+CqLLXjWgl9q1pS4uTzNB+e9kcVeKKC9P+Sw2SDAmyQ7zOj8Qm5Eu LAnvOd+pkaTy8huqRGCQE3fcn7OIDrZO/YrJTYjxA4n4590POc612d9CAIp13cgdXfbg /xyqUOrTqw2ZYzjbQquemH2RpCDOdTC2aa+BATOMoqSj+1vjKla3LYKc4uFbKNknKMx/ QpdA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:dkim-signature:date; bh=NxZyu7tPscjey5beeQmsSeREFcDJqNrkFYZl4eliD3U=; fh=/fkik48TZb3H5MSM9ux0ZFPR4p+9JOEDmNAk8viOvNM=; b=Hh/5Jij9JagtTA1pH9tshVD5dkTpCxzIPuajPpDNih3/woF3sP2LqWEuJ52z35iiX/ jbzNScHZFwNIC3apqxsgsgavpZc6gLXSX0F6wM4ef+hkqvawJlInuNUeknUndTNF+Tpi CEi5pnWgg7B35r2NsiIUGZB/oBEnEvovsaQo7Jh7Z3TPXyRqpcf77Bj5pe9f3jJtifBJ 6HiFudxuR2x400BPSsHBR+hpU6zujSkiTDv2s8s0jxs6HcREx59c2M0UxQk6SQB2edwO d9cxpThpeokui1x/y4cHehJJi26JGWwX1I9cyWD5YuHuYRSRD7O5BaDAzxS2Jv90UBVj ysWw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=bZtzy28t; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-62828-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62828-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev X-Forwarded-Encrypted: i=2; AJvYcCWv67nmc8ia9s9au0efyeBPu4gI6j8R0ERLnPPSqKm7c+eswqPpUYx8XerJIW/0Wao1kNcsVQBtrmeaLSIi2vo9BrToJn0O58anPzUOYQ== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id z13-20020a170903018d00b001d95f009fd0si1117271plg.367.2024.02.12.17.41.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 17:41:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62828-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=bZtzy28t; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-62828-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62828-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id B6070288D00 for ; Tue, 13 Feb 2024 01:37:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 68CA165BCD; Tue, 13 Feb 2024 00:56:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="bZtzy28t" Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C50D5651AB for ; Tue, 13 Feb 2024 00:56:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707785801; cv=none; b=qwtw79nGuQcEqT5uOvZRfpLm59biayQsy1wRWo4AxExVRHBU5C2J3vQzDJXRPc6xUhVQfOzikARKiPv4j1wHbIhNOEFGj4CXkpgxyd8ZGo9bvKwU0MImeh4kkGLZxdfgmW21bJ2u4Y2e9vZ2dssima8Tt1LAIvXWDTQGZWPp9AU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707785801; c=relaxed/simple; bh=cpXwYfa/s/OG0ffz6V/CvOkQmNg3H8ZvI1LfOLnOlZU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=FbzvtYGPKujIwSWvsOMLLmojMOhhIAX4g5w/KrcPQI2jUGCrhjApsIGdeH/3teBHdGs3PCA8nve/CIYnTIsHuu/wfZGdJy2qmHLXDGchOo9HGnipI18cN5Upnnfs263PNokfWsqNyubTjYqksX5O6znM/CB6JmdWpFl97DG/oP4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=bZtzy28t; arc=none smtp.client-ip=91.218.175.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Tue, 13 Feb 2024 00:56:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1707785792; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NxZyu7tPscjey5beeQmsSeREFcDJqNrkFYZl4eliD3U=; b=bZtzy28t7sioD5jzmg9WMbW0fB95PnG8PbDj0BcPYGUp2bCHLN3cvBkqqslyVFjfvgoXnn o4Gyz7e7PAWNWuonilJa4XWLJKPdgpm0N7t4C5wfHf+r9wINKHGubae1kKjan+p7gubo/t OkLkGtmBH/W47QRlNdTFXrzcH8dkl6o= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Sebastian Ene Cc: catalin.marinas@arm.com, gshan@redhat.com, james.morse@arm.com, mark.rutland@arm.com, maz@kernel.org, rananta@google.com, ricarkol@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, vdonnefort@google.com Subject: Re: [PATCH v5 3/4] KVM: arm64: Register ptdump with debugfs on guest creation Message-ID: References: <20240207144832.1017815-2-sebastianene@google.com> <20240207144832.1017815-5-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240207144832.1017815-5-sebastianene@google.com> X-Migadu-Flow: FLOW_OUT On Wed, Feb 07, 2024 at 02:48:32PM +0000, 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. > > 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 | 13 ++++++ > arch/arm64/kvm/Makefile | 1 + > arch/arm64/kvm/debug.c | 7 ++++ > arch/arm64/kvm/kvm_ptdump.h | 20 ++++++++++ > arch/arm64/kvm/ptdump.c | 79 +++++++++++++++++++++++++++++++++++++ > 5 files changed, 120 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 6c3c8ca73e7f..28097dd72174 100644 > --- a/arch/arm64/kvm/Kconfig > +++ b/arch/arm64/kvm/Kconfig > @@ -68,4 +68,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 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/Makefile b/arch/arm64/kvm/Makefile > index c0c050e53157..190eac17538c 100644 > --- a/arch/arm64/kvm/Makefile > +++ b/arch/arm64/kvm/Makefile > @@ -23,6 +23,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ > vgic/vgic-its.o vgic/vgic-debug.o > > kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o > +kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o > > always-y := hyp_constants.h hyp-constants.s > > diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c > index 8725291cb00a..aef52836cd90 100644 > --- a/arch/arm64/kvm/debug.c > +++ b/arch/arm64/kvm/debug.c > @@ -14,6 +14,8 @@ > #include > #include > > +#include > + > #include "trace.h" > > /* These are the bits of MDSCR_EL1 we may manipulate */ > @@ -342,3 +344,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 kvm_ptdump_guest_register(kvm); > +} > diff --git a/arch/arm64/kvm/kvm_ptdump.h b/arch/arm64/kvm/kvm_ptdump.h > new file mode 100644 > index 000000000000..a7c00a28481b > --- /dev/null > +++ b/arch/arm64/kvm/kvm_ptdump.h > @@ -0,0 +1,20 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Copyright (C) Google, 2023 > + * Author: Sebastian Ene > + */ > + > +#ifndef __KVM_PTDUMP_H > +#define __KVM_PTDUMP_H > + > +#include > +#include > + > + > +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS > +int kvm_ptdump_guest_register(struct kvm *kvm); > +#else > +static inline int kvm_ptdump_guest_register(struct kvm *kvm) { return 0; } > +#endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ > + > +#endif /* __KVM_PTDUMP_H */ > diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c > new file mode 100644 > index 000000000000..a4e984da8aa7 > --- /dev/null > +++ b/arch/arm64/kvm/ptdump.c > @@ -0,0 +1,79 @@ > +// 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, 2023 > +// Author: Sebastian Ene Same comment as last time about ... the comment :) Should be of the form /* * */ > +#include > +#include > +#include > + > +#include is this needed? > +#include > + > + > +static int kvm_ptdump_guest_open(struct inode *inode, struct file *file); > +static int kvm_ptdump_guest_show(struct seq_file *m, void *); can you structure the file in a way to avoid forward declarations? > +static const struct file_operations kvm_ptdump_guest_fops = { > + .open = kvm_ptdump_guest_open, > + .read = seq_read, > + .llseek = seq_lseek, > + .release = single_release, > +}; > + > +static int kvm_ptdump_guest_open(struct inode *inode, struct file *file) > +{ > + return single_open(file, kvm_ptdump_guest_show, inode->i_private); > +} > + Shouldn't we take a reference on the KVM struct at open to avoid UAF? struct kvm *kvm = inode->i_private; if (!kvm_get_kvm_safe(kvm)) return -ENOENT; Then you can do a put on it at close(). > +static int kvm_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; > + > + note_page(pt_st, ctx->addr, ctx->level, ctx->old); > + return 0; > +} > + > +static int kvm_ptdump_show_common(struct seq_file *m, > + struct kvm_pgtable *pgtable, > + struct 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, > + }; > + > + return kvm_pgtable_walk(pgtable, 0, BIT(pgtable->ia_bits), &walker); > +} > + > +static int kvm_ptdump_guest_show(struct seq_file *m, void *) > +{ > + struct kvm *guest_kvm = m->private; > + struct kvm_s2_mmu *mmu = &guest_kvm->arch.mmu; > + struct pg_state parser_state = {0}; > + int ret; > + > + write_lock(&guest_kvm->mmu_lock); > + ret = kvm_ptdump_show_common(m, mmu->pgt, &parser_state); > + write_unlock(&guest_kvm->mmu_lock); > + > + return ret; > +} > + > +int kvm_ptdump_guest_register(struct kvm *kvm) > +{ > + struct dentry *parent; > + > + parent = debugfs_create_file("stage2_page_tables", 0400, > + kvm->debugfs_dentry, kvm, > + &kvm_ptdump_guest_fops); > + if (IS_ERR(parent)) > + return PTR_ERR(parent); This makes the otherwise benign debugfs failure into something fatal for VM creation, no? From the documentation on debugfs_create_file(): * NOTE: it's expected that most callers should _ignore_ the errors returned * by this function. Other debugfs functions handle the fact that the "dentry" * passed to them could be an error and they don't crash in that case. * Drivers should generally work fine even if debugfs fails to init anyway. The fact that kvm_arch_create_vm_debugfs() has a return value is a bit of an anti-pattern to begin with. -- Thanks, Oliver