Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp657812rdb; Tue, 19 Sep 2023 06:44:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEAyjnrb0MKZMPzIDD+spVGbrmOgUVKIgBkBcVx2E1anyGPxYwDQwF7rLqIWeXRSZavM1w4 X-Received: by 2002:a17:90b:10f:b0:25e:a8ab:9157 with SMTP id p15-20020a17090b010f00b0025ea8ab9157mr10691861pjz.22.1695131040446; Tue, 19 Sep 2023 06:44:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695131040; cv=none; d=google.com; s=arc-20160816; b=RjYYThWgmXOO8IImuYzXfB456c48AjmMxKaBgtm2rAGNg97ieiGCX5LBTBxyevZ28U WasO6fhcRZEsOkxx1azsRdIgBK0t8sMCCF//A4cZcAWEsllWx86Rb0XRw358pWvfBWSs PxClL45VOZG5d/p92fyovAsn4EbqvpVGJP3iLq7aRi9o3RpuKq7GSjQenZhPZ3n9cM3O 0Gcu/mN7AKCsFAExKiAmr3rw+0i7M0+e/HM6VOB27kA8d0U4bfwv3k37CmAx9X+EeNBW LNqvk+BGmWA/RjvxlUgV3HAALeoELS5uTtGAtJS/9d2oZnLHfyffp80R4LDby+ldNUOL bfDw== 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=rYA2c6obTkYF/X7G/B4Xb9ciRYUkDwbnPL4CtJ0MRDU=; fh=dMrWgnXzzePbjJ5Lnz3JQ1FNdAF92uv7SkGdCptz9bs=; b=xvp2hPIZAqajsUTkMvHpzWyryNYbCNp2bUaE6zXrAtMYwYuRgc2C6NGBRN/3acHkJ9 8GFiOVISWKajnS+F/vzf+B/cqnufBbF5zufVPIKP2Rn0DNFh5bFB+/wtlHcCcfv0z4Si xcLaN7W241EBuPkXthMtWgSkVpUP4FtQ95rZlHwixkSvd7kkrEJhmcRIxuz+mrD0ROp4 eSY9Bcr/AX8ssg+uNBJbwZFSmOBfbpGbhiLvF6CVl5a/37o41DfnLolB96uQxHmz30aQ 6sfrf3daTn31fC8ejQ38XdVaLX47acQDJcBUaUgEaULoH8W/d/dfybp9N+yK47yhf4a9 AIxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YP10dzWB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id mh7-20020a17090b4ac700b0027198fe5672si10291469pjb.129.2023.09.19.06.43.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 06:44:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YP10dzWB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 1C8E480E4FF8; Mon, 18 Sep 2023 10:07:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230006AbjIRRHi (ORCPT + 99 others); Mon, 18 Sep 2023 13:07:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229940AbjIRRHg (ORCPT ); Mon, 18 Sep 2023 13:07:36 -0400 Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D39788E for ; Mon, 18 Sep 2023 10:07:30 -0700 (PDT) Received: by mail-il1-x134.google.com with SMTP id e9e14a558f8ab-34fcc39fae1so4335ab.0 for ; Mon, 18 Sep 2023 10:07:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695056850; x=1695661650; 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=rYA2c6obTkYF/X7G/B4Xb9ciRYUkDwbnPL4CtJ0MRDU=; b=YP10dzWBTrTiDMJKQS4XjPIR57MmjeQcxIYrHCDkz3hCRe0nh780JufaFBJQ8r6E0k psEiV+dMvP6PkvoPZHEx3u5DS8QUIvnyY/AZuxl0qiZHtUs16NF3nnJdBdpMMpu586Hf mmG11u5Rqz04kwGgsOQFLxihRuygJWfQGhzlJyr7zSEAxnhG4PuHstYA41EhvKcEWrGC 0DQnmnzX35+CyoFrPze61wZnce08NbQbt9qazBjMhCiXsnFp0rhE2vf9agbzJ66qljH4 4nfOFmOMcQO+un9ZaVwf9cix0EBcNgywmtX63ZCdMPQ11V2uzx2iMT5sudnqV/u0SJ2n /0bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695056850; x=1695661650; 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=rYA2c6obTkYF/X7G/B4Xb9ciRYUkDwbnPL4CtJ0MRDU=; b=dEg7SHcVfUL7oAQKEXGgPqCkqFp9XL2Alj7SeRSAYGJsRvbiXE8K7YBCx+DSjoWiDf ZuwEw6H0iVudALziNs1hukM2leEfyLJeRn4rbMmNipogc7RBAld+x4RwqMHYPgfLAFsy 5qrVrDXorD+nCnihQR2zGob6KPLQc38lbPR6uNl3hwXoX3pdz6Dy2bsT1OuyaeErCffO BAas/wKAhG6TBihNtxFvy2pywQ42N6zZxfOgMtRWdPTyaRs4Pw8ZPqHULU3pdEOgku2U MNGHjHM7gkdEkzkQvyqkfK2SASXnK14KJ1MW9mnlehXpNlYM3SVbkqwJqbvK2YYdBCSF LHZw== X-Gm-Message-State: AOJu0YxJ0XIQjDs2RDkwFlha/OZfYNVNdL2NHBR23XPAGZ2b/dtUt3zn LuJwRpZXqmRi44Z/8Ujr4QGaMuxBtD7Tmu3BMxjCXQ== X-Received: by 2002:a05:6e02:170f:b0:34d:f90f:d42a with SMTP id u15-20020a056e02170f00b0034df90fd42amr602937ill.1.1695056850063; Mon, 18 Sep 2023 10:07:30 -0700 (PDT) MIME-Version: 1.0 References: <20230817003029.3073210-1-rananta@google.com> <20230817003029.3073210-9-rananta@google.com> In-Reply-To: From: Raghavendra Rao Ananta Date: Mon, 18 Sep 2023 10:07:18 -0700 Message-ID: Subject: Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest To: Oliver Upton Cc: Marc Zyngier , 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 morse.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 (morse.vger.email [0.0.0.0]); Mon, 18 Sep 2023 10:07:41 -0700 (PDT) On Fri, Sep 15, 2023 at 1:53=E2=80=AFPM Oliver Upton wrote: > > Hi Raghu, > > On Thu, Aug 17, 2023 at 12:30:25AM +0000, 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 upserspace). > > typo: userspace > Noted. > > diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c > > index ce7de6bbdc967..39ad56a71ad20 100644 > > --- a/arch/arm64/kvm/pmu-emul.c > > +++ b/arch/arm64/kvm/pmu-emul.c > > @@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_= pmu *arm_pmu) > > * while the latter does not. > > */ > > kvm->arch.pmcr_n =3D arm_pmu->num_events - 1; > > + kvm->arch.pmcr_n_limit =3D arm_pmu->num_events - 1; > > Can't we just get at this through the arm_pmu instance rather than > copying it into kvm_arch? > Yeah, I suppose we can directly access it in set_pmcr(). Thank you. Raghavendra > > return 0; > > } > > diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c > > index 2075901356c5b..c01d62afa7db4 100644 > > --- a/arch/arm64/kvm/sys_regs.c > > +++ b/arch/arm64/kvm/sys_regs.c > > @@ -1086,6 +1086,51 @@ 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; > > + int ret =3D 0; > > + > > + new_n =3D FIELD_GET(ARMV8_PMU_PMCR_N, val); > > + > > + mutex_lock(&kvm->arch.config_lock); > > + if (unlikely(new_n !=3D kvm->arch.pmcr_n)) { > > + /* > > + * The vCPU can't have more counters than the PMU > > + * hardware implements. > > + */ > > + if (new_n <=3D kvm->arch.pmcr_n_limit) > > + kvm->arch.pmcr_n =3D new_n; > > + else > > + ret =3D -EINVAL; > > + } > > Hmm, I'm not so sure about returning an error here. ABI has it that > userspace can write any value to PMCR_EL0 successfully. Can we just > ignore writes that attempt to set PMCR_EL0.N to something higher than > supported by hardware? Our general stance should be that system register > fields responsible for feature identification are immutable after the VM > has started. > > -- > Thanks, > Oliver