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 3F00AC3DA59 for ; Fri, 19 Jul 2024 14:01:59 +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:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From: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=Uc/lvxk1g3HuegnXaisJU/OL2Ih+Ws0A6QMr/ME/lAs=; b=tYRecvzvH6BRTFJy3uF70+yRdQ D2BjKF6YYN3avTqwG7gSZ7iO+yUJqCB1WRIv5cZirmOEwiBFMZfWAqVl86ueJg3b/pRQGwpeIZmqK s/EdtftH0nbCwtUdu+KVbltTqZDDy2JUGAYpVEcBPuSR+BdLYSf+vTjAP3/+stf6cF2MDkN18EEdI ZijVnjNt3aiI1/Gfo0ZVzDNJmBbE7PubgoJdNlARtBRlUxOlhKdtBn1uB9EZYmg2kPE5ifux5DlwN 2yRbllCrZWwmEys0JW3GeIZnzj1pBlugezY6sFnO0mc1WHym2EU3m9aVWWEcpL345II43TRZWfqMd 5HbLhKbA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sUoB4-00000002r7A-1Qur; Fri, 19 Jul 2024 14:01:42 +0000 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sUoAh-00000002qyL-1TrC for linux-arm-kernel@lists.infradead.org; Fri, 19 Jul 2024 14:01:21 +0000 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-42725f8a789so36075e9.1 for ; Fri, 19 Jul 2024 07:01:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721397678; x=1722002478; darn=lists.infradead.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Uc/lvxk1g3HuegnXaisJU/OL2Ih+Ws0A6QMr/ME/lAs=; b=ED20VF9KI6EimhYJ1iF8UJ0B6Y4CBdZ/utRDKMwYkEdeSNTJO2GcMeQpRCpWHgS9bQ Qh7qLxTed+Vx9EX98Z4NIxadSRPrSzV8t3b32beSRcxNvKr2A9VsfPFBGvWNDpRuXjvZ v5B/qiFUV0zMwQwbZ9DFksv3DqS5+s6w1Cx6w3kwrbWTRjsoBFGeEoNUz7xahj93WPb8 d3eZTqDXx0fGuYVuNOBaMHPcBT2KVq0TxSnJPDluSeHMTqGhN02aiIHLCJ+uaKU+CY16 ePGx08Xr4vjMWMwnRY6r4ll7ZUtg361UT1DQk11rQidmk1aF1wPP3szYVGcSULVrCy4A 18PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721397678; x=1722002478; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Uc/lvxk1g3HuegnXaisJU/OL2Ih+Ws0A6QMr/ME/lAs=; b=JD/6iyKNhnjnuMx2GnDke+3OsbbMn88lI8HTOrWkOvn/7NGmGa4IC9nqr4vRMVHmBG ptqa1WH/ibjRGiwYBOUFEL8E2zMn9FLeHaTIRVZ25GZSte9zA6KQoaNkVW3/MRQSrlBr 5558YM4yw9Xfw4HOZY5v6JIsDWTyaYQniqqOG0affOm45BkPXXwPIJS1TIuQmTGtGg+T ilnDvuB+kZMrqQ9o2suW0P1B/GavELDlCYO6bz24lCNzUmDe1FUxtALPB7Wn0crDXPHw t9z/q6s+rvAOXfh8bGj/nlD4VtayJTzYLG8RuF9URpZROrG7gLh6tQaLxt3z+svTTfN9 rRuA== X-Forwarded-Encrypted: i=1; AJvYcCXksrJsH8Bydm9RcsUHiEasDqeetWmR4zbbaMAwaqzgdGMrR+ZRZhu1mQXgBo5YZmywZ0Bn9QIUvwP+1wkD789cNkHEMuLSM34MhKZN2qHxn2neios= X-Gm-Message-State: AOJu0Yxe48eYlMFpmr4gqdJegJRoUdY4lGAc9kiV0IV0SfH8IPxKLRCP er2+FcKqQFRmINczdV3K+W1/vktesw65eQN07hmr6fb1q67F5pDo0A72cqkvBg== X-Google-Smtp-Source: AGHT+IGtGTacUit9HaIrHSZTBHJdDYIrRZbK5puBsJayYiuB18xnHZFiAxAnBCnQocCzhGwWjeN0pQ== X-Received: by 2002:a05:600c:5683:b0:424:898b:522b with SMTP id 5b1f17b1804b1-427d69c0713mr1086675e9.1.1721397677189; Fri, 19 Jul 2024 07:01:17 -0700 (PDT) Received: from google.com (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427d6906a96sm26412155e9.22.2024.07.19.07.01.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jul 2024 07:01:16 -0700 (PDT) Date: Fri, 19 Jul 2024 14:01:15 +0000 From: Sebastian Ene To: Oliver Upton 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, maz@kernel.org, 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 v7 5/6] KVM: arm64: Initialize the ptdump parser with stage-2 attributes Message-ID: References: <20240621123230.1085265-1-sebastianene@google.com> <20240621123230.1085265-6-sebastianene@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240719_070119_418259_4FD42A8D X-CRM114-Status: GOOD ( 38.01 ) 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, Jun 28, 2024 at 09:18:16PM +0000, Oliver Upton wrote: > Hi Seb, > > On Fri, Jun 21, 2024 at 12:32:29PM +0000, Sebastian Ene wrote: > > Define a set of attributes used by the ptdump parser to display the > > properties of a guest memory region covered by a pagetable descriptor. > > Build a description of the pagetable levels and initialize the parser > > with this configuration. > > > > Signed-off-by: Sebastian Ene > > This patch should come *before* patch 4, no point in exposing the > debugfs file if we aren't ready to handle it yet. > This is true but this patch doesn't make sense without 4 because here I add a bunch of functions which will not be invoked (they are invoked from the debugfs calls). IMO we can squash them (4 and 5) but it will be a bit harder to follow. Let me know what you think, thanks. Seb > > --- > > arch/arm64/kvm/ptdump.c | 143 ++++++++++++++++++++++++++++++++++++++-- > > 1 file changed, 137 insertions(+), 6 deletions(-) > > > > diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c > > index 36dc7662729f..cc1d4fdddc6e 100644 > > --- a/arch/arm64/kvm/ptdump.c > > +++ b/arch/arm64/kvm/ptdump.c > > @@ -14,6 +14,61 @@ > > #include > > > > > > +#define MARKERS_LEN (2) > > +#define KVM_PGTABLE_MAX_LEVELS (KVM_PGTABLE_LAST_LEVEL + 1) > > + > > +struct kvm_ptdump_guest_state { > > + struct kvm *kvm; > > + struct pg_state parser_state; > > + struct addr_marker ipa_marker[MARKERS_LEN]; > > + struct pg_level level[KVM_PGTABLE_MAX_LEVELS]; > > + struct ptdump_range range[MARKERS_LEN]; > > +}; > > + > > +static const struct prot_bits stage2_pte_bits[] = { > > + { > > + .mask = PTE_VALID, > > + .val = PTE_VALID, > > + .set = " ", > > + .clear = "F", > > + }, { > > + .mask = KVM_PTE_LEAF_ATTR_HI_S2_XN | PTE_VALID, > > + .val = KVM_PTE_LEAF_ATTR_HI_S2_XN | PTE_VALID, > > + .set = "XN", > > + .clear = " ", > > + }, { > > + .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | PTE_VALID, > > + .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | PTE_VALID, > > + .set = "R", > > + .clear = " ", > > + }, { > > + .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | PTE_VALID, > > + .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | PTE_VALID, > > + .set = "W", > > + .clear = " ", > > + }, { > > + .mask = KVM_PTE_LEAF_ATTR_LO_S2_AF | PTE_VALID, > > + .val = KVM_PTE_LEAF_ATTR_LO_S2_AF | PTE_VALID, > > + .set = "AF", > > + .clear = " ", > > > > > + }, { > > + .mask = PTE_NG, > > + .val = PTE_NG, > > + .set = "FnXS", > > + .clear = " ", > > + }, { > > + .mask = PTE_CONT | PTE_VALID, > > + .val = PTE_CONT | PTE_VALID, > > + .set = "CON", > > + .clear = " ", > > + }, { > > > > Neither of these bits are used at stage-2, why have descriptors for > them? > > > +static int kvm_ptdump_build_levels(struct pg_level *level, u32 start_lvl) > > +{ > > + static const char * const level_names[] = {"PGD", "PUD", "PMD", "PTE"}; > > + u32 i = 0; > > + u64 mask = 0; > > + > > + if (start_lvl > 2) { > > + pr_err("invalid start_lvl %u\n", start_lvl); > > + return -EINVAL; > > + } > > if (WARN_ON_ONCE(start_lvl >= KVM_PGTABLE_LAST_LEVEL)) > return -EINVAL; > > > + for (i = 0; i < ARRAY_SIZE(stage2_pte_bits); i++) > > + mask |= stage2_pte_bits[i].mask; > > + > > + for (i = start_lvl; i < KVM_PGTABLE_MAX_LEVELS; i++) { > > + strscpy(level[i].name, level_names[i], sizeof(level[i].name)); > > + > > + level[i].num = ARRAY_SIZE(stage2_pte_bits); > > + level[i].bits = stage2_pte_bits; > > + level[i].mask = mask; > > + } > > + > > + if (start_lvl > 0) > > + strscpy(level[start_lvl].name, level_names[0], sizeof(level_names[0])); > > This should pass the size of @dst, not the source. This becomes slightly > more self-documenting if you use a literal for "PGD" here too. > > strscpy(level[start_lvl].name, "PGD", sizeof(level[start_lvl].name)); > > > + return 0; > > +} > > + > > +static struct kvm_ptdump_guest_state > > +*kvm_ptdump_parser_init(struct kvm *kvm) > > +{ > > + struct kvm_ptdump_guest_state *st; > > + struct kvm_s2_mmu *mmu = &kvm->arch.mmu; > > + struct kvm_pgtable *pgtable = mmu->pgt; > > + int ret; > > + > > + st = kzalloc(sizeof(struct kvm_ptdump_guest_state), GFP_KERNEL_ACCOUNT); > > + if (!st) > > + return NULL; > > + > > + ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level); > > + if (ret) > > + goto free_with_state; > > I don't see any value in the use of goto here, as there isn't any sort > of cascading initialization / cleanup. This also presents an opportunity > to get an error back out to the caller. > > if (ret) { > kfree(st); > return ERR_PTR(ret); > } > > > @@ -57,22 +176,34 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) > > static int kvm_ptdump_guest_open(struct inode *m, struct file *file) > > { > > struct kvm *kvm = m->i_private; > > + struct kvm_ptdump_guest_state *st; > > 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); > > + st = kvm_ptdump_parser_init(kvm); > > + if (!st) { > > + ret = -ENOMEM; > > + goto free_with_kvm_ref; > > + } > > (with the earlier suggestion) > > st = kvm_ptdump_parser_init(kvm); > if (IS_ERR(st)) { > ret = PTR_ERR(st); > goto free_with_kvm_ref; > } > > Otherwise genuine KVM bugs (-EINVAL) are getting lumped into ENOMEM. > > -- > Thanks, > Oliver