From patchwork Wed Feb 7 14:48:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 197958 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2279841dyb; Wed, 7 Feb 2024 06:50:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IHg6KiGkhdi+umeNBVrW4AJhUxDB3QFOS1n2Oo4v8UYp/KPut+T8pIURvyi/towuolJij/k X-Received: by 2002:a25:361f:0:b0:dc2:2799:981a with SMTP id d31-20020a25361f000000b00dc22799981amr5012711yba.18.1707317427575; Wed, 07 Feb 2024 06:50:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707317427; cv=pass; d=google.com; s=arc-20160816; b=ttvdJ+bt0f27qU0ime+RLXH6eqa6jT7eee/lVDn+wMOg5hFb7DcQ/mqJMkTfSd4qma dESnYR53EIeQTar5bemDrtejqk9aA+X4pilfMh2jYT+4NSwFzSId8brzEbLZH4Y9Zwjc HVwtcLS7g6/npH07Bh8mWgZfy/mx7otRYQ74UEhKyqNiJ19RXU6kiUOW2m92NXc9ce2q TEnHaaeCDx+jHlHVhHYsi0HJlZeAEvC03k0xQvTj8kgLUzeVHPIt6P0tzS7kGMDyHHYC mG5BiPBEPmkmXnBGE2vIymBvCmwgL3wvWjD2xpk+4PyTd/JPKkEJELVoybAPGYA9yWxq cMjQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=J1F3sgOZBAQMU7R2Rlac0l+wJat72mlIsX+wYVgtE5c=; fh=SvCmgZovs3mGa+N9+o6udi4hmqRZ3fFxl+Mt6Ywoiss=; b=zTSErMQ4AHs1cosC33PPFTk/+RXBiINAuADhb4QjwBO9W0euaJ6KnjH7lc+dC//1kf X5ZAFcURmv2AGs/6Hdku78nH1V57WM3PLoMlsdtzoFukjnsqpDKAteesUvr9cHFIN8nM Pc5U8cmGgFbZAvxGWo+IhADDJN/wzqQ7WC1rcakZCCYOhhlVRhK9L4Ai0MWknVh5S734 FHyj/E14no4j9Ay5lqeiHj2zHy5LhYKHFdjEeirzKBh66SQULqOukTqELMSHpNKXzRp5 VnK4n/v5ijSfHG4XxM0Y/rdIyGrCpVEhaV9pb/vH05+4j8rJLA1uQMNleWJoWuzlwjTs tD+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mT7hu7CU; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUONMmRkEUiG8/btDRWUGgOY4/ls6Tu05UWmT1A9iSarylresK6aRtgv4XTpd+ZHJWOtEHXo3OJlGDvbhhsEUy78hOlAQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id fq12-20020a056214258c00b0068c67a0fc45si1298545qvb.572.2024.02.07.06.50.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 06:50:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mT7hu7CU; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 4E0F71C20A10 for ; Wed, 7 Feb 2024 14:50:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 755E27FBD5; Wed, 7 Feb 2024 14:49:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mT7hu7CU" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 364CE7F7CB for ; Wed, 7 Feb 2024 14:49:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317366; cv=none; b=NmKdxB23+vheXpX/znlkzYogfLYxYeHgoZCmlIyzMMGvP7sYYN/1Vo2nKNUaa/IWQc25MjKZKjcqdY+4oKPFf/dWiGkykoP4M81Qv53bNHIYU+CYMDry1lJLLRXKLWc6vz+pyG1NL4IJLbuLMCLWOe7Jim2ztOu2tvG6C0mfXrs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317366; c=relaxed/simple; bh=rNGGZUY2dBsJq36eaQaTKYVIz8wf7Lh+KRhuF6gF5W8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uhogigeS7Z02GU5TAarZXSBQ4WjKuW93vl1qFnXgESzuHNWDzFjDHuZ/dxSPlddUdwN0ysYamkogJSrF1MxqhFSOfZvfCFH70hJus8PUoNsBK7ERRW9nn63P3wb4n/YS7nP1QtEFa44nlD8v8HzlnNHkJ75QQpwnpePUErXjpGQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mT7hu7CU; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60484a61250so13945727b3.1 for ; Wed, 07 Feb 2024 06:49:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707317364; x=1707922164; 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=J1F3sgOZBAQMU7R2Rlac0l+wJat72mlIsX+wYVgtE5c=; b=mT7hu7CUGFOIS+XLPewRqWmTVYeEXNsJCow9FtMkZvi4rOgj5qMvlSo2CPDrEJHc3A T1J7oTJWracl5M8TZxDssHBdqeahrPGQfMAqwDctQRsnOEup2inS0VUxWApvwGpEYpsq qAn+aBEmza8uX/stXyWMuCB+t4zjqDhKqojNLNif57sBKFoY5ek6YMMFDdvAT/yi4X2x vWZCJq17Auf9wH4d+YmtqRcrpBCCdeW1zg5+YKuZIMNNtfr2svs7KaVsIXDboXZJl8rZ aZmMF4L8iDs/SmzUTgheCZKErzuvzSS6AUhBGCvNnQxRK9khFTdkw4IqdIJ2A/QsgdEO Pn8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707317364; x=1707922164; 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=J1F3sgOZBAQMU7R2Rlac0l+wJat72mlIsX+wYVgtE5c=; b=cqCod8HM7TMCNxXvRjPNGZePm9nAgq8DildUYknDIoJ84eOH26Jm3TrbMZV8pa0iCR ymUlfFM9bqDcIsIXnQ+jwoaUH44OXNOLyYjN2fpLdlL1MPUgMofPiblriHtSQDodfaix QHw8xYqJSc8P2cuPqUGwUDVggHTJ7FvHG6jwxUNmVE/teJmluGSYnwlB6yJ2nWH1e5+W aebgjbIM6K806r+AW5EgpMl5s1aSOMzDFnY7o5E4VeHCWstg9rQBkJeU4A7NE6KkABra BR2FNwH6jDjIaS6DfW1V+uBA0geOfT0qVe5ZvCPlgq5X/DdPj7PSpjtWPHpoT5dzP09s k1XA== X-Gm-Message-State: AOJu0YzOuyQuVqGUo2od9oJl2MgTA2LPgUdD0+1oFym0H5XWb1hfjE6m DGjFAIDmodq8+EqPIPVd0deq47zvrH/2Ama/+bB5FUjoehzbVNxTcgwt0r1s2P8UwpkrvxbDoPJ +uIyMlRt0g52rlmOc53aHONpkEA== X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6902:2187:b0:dc2:1cd6:346e with SMTP id dl7-20020a056902218700b00dc21cd6346emr1222350ybb.8.1707317364293; Wed, 07 Feb 2024 06:49:24 -0800 (PST) Date: Wed, 7 Feb 2024 14:48:32 +0000 In-Reply-To: <20240207144832.1017815-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240207144832.1017815-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240207144832.1017815-5-sebastianene@google.com> Subject: [PATCH v5 3/4] KVM: arm64: Register ptdump with debugfs on guest creation From: Sebastian Ene To: catalin.marinas@arm.com, gshan@redhat.com, james.morse@arm.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ricarkol@google.com, ryan.roberts@arm.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790252078665693786 X-GMAIL-MSGID: 1790252078665693786 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 + +#include +#include +#include + +#include +#include + + +static int kvm_ptdump_guest_open(struct inode *inode, struct file *file); +static int kvm_ptdump_guest_show(struct seq_file *m, void *); + +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); +} + +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); + return 0; +}