Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2202754rwi; Fri, 21 Oct 2022 00:37:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6+EWZ/ab0X2ehUW4qLQntZsnScKQ7elbB5TKo5/rdGJWAas3raoYQ0cGS4eNMuhNr1+0X9 X-Received: by 2002:a17:907:a062:b0:78d:d4c3:caf9 with SMTP id ia2-20020a170907a06200b0078dd4c3caf9mr14794931ejc.704.1666337820455; Fri, 21 Oct 2022 00:37:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666337820; cv=none; d=google.com; s=arc-20160816; b=qNr3tB2ih/yaL370pIl0detVdF+O7763CvHRjM8sFdo7gb8RolTD26GjjlzF5ymluA /Hax8HmPOkyHnGrwOAhfl/Ka+zg6FBksb62tZkcQu+eMziAd9DSc8HC5zfIPR8AkNurE 5DTWU5ISuKkI+XvnvqD6PQB0RaCn9RJ0vt/bRe1wf7bVTAXZ+9d3QqeYXA23gyen+ZPg w637/s1RfazLaYBUMSt8AG42C+TiBznQo+fYk8udWbE1NcbuPcDgJY/2EstTrKLnslKB QsJFJ4FcpOADhb9dFaKSgE8UNyBZwOlikLAm9fuL7JqMeXUgy+4kSXlehWyxhAdHTzlJ KikA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Rz8S9MMQRt8zPpvrNpbhX5Tj2i1jjVBTVbkbjHZYyRM=; b=SUeaDMF39n3X2eYtB/0DmXs2rCPhHcsij5nXTzlpmQ4EIVSV13+PSCwrug0brb16EW TF5LtPcCJTbFT1mUqX72ZG4ax2+PHBmwc0FqvfFSMznBGVEzIDvn1AzUI6Qm9gj31qvo Cij6vuZneCAoVwcd24ErSbPm5Pi8NzrBjuc5a+pVBjCSxkfcuaYhmsPXtpWnJkvepa/M 6lf7m+R58e28/0P6FrTDUf5bm8uVSFlEC975jRYZNuiKn77rUN1xjTK7KPSbtw8ypCrW S6RYjponH7W3eB7+KjoNSfZFqfqq6dxmmdgC9ISTvpvptDn0FdYu0+R6FAwsNYxcw1gr OkTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b=WQE6C3Cf; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cr21-20020a170906d55500b007999075bf51si2325205ejc.224.2022.10.21.00.36.35; Fri, 21 Oct 2022 00:37:00 -0700 (PDT) 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=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b=WQE6C3Cf; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229956AbiJUGrp (ORCPT + 99 others); Fri, 21 Oct 2022 02:47:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229978AbiJUGrm (ORCPT ); Fri, 21 Oct 2022 02:47:42 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63843242C98 for ; Thu, 20 Oct 2022 23:47:39 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id w18so4764425ejq.11 for ; Thu, 20 Oct 2022 23:47:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Rz8S9MMQRt8zPpvrNpbhX5Tj2i1jjVBTVbkbjHZYyRM=; b=WQE6C3Cfh/sF5gQfmupWGacH2Ck1q0VF6PV83R45exfq8m59GATBQ3H7J//jcxkDNp G5K4CG+PbOJLrmWYhAR+g8BWQMn5Z6qYhOMx5XVIviGST2E1FHLZZWRKOfmraGsN8XHd krPv/COxA8nMYOyAbxPmWNNhNsaAflvIMqNuUzeOjyoFDr/zP8H5j5knuj4YC+8zSA1V NIgGclsyH5FaFGXoznhSAUviqAmc+BhFrn5MHPpBta5k8wmZLQKFQWzTj9/adOXgdpUv t5GRkqbIHJC+lsgAdGQrVAUeOZcpGxDsSPPuJgzz0mnOxsaT57QFq3vNIdDyas0tI1av +Rqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=Rz8S9MMQRt8zPpvrNpbhX5Tj2i1jjVBTVbkbjHZYyRM=; b=FDoblrnJJQHUVLFh7/2xZFNiVmgGHIc8QJaF5/oprwxQdyu5Aq8LmTHehs+YokZpMq z9HJCRGq56r3OSVIdwbt4Bipxm0htCepW7/vnR2i3m2XhXDD/qrlKywUFMBffNBMH9La xp9R6cOGBf0x1wUJoE7EtMf1TWAWj6KebNWINyyKw4BqHordNqs149F2VPC/dZIUXvr/ w9zxiJmk8x0nP7FhOxgjZ8uCSVddIZENjrvO02Sa4ptPxD6a7pH9+YgNnDmwkzwIw/m/ BfurdfJBPkkRUpddITSfwOvF+r2qLW3diVc/m0kG8oQMh1fofJnMd5WGNeEs3zGCe9VS qp/g== X-Gm-Message-State: ACrzQf3RrObX8Q5l+LCb4eX4asqdC9tCs3hbEbjKi6orqgg/pJMNlm55 nAiu7pdWOrC09y36LCILkdJxZOfeWZ1OT8wC8CnTog== X-Received: by 2002:a17:906:dac8:b0:741:545b:796a with SMTP id xi8-20020a170906dac800b00741545b796amr13903081ejb.240.1666334857669; Thu, 20 Oct 2022 23:47:37 -0700 (PDT) MIME-Version: 1.0 References: <20221019114535.131469-1-apatel@ventanamicro.com> In-Reply-To: From: Anup Patel Date: Fri, 21 Oct 2022 12:17:25 +0530 Message-ID: Subject: Re: [PATCH] RISC-V: KVM: Fix kvm_riscv_vcpu_timer_pending() for Sstc To: Atish Patra Cc: Anup Patel , Paolo Bonzini , Palmer Dabbelt , Paul Walmsley , Andrew Jones , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE 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 Fri, Oct 21, 2022 at 11:34 AM Atish Patra wrote: > > On Wed, Oct 19, 2022 at 4:45 AM Anup Patel wrote: > > > > The kvm_riscv_vcpu_timer_pending() checks per-VCPU next_cycles > > and per-VCPU software injected VS timer interrupt. This function > > returns incorrect value when Sstc is available because the per-VCPU > > next_cycles are only updated by kvm_riscv_vcpu_timer_save() called > > from kvm_arch_vcpu_put(). As a result, when Sstc is available the > > VCPU does not block properly upon WFI traps. > > > > To fix the above issue, we introduce kvm_riscv_vcpu_timer_sync() > > which will update per-VCPU next_cycles upon every VM exit instead > > of kvm_riscv_vcpu_timer_save(). > > > > Fixes: 8f5cb44b1bae ("RISC-V: KVM: Support sstc extension") > > Signed-off-by: Anup Patel > > --- > > arch/riscv/include/asm/kvm_vcpu_timer.h | 1 + > > arch/riscv/kvm/vcpu.c | 3 +++ > > arch/riscv/kvm/vcpu_timer.c | 17 +++++++++++++++-- > > 3 files changed, 19 insertions(+), 2 deletions(-) > > > > diff --git a/arch/riscv/include/asm/kvm_vcpu_timer.h b/arch/riscv/include/asm/kvm_vcpu_timer.h > > index 0d8fdb8ec63a..82f7260301da 100644 > > --- a/arch/riscv/include/asm/kvm_vcpu_timer.h > > +++ b/arch/riscv/include/asm/kvm_vcpu_timer.h > > @@ -45,6 +45,7 @@ int kvm_riscv_vcpu_timer_deinit(struct kvm_vcpu *vcpu); > > int kvm_riscv_vcpu_timer_reset(struct kvm_vcpu *vcpu); > > void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu); > > void kvm_riscv_guest_timer_init(struct kvm *kvm); > > +void kvm_riscv_vcpu_timer_sync(struct kvm_vcpu *vcpu); > > void kvm_riscv_vcpu_timer_save(struct kvm_vcpu *vcpu); > > bool kvm_riscv_vcpu_timer_pending(struct kvm_vcpu *vcpu); > > > > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > > index a032c4f0d600..71ebbc4821f0 100644 > > --- a/arch/riscv/kvm/vcpu.c > > +++ b/arch/riscv/kvm/vcpu.c > > @@ -708,6 +708,9 @@ void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu) > > clear_bit(IRQ_VS_SOFT, &v->irqs_pending); > > } > > } > > + > > + /* Sync-up timer CSRs */ > > + kvm_riscv_vcpu_timer_sync(vcpu); > > } > > > > int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq) > > diff --git a/arch/riscv/kvm/vcpu_timer.c b/arch/riscv/kvm/vcpu_timer.c > > index 185f2386a747..ad34519c8a13 100644 > > --- a/arch/riscv/kvm/vcpu_timer.c > > +++ b/arch/riscv/kvm/vcpu_timer.c > > @@ -320,20 +320,33 @@ void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu) > > kvm_riscv_vcpu_timer_unblocking(vcpu); > > } > > > > -void kvm_riscv_vcpu_timer_save(struct kvm_vcpu *vcpu) > > +void kvm_riscv_vcpu_timer_sync(struct kvm_vcpu *vcpu) > > { > > struct kvm_vcpu_timer *t = &vcpu->arch.timer; > > > > if (!t->sstc_enabled) > > return; > > > > - t = &vcpu->arch.timer; > > #if defined(CONFIG_32BIT) > > t->next_cycles = csr_read(CSR_VSTIMECMP); > > t->next_cycles |= (u64)csr_read(CSR_VSTIMECMPH) << 32; > > #else > > t->next_cycles = csr_read(CSR_VSTIMECMP); > > #endif > > +} > > + > > +void kvm_riscv_vcpu_timer_save(struct kvm_vcpu *vcpu) > > +{ > > + struct kvm_vcpu_timer *t = &vcpu->arch.timer; > > + > > + if (!t->sstc_enabled) > > + return; > > + > > + /* > > + * The vstimecmp CSRs are saved by kvm_riscv_vcpu_timer_sync() > > + * upon every VM exit so no need to save here. > > + */ > > + > > /* timer should be enabled for the remaining operations */ > > if (unlikely(!t->init_done)) > > return; > > -- > > 2.34.1 > > > > Ahh. That's a tricky one. Thanks for fixing it. > > Reviewed-by: Atish Patra Thanks, queued this patch for Linux-6.1-rcX Regards, Anup