Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp3999064rwb; Sat, 21 Jan 2023 04:13:54 -0800 (PST) X-Google-Smtp-Source: AMrXdXslcZFzssE7ad7AgkPZDguOMA08HIoKqi+yL1U1CZM1gwk8ECMttWctK9SRg4FDb6LvMnMh X-Received: by 2002:a05:6402:5110:b0:49d:fff2:d4d7 with SMTP id m16-20020a056402511000b0049dfff2d4d7mr25302314edd.30.1674303234286; Sat, 21 Jan 2023 04:13:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674303234; cv=none; d=google.com; s=arc-20160816; b=SnsVsajatArc8W/lUaqjziEii8XMn3YTWjsjpwLhdugA/4WM3SndcQhDv+oEQzqFpy VAutssuPZSGaqozm3OgHjQhcJm/3hTtGEkpT2VqFYFoFzXRlauqj5FVOWqYsOKG3DTin 4u5+t/XuqWismLiY5ihxqxvUAo7bg7TEVklVkVDpJG0AwRv9ADNMj5wTgkH/v0yIHWvs 46ymhQOvUPeNY+3aeQH6DT3op9uWdW9w5HmoaMRyjYO2DDFo/LiPuFUmLAXfnUEFm6V8 bZdCMljgWLyuAB23m2sdBK1GvWKolVbssgkemTeOgOSdB+fk9903zTCJN1NFsrtTZybJ SYEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=6M43GWBh3zhykt4hpFvjv6fHhRmwkiz1TbvMKi8hH6g=; b=Ws6BoxDXjk71BZG3I9deJol9rXQBzeRLHSM8aKJ8NeSCIp4gm8dGwP/fn2XHb8u6h9 6KmfFQG17YZtR0llhDGYl2R9ZmSN7BCRKZn3Qo2Uzp3oWC92XGSqxPnd9uyJdfVmKYF9 rQ/5dS+qfff5E4JaUDT/POtI1bA5zY7dslaDkm0kcBOQD75VbJvwmdc3skYJf1Z7lFR0 EGBQOe+893IcWaW+PMv0Bx0ICxcDq6hW87NtgxwyBzzmbP7TuqqOEazhBZPXRkAfsvdH h08XwsmY+GtsQoxFFWWlABaBKvJfReUi3kjKCJc6li4Hkwif2kAFT1txVQ0aacrf8suy mwow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XqWtOPUp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id go34-20020a1709070da200b00871969cddafsi19102101ejc.530.2023.01.21.04.13.39; Sat, 21 Jan 2023 04:13:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XqWtOPUp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229645AbjAUMCM (ORCPT + 52 others); Sat, 21 Jan 2023 07:02:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229450AbjAUMCL (ORCPT ); Sat, 21 Jan 2023 07:02:11 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80DAF13DE7 for ; Sat, 21 Jan 2023 04:02:09 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 0C941B80760 for ; Sat, 21 Jan 2023 12:02:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A40FEC4339B; Sat, 21 Jan 2023 12:02:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1674302526; bh=oYtg75ka9VvkbzbkuCBtvmOJl2dncX28jjo0X1mZ/H8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=XqWtOPUpTRni7OvcYGsxDhJJK09DEUcvQtbAYQe9PNTKtS6elkHPTS06OTTGds6XP WFy1V2d2fc9MVlQYa817/88emTdu1VLLRt8QxmNQ4SPKdtXj5xnklZRazsPSlZKOVs jOWav8X4qFHdu7MBpDfk4sneNsJzeAxruPUj3PxT9EPE+wmK7Fmb7T8hriNK1mxrdc SIoA3FEghqCuzDuavNtT4MTYWpH7EVUdzw/WVnQGZ1oFVbe7pFED52Q9pGyiI30Op/ Z6MRoaXpWdXl+isoDtE2M22ujd4frYehlVZcJQ0Y/JEIN0wypg7AzAgrAitDxo8Fx3 tHUQDnvDr/CvQ== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pJCZQ-003d6M-2q; Sat, 21 Jan 2023 12:02:04 +0000 Date: Sat, 21 Jan 2023 12:02:03 +0000 Message-ID: <86k01gm6ys.wl-maz@kernel.org> From: Marc Zyngier To: Oliver Upton , Akihiko Odaki Cc: Mark Brown , linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Mathieu Poirier , Suzuki K Poulose , Alexandru Elisei , James Morse , Will Deacon , Catalin Marinas , asahi@lists.linux.dev, Alyssa Rosenzweig , Sven Peter , Hector Martin Subject: Re: [PATCH v7 7/7] KVM: arm64: Normalize cache configuration In-Reply-To: References: <20230112023852.42012-1-akihiko.odaki@daynix.com> <20230112023852.42012-8-akihiko.odaki@daynix.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/28.2 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: oliver.upton@linux.dev, akihiko.odaki@daynix.com, broonie@kernel.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, mathieu.poirier@linaro.org, suzuki.poulose@arm.com, alexandru.elisei@arm.com, james.morse@arm.com, will@kernel.org, catalin.marinas@arm.com, asahi@lists.linux.dev, alyssa@rosenzweig.io, sven@svenpeter.dev, marcan@marcan.st X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 19 Jan 2023 19:46:16 +0000, Oliver Upton wrote: > > Hi Akihiko, > > On Thu, Jan 12, 2023 at 11:38:52AM +0900, Akihiko Odaki wrote: > > Before this change, the cache configuration of the physical CPU was > > exposed to vcpus. This is problematic because the cache configuration a > > vcpu sees varies when it migrates between vcpus with different cache > > configurations. > > > > Fabricate cache configuration from the sanitized value, which holds the > > CTR_EL0 value the userspace sees regardless of which physical CPU it > > resides on. > > > > CLIDR_EL1 and CCSIDR_EL1 are now writable from the userspace so that > > the VMM can restore the values saved with the old kernel. > > > > Suggested-by: Marc Zyngier > > Signed-off-by: Akihiko Odaki > > I needed to squash in the patch below to get all of this working. > Writing back the value read for a given cache level was failing, which I > caught with the get-reg-list selftest. > > Pushed the result here if you want to have a look: > > https://github.com/oupton/linux/tree/kvm-arm64/virtual-cache-geometry > > -- > Thanks, > Oliver > > diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c > index 459e6d358dab..b6228f7d1d8d 100644 > --- a/arch/arm64/kvm/sys_regs.c > +++ b/arch/arm64/kvm/sys_regs.c > @@ -148,17 +148,19 @@ static u32 get_ccsidr(struct kvm_vcpu *vcpu, u32 csselr) > > static int set_ccsidr(struct kvm_vcpu *vcpu, u32 csselr, u32 val) > { > - u8 line_size = FIELD_GET(CCSIDR_EL1_LineSize, val); > + u8 line_size = SYS_FIELD_GET(CCSIDR_EL1, LineSize, val); > + u32 cur = get_ccsidr(vcpu, csselr); > + u8 min_line_size = SYS_FIELD_GET(CCSIDR_EL1, LineSize, cur); > u32 *ccsidr = vcpu->arch.ccsidr; > u32 i; > > - if ((val & CCSIDR_EL1_RES0) || line_size < get_min_cache_line_size(csselr)) > + if (cur == val) > + return 0; > + > + if ((val & CCSIDR_EL1_RES0) || line_size < min_line_size) > return -EINVAL; This doesn't look right. You're comparing the value userspace is trying to set for a given level with the value that is already set for that level, and forbid the cache line size to be smaller. It works if no value has been set yet (you fallback to something derived from CTR_EL0), but this fails if userspace does multiple writes. The original check is against CTR_EL0, which makes absolute sense because we want to check across the whole hierarchy. It is just that the original code has two bugs: - It fails to convert the CCSIDR_EL1.LineSize value to a number of words (the missing +4). Admire how the architecture is actively designed to be hostile to SW by providing two different formats for the cache line size, none of which is in... bytes. - It passes the full CSSELR value to get_min_cache_line_size(), while this function wants a bool... Yes, there are times where you'd want a stronger type system (did anyone say Rust? ;-) I propose that we fold something like the patch below in instead (tested with get-reg-list). Thanks, M. diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 3b3024c42e61..ac943dcb4610 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -148,11 +148,12 @@ static u32 get_ccsidr(struct kvm_vcpu *vcpu, u32 csselr) static int set_ccsidr(struct kvm_vcpu *vcpu, u32 csselr, u32 val) { - u8 line_size = FIELD_GET(CCSIDR_EL1_LineSize, val); + u8 line_size = FIELD_GET(CCSIDR_EL1_LineSize, val) + 4; u32 *ccsidr = vcpu->arch.ccsidr; u32 i; - if ((val & CCSIDR_EL1_RES0) || line_size < get_min_cache_line_size(csselr)) + if ((val & CCSIDR_EL1_RES0) || + line_size < get_min_cache_line_size(csselr & CSSELR_EL1_InD)) return -EINVAL; if (!ccsidr) { -- Without deviation from the norm, progress is not possible.