Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1385444rda; Mon, 23 Oct 2023 10:54:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEAHLgc24/A7DY3AjHSXJCjVOZX4IAfHCGRBjEYo4Raho4qO7cEkLpNVkPkCpjHd1aDktN6 X-Received: by 2002:a17:90a:6e05:b0:27c:edf3:d045 with SMTP id b5-20020a17090a6e0500b0027cedf3d045mr9869355pjk.40.1698083653293; Mon, 23 Oct 2023 10:54:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698083653; cv=none; d=google.com; s=arc-20160816; b=VXOh71nttFOc7kljXzy2bx6AkZgJFgBa0rJ3EDtOUgrrPuu3K+O0Foed0RncDYFb0/ kgTXu5/gtTibh+7PG8q/5UScPWqjaNI3tYu+JP4mrJyZPE+Gssq+ZDt/fsAFgo1TERbm 2zSMEgtnja/9bnMRqz12DCeubV77ZEeIFAoK1PFHMiWFhMGLK3zzqGPl9ZGf2lztcwBD HF3XCP3MUEUOMVPQSIcRORnLrdBvTjVtb51mqcPbTxqc85Avch3h1VkluLxV31o9BipU 8jnJD0kNZDe46rJ6JQgxgzmBYZWJHvAhfgtB2jGbsZeUt0zHWuVENaAFWewr7LqcYX43 vlVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=Qw5+2AMntm7pXpxFSU/tC2f7FyRYAShkL6ADVOeD7Gg=; fh=TGk4urwePQAtNEz2g8zYdmZqxEEWumkMKaQFXo1TwoY=; b=wR2W3FBe3R+vMU+YCDsmiENynHwN0vaNXexzdC4fZr/Bft+OsbJEeFyYensGwJdR8Z RZxuxkqopRxOTcrab3/bUjBNRn+LJdibb8IQQs2fd3/vuvgooUGiLJrI4QH9XX8rFKq0 DkINwu61uYORQsD4KaQhN42phEEY5CH3i9rz3OtubRSSWzkMdvicXHB4PrqsLhjyfMw6 HvqyNw4iEAqmJX8/gz63ag+Up1Zsr+vp+ANKYV0zdZ/j35eVRzobdsJzu0S6PZKsccmE mvy5q7SU4N6kTAPfgFMl3idkzEe6FLnb/EvyZHjJA+b7nFSz/n5M5wGIoOXg/8ul+5Rk Etsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NrrvvsQg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id v14-20020a17090abb8e00b0027cdee52324si9016676pjr.74.2023.10.23.10.54.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 10:54:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NrrvvsQg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id 71669808E8CF; Mon, 23 Oct 2023 10:54:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231231AbjJWRxo (ORCPT + 99 others); Mon, 23 Oct 2023 13:53:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230481AbjJWRxl (ORCPT ); Mon, 23 Oct 2023 13:53:41 -0400 Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE36EB0 for ; Mon, 23 Oct 2023 10:53:38 -0700 (PDT) Received: by mail-il1-x132.google.com with SMTP id e9e14a558f8ab-3575826ba20so7505ab.0 for ; Mon, 23 Oct 2023 10:53:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698083618; x=1698688418; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Qw5+2AMntm7pXpxFSU/tC2f7FyRYAShkL6ADVOeD7Gg=; b=NrrvvsQguV6UPHGzI2le4Kowy1j1+MZguedVVTImwL+36wgJyYU8mHtot92nUFvx+i Ubc2y+fFvplvNPvBuKxbG0ySdU5YhBUploUCg3BdgzMjZECl7Q8PqHA3dr66roHUkSaw 4fFUF3A0Yi2bRGcoIGcJPpzrhhA5Q4AEyK2A5NqU9blhjomCGpPOXuCwNSHleCKKPIU9 wBEUv5Zk266gGyDm5HvZSwZ2eag0RFa3es2azH1urXD4n4QozOOFDB3XMS6yvB7UJkhp y4JQfRZvS1raV82D73SyxSH2BfpBVhGNiBdeYuUPLnunmTQ5GkRdrk4jvKClIc4rYVXg P7bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698083618; x=1698688418; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Qw5+2AMntm7pXpxFSU/tC2f7FyRYAShkL6ADVOeD7Gg=; b=FO3gzTQg/ubCcr3hr51bhPlVQhOmEu+VEzBde6fBruVZ6IVIjjQ5idWTaKC68K5Hy7 Y2cmQ1tpdyXGzcw7BMWjRUUpdb8PYZfBm+Hg6HPE7AcKk/sW9UCr3koMICOY6qWknd3t l+dSWdkpbgmhEG/S3HmwVA5NJzlInxuYYLtEyDgki9TTBiF8JfbNz1nfjGhW/Ujk0AQU EAUfI57c1LF7HKY/mlNXGihVyzQEUiJW373fuXfqseci4qDtMgyBRJVKgjCbrMDoYmIe whSYHD+U8oQ2Gzska1BTPH2AmjxKggEIHH8bn/IzdGqW0ZriAYrWqq/fKQuerWsH2GX1 BkqA== X-Gm-Message-State: AOJu0YyR7JlIMZBwPlVsDw8oEWeKnNzO7AMmiG+in5Ajh55gPUjRgSnB p6FUpf1/AQ9C/yYHK5FUxYErcpajZEAkdY37+mT2+Q== X-Received: by 2002:a92:8e42:0:b0:357:cdca:d0b1 with SMTP id k2-20020a928e42000000b00357cdcad0b1mr28845ilh.8.1698083618114; Mon, 23 Oct 2023 10:53:38 -0700 (PDT) MIME-Version: 1.0 References: <20231020214053.2144305-1-rananta@google.com> <20231020214053.2144305-8-rananta@google.com> <86wmvd4hp9.wl-maz@kernel.org> In-Reply-To: <86wmvd4hp9.wl-maz@kernel.org> From: Raghavendra Rao Ananta Date: Mon, 23 Oct 2023 10:53:26 -0700 Message-ID: Subject: Re: [PATCH v8 07/13] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest To: Marc Zyngier Cc: Oliver Upton , Alexandru Elisei , James Morse , Suzuki K Poulose , Paolo Bonzini , Zenghui Yu , Shaoqin Huang , Jing Zhang , Reiji Watanabe , Colton Lewis , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email 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 (groat.vger.email [0.0.0.0]); Mon, 23 Oct 2023 10:54:10 -0700 (PDT) On Mon, Oct 23, 2023 at 6:00=E2=80=AFAM Marc Zyngier wrote= : > > On Fri, 20 Oct 2023 22:40:47 +0100, > Raghavendra Rao Ananta wrote: > > > > From: Reiji Watanabe > > > > KVM does not yet support userspace modifying PMCR_EL0.N (With > > the previous patch, KVM ignores what is written by userspace). > > Add support userspace limiting PMCR_EL0.N. > > > > Disallow userspace to set PMCR_EL0.N to a value that is greater > > than the host value as KVM doesn't support more event counters > > than what the host HW implements. Also, make this register > > immutable after the VM has started running. To maintain the > > existing expectations, instead of returning an error, KVM > > returns a success for these two cases. > > > > Finally, ignore writes to read-only bits that are cleared on > > vCPU reset, and RES{0,1} bits (including writable bits that > > KVM doesn't support yet), as those bits shouldn't be modified > > (at least with the current KVM). > > > > Signed-off-by: Reiji Watanabe > > Signed-off-by: Raghavendra Rao Ananta > > --- > > arch/arm64/kvm/sys_regs.c | 57 +++++++++++++++++++++++++++++++++++++-- > > 1 file changed, 55 insertions(+), 2 deletions(-) > > > > diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c > > index 2e5d497596ef8..a2c5f210b3d6b 100644 > > --- a/arch/arm64/kvm/sys_regs.c > > +++ b/arch/arm64/kvm/sys_regs.c > > @@ -1176,6 +1176,59 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const= struct sys_reg_desc *r, > > return 0; > > } > > > > +static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *= r, > > + u64 val) > > +{ > > + struct kvm *kvm =3D vcpu->kvm; > > + u64 new_n, mutable_mask; > > Really, this lacks consistency. Either you make N a u8 everywhere, or > a u64 everywhere. I don't mind either, but the type confusion is not > great. > Sorry about that. I'll make it u8 across the board. > > + > > + mutex_lock(&kvm->arch.config_lock); > > + > > + /* > > + * Make PMCR immutable once the VM has started running, but > > + * do not return an error to meet the existing expectations. > > + */ > > + if (kvm_vm_has_ran_once(vcpu->kvm)) { > > + mutex_unlock(&kvm->arch.config_lock); > > + return 0; > > + } > > + > > + new_n =3D (val >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK= ; > > + if (new_n !=3D kvm->arch.pmcr_n) { > > Why do we need to check this? > Hmm, it may be redundant. I guess we can skip this, check for the limit, and directly write new_n to kvm->arch.pmcr_n. > > + u8 pmcr_n_limit =3D kvm_arm_pmu_get_max_counters(kvm); > > Can you see why I'm annoyed? > Yes. I'll make these consistent. > > + > > + /* > > + * The vCPU can't have more counters than the PMU hardwar= e > > + * implements. Ignore this error to maintain compatibilit= y > > + * with the existing KVM behavior. > > + */ > > + if (new_n <=3D pmcr_n_limit) > > Isn't this the only thing that actually matters? > Yes, I'll remove the above check. > > + kvm->arch.pmcr_n =3D new_n; > > + } > > + mutex_unlock(&kvm->arch.config_lock); > > + > > + /* > > + * Ignore writes to RES0 bits, read only bits that are cleared on > > + * vCPU reset, and writable bits that KVM doesn't support yet. > > + * (i.e. only PMCR.N and bits [7:0] are mutable from userspace) > > + * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the v= CPU. > > + * But, we leave the bit as it is here, as the vCPU's PMUver migh= t > > + * be changed later (NOTE: the bit will be cleared on first vCPU = run > > + * if necessary). > > + */ > > + mutable_mask =3D (ARMV8_PMU_PMCR_MASK | > > + (ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT)= ); > > Why is N part of the 'mutable' mask? The only bits that should make it > into the register are ARMV8_PMU_PMCR_MASK. > > > + val &=3D mutable_mask; > > + val |=3D (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask); > > + > > + /* The LC bit is RES1 when AArch32 is not supported */ > > + if (!kvm_supports_32bit_el0()) > > + val |=3D ARMV8_PMU_PMCR_LC; > > + > > + __vcpu_sys_reg(vcpu, r->reg) =3D val; > > + return 0; > > I think this should be rewritten as: > > val &=3D ARMV8_PMU_PMCR_MASK; > /* The LC bit is RES1 when AArch32 is not supported */ > if (!kvm_supports_32bit_el0()) > val |=3D ARMV8_PMU_PMCR_LC; > > __vcpu_sys_reg(vcpu, r->reg) =3D val; > return 0; > > And that's it. Drop this 'mutable_mask' nonsense, as we should be > getting the correct value (merge of the per-vcpu register and VM-wide > N) since patch 4. > Sure, I'll consider this. Thank you. Raghavendra