Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp5219982rdb; Sat, 16 Sep 2023 07:38:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGUprVHm5nE983psmH7qFCEAZPqL6/fEHrfwYB3l86oHltMXNQ5vS8k19GCPyXJuTJsfce0 X-Received: by 2002:a17:903:246:b0:1bf:6ad7:2286 with SMTP id j6-20020a170903024600b001bf6ad72286mr4477520plh.43.1694875117291; Sat, 16 Sep 2023 07:38:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694875117; cv=none; d=google.com; s=arc-20160816; b=O0JLQuEJt/FLZaBZHuoQvYXWZK/9Z9B4U4pf3kx7neSxTDkB4N1ytLPWYMugcekA6E dZzW0yFZDP4iymfahIlkiMwO+/c/g9cS6dJCTuj+Gxv/wiGkVAkh7wCYhKywTY7PtCvK r4Gz5jLpUuY6nNay1TaCNQO/37QE0NHkmmiCOJZyl9FtE9f/Q8VXhEJ0dF9CV6Wht//7 6kxt/wzUMKX9V51FKy/ImnOxwSq9NqXSTDxPC9Px5kY/uMVWJBZjme0C3Oi9v6AplLmf wWU/lo88NGHpMYC0GSGQZvhLVCH1M8X+R/UnIfO1aGA7Xg+/T/RUOmvZqS5ynY3jZl/p cT1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=IRK5mFLmf/ekVrKw/W6wumPqbLTXGlC7g336g390ZFg=; fh=4I6BNv7iobpZrRcbueKA7r9RD02xPQmynyU/PeDFguM=; b=BMhGUcRs11HrJGkLF9vIj2uR0iUQdVxwP7zducyxN9jK+vP+sPWkuvUyq7X7KbiZyF NcKT+9sKkyTrbSFE+gX2WJ5SjTPIAxLweICpIerys1zuNiegvcKSdkoZ3rrzVJ+lsSOU bp35sEb+lA7tpR/mxpXB4CGfpq6epkww+kabW16LhDZzvG3hIqmXkJzka+3TFBM5JXF/ KtXda7eNuQbxju78WElt3V/G+7RILYt7tfMSs1+aa0YkqR3nVrPvCLKy1UICNE8iWn+i 8C/GwU1lnTEue5zQlneKxoUSUGCFu5sWJLbb5+oEceqp3uRJUrSpq5Cu0wmXWDL2zK7t d0qg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=HM4B49eR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id b13-20020a170902d50d00b001b9c3498510si5276831plg.469.2023.09.16.07.38.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Sep 2023 07:38:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=HM4B49eR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 2F02B836D620; Fri, 15 Sep 2023 14:54:56 -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 S236460AbjIOVyX (ORCPT + 99 others); Fri, 15 Sep 2023 17:54:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237025AbjIOVyT (ORCPT ); Fri, 15 Sep 2023 17:54:19 -0400 Received: from out-218.mta0.migadu.com (out-218.mta0.migadu.com [91.218.175.218]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DA29118 for ; Fri, 15 Sep 2023 14:54:12 -0700 (PDT) Date: Fri, 15 Sep 2023 21:54:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1694814850; 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=IRK5mFLmf/ekVrKw/W6wumPqbLTXGlC7g336g390ZFg=; b=HM4B49eRkbN+AztPqOtcIBPQC13taubkjpx98EkeUP/8Y+6/ptecJDrBvfnA/jQphRy9pj 2LFFJMqKbLJ6s3StvU6XlyXPsr0+lYYUqilCc/R79wnSGIeoyFR+griit9Y7i+qyOq48ee oT5hUVuAEb96mZXronFcO5nwBXzvVHI= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Raghavendra Rao Ananta 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 Subject: Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest Message-ID: References: <20230817003029.3073210-1-rananta@google.com> <20230817003029.3073210-9-rananta@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS 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]); Fri, 15 Sep 2023 14:54:56 -0700 (PDT) On Fri, Sep 15, 2023 at 08:53:16PM +0000, 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 > > > 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 = arm_pmu->num_events - 1; > > + kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1; > > Can't we just get at this through the arm_pmu instance rather than > copying it into kvm_arch? > > > 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 = vcpu->kvm; > > + u64 new_n, mutable_mask; > > + int ret = 0; > > + > > + new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val); > > + > > + mutex_lock(&kvm->arch.config_lock); > > + if (unlikely(new_n != kvm->arch.pmcr_n)) { > > + /* > > + * The vCPU can't have more counters than the PMU > > + * hardware implements. > > + */ > > + if (new_n <= kvm->arch.pmcr_n_limit) > > + kvm->arch.pmcr_n = new_n; > > + else > > + ret = -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. I hacked up my reply and dropped some context; this doesn't read right. Shaoqin made the point about preventing changes to PMCR_EL0.N after the VM has started and I firmly agree. The behavior should be: - Writes to PMCR always succeed - PMCR_EL0.N values greater than what's supported by hardware are ignored - Changes to N after the VM has started are ignored. -- Thanks, Oliver