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 32A94C6FD1D for ; Thu, 30 Mar 2023 10:16:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Subject:Cc:To:From:Message-ID:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=CUlTRwa6tnt1E1KsH+T/0mNRNtd/bHtJt6D52JAiJKU=; b=lp1gd68bvH+08G t3dKtVs/E26Z20Kit0xeF0oDriQBNu5AIbmgOnfGJeQJIgZGTY+9qO3N/OuJrr2rWNZ6aauQ76yuk 40DCaPnjOihLd0aDQd8+yIkpD+mLXfzbRinKlIAI0F8EI3mwL2URCws+vOykOeD79MEmy+k1cMzxa 4umaLVqPusYwpDcwAqb3gm1eY+lJ6PaS1b1UiMF/hlQmySufMGfnbalGwPAseqsL3kzZeXH5NCSF3 /hENfz8XYA4/laQKULIo6GP6hPErKu308Jz2kBo2TWECliXMC42DdknTVKpU+PBBOJKZW/NzG+1Jx 8mrZ1SFrQOeOsnADv84Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phpJk-003R5i-34; Thu, 30 Mar 2023 10:15:40 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phpJh-003R4Z-2y for linux-arm-kernel@lists.infradead.org; Thu, 30 Mar 2023 10:15:39 +0000 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 dfw.source.kernel.org (Postfix) with ESMTPS id 34E2461FCA; Thu, 30 Mar 2023 10:15:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 97742C433D2; Thu, 30 Mar 2023 10:15:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680171336; bh=NdaGsMedVQMdyb65BLGq+Cwgltdi3hP6jpvLS9oJOcM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=I8KOOyH9ZMvunwfLcgIt5VUnq5G6tyhOPTPizGWmiao7+b4qirYOYw1mkpmxUcXh7 1Pv/hon11M2uMaYExCAgiVuFyjGSe2+qO4lcSyONxGLMJisUfxiFTmhK1FLsZchGbH dSa0e+8UhKv6qh2plTTPXfRzE93CUgn7s3mIOC/4u2f7BWt7VlswwLuWnKByb0baVq oJVdx6A7dkJUr1l6/F+PnTcDKNNQE/zhQ1WiXvLBVC8EAkpi9ByWQLTC7Jtt7gy7gg 37y0n8I+78DxjJIhf+6J20Hnj0FqnQ0TQbEe5QIKhH1/ZhXN3H/3tBdq0bYxNz82Ma 9DNZwa0LhyqgQ== 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 1phpJe-004KMF-6F; Thu, 30 Mar 2023 11:15:34 +0100 Date: Thu, 30 Mar 2023 11:15:33 +0100 Message-ID: <86fs9mwnne.wl-maz@kernel.org> From: Marc Zyngier To: Oliver Upton Cc: kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, James Morse , Suzuki K Poulose , Zenghui Yu , Ricardo Koller , Simon Veith , Reiji Watanabe , Colton Lewis , Joey Gouly , dwmw2@infradead.org Subject: Re: [PATCH v3 07/18] KVM: arm64: timers: Allow userspace to set the global counter offset In-Reply-To: References: <20230324144704.4193635-1-maz@kernel.org> <20230324144704.4193635-8-maz@kernel.org> 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") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: oliver.upton@linux.dev, kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, james.morse@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, ricarkol@google.com, sveith@amazon.de, reijiw@google.com, coltonlewis@google.com, joey.gouly@arm.com, dwmw2@infradead.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230330_031538_033717_BC79052D X-CRM114-Status: GOOD ( 37.99 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Thu, 30 Mar 2023 07:26:48 +0100, Oliver Upton wrote: > > Hi Marc, > > On Fri, Mar 24, 2023 at 02:46:53PM +0000, Marc Zyngier wrote: > > And this is the moment you have all been waiting for: setting the > > counter offset from userspace. > > > > We expose a brand new capability that reports the ability to set > > the offset for both the virtual and physical sides. > > > > In keeping with the architecture, the offset is expressed as > > a delta that is substracted from the physical counter value. > > > > Once this new API is used, there is no going back, and the counters > > cannot be written to to set the offsets implicitly (the writes > > are instead ignored). > > > > Reviewed-by: Colton Lewis > > Signed-off-by: Marc Zyngier > > --- > > arch/arm64/include/asm/kvm_host.h | 4 +++ > > arch/arm64/include/uapi/asm/kvm.h | 9 ++++++ > > arch/arm64/kvm/arch_timer.c | 46 +++++++++++++++++++++++++++---- > > arch/arm64/kvm/arm.c | 8 ++++++ > > include/uapi/linux/kvm.h | 3 ++ > > 5 files changed, 65 insertions(+), 5 deletions(-) > > > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > > index 002a10cbade2..116233a390e9 100644 > > --- a/arch/arm64/include/asm/kvm_host.h > > +++ b/arch/arm64/include/asm/kvm_host.h > > @@ -221,6 +221,8 @@ struct kvm_arch { > > #define KVM_ARCH_FLAG_EL1_32BIT 4 > > /* PSCI SYSTEM_SUSPEND enabled for the guest */ > > #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED 5 > > + /* VM counter offset */ > > +#define KVM_ARCH_FLAG_VM_COUNTER_OFFSET 6 > > > > unsigned long flags; > > > > @@ -1010,6 +1012,8 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu, > > > > long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, > > struct kvm_arm_copy_mte_tags *copy_tags); > > +int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm, > > + struct kvm_arm_counter_offset *offset); > > > > /* Guest/host FPSIMD coordination helpers */ > > int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu); > > diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h > > index f8129c624b07..12fb0d8a760a 100644 > > --- a/arch/arm64/include/uapi/asm/kvm.h > > +++ b/arch/arm64/include/uapi/asm/kvm.h > > @@ -198,6 +198,15 @@ struct kvm_arm_copy_mte_tags { > > __u64 reserved[2]; > > }; > > > > +/* > > + * Counter/Timer offset structure. Describe the virtual/physical offset. > > + * To be used with KVM_ARM_SET_COUNTER_OFFSET. > > + */ > > +struct kvm_arm_counter_offset { > > + __u64 counter_offset; > > + __u64 reserved; > > +}; > > + > > #define KVM_ARM_TAGS_TO_GUEST 0 > > #define KVM_ARM_TAGS_FROM_GUEST 1 > > > > diff --git a/arch/arm64/kvm/arch_timer.c b/arch/arm64/kvm/arch_timer.c > > index bb64a71ae193..25625e1d6d89 100644 > > --- a/arch/arm64/kvm/arch_timer.c > > +++ b/arch/arm64/kvm/arch_timer.c > > @@ -851,9 +851,11 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) > > ptimer->vcpu = vcpu; > > ptimer->offset.vm_offset = &vcpu->kvm->arch.timer_data.poffset; > > > > - /* Synchronize cntvoff across all vtimers of a VM. */ > > - timer_set_offset(vtimer, kvm_phys_timer_read()); > > - timer_set_offset(ptimer, 0); > > + /* Synchronize offsets across timers of a VM if not already provided */ > > + if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) { > > + timer_set_offset(vtimer, kvm_phys_timer_read()); > > + timer_set_offset(ptimer, 0); > > + } > > > > hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD); > > timer->bg_timer.function = kvm_bg_timer_expire; > > @@ -897,8 +899,11 @@ int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) > > kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value); > > break; > > case KVM_REG_ARM_TIMER_CNT: > > - timer = vcpu_vtimer(vcpu); > > - timer_set_offset(timer, kvm_phys_timer_read() - value); > > + if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, > > + &vcpu->kvm->arch.flags)) { > > + timer = vcpu_vtimer(vcpu); > > + timer_set_offset(timer, kvm_phys_timer_read() - value); > > + } > > break; > > case KVM_REG_ARM_TIMER_CVAL: > > timer = vcpu_vtimer(vcpu); > > @@ -908,6 +913,13 @@ int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) > > timer = vcpu_ptimer(vcpu); > > kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value); > > break; > > + case KVM_REG_ARM_PTIMER_CNT: > > + if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, > > + &vcpu->kvm->arch.flags)) { > > + timer = vcpu_ptimer(vcpu); > > + timer_set_offset(timer, kvm_phys_timer_read() - value); > > + } > > + break; > > case KVM_REG_ARM_PTIMER_CVAL: > > timer = vcpu_ptimer(vcpu); > > kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value); > > @@ -1443,3 +1455,27 @@ int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) > > > > return -ENXIO; > > } > > + > > +int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm, > > + struct kvm_arm_counter_offset *offset) > > +{ > > + if (offset->reserved) > > + return -EINVAL; > > + > > + if (!lock_all_vcpus(kvm)) > > + return -EBUSY; > > Similar to what you had mentioned over on the lock (un)inversion series, > doesn't this risk racing with vCPU creation w/o holding the kvm->lock? Huh, I was under the impression that kvm_vm_ioctl() would take the kvm->lock, but it really doesn't! I'll fix that right away. > Alternatively you could require this ioctl to be issued before any vCPUs > are created to avoid the need to lock them all here. But, if you see > value in poking this at runtime then fine by me. I think there is some value in allowing the offset adjustment to be set at the last possible point, right before a vcpu is ran. Thanks for the heads up! M. -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel