Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp6629612ybh; Thu, 8 Aug 2019 03:17:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqwRtiCvhi8O9xINMTkf24lpSfwAI91F8DfLgGJgB2G1auYpADav1Lhkzs2LCHGTYPThVu+W X-Received: by 2002:a63:7205:: with SMTP id n5mr11971450pgc.443.1565259428963; Thu, 08 Aug 2019 03:17:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565259428; cv=none; d=google.com; s=arc-20160816; b=PhdiHBqIJN318IL6X2u8TQ8c0hl/lQFlcMWkY5T4Q6gaV7PIBshPfkAWCpSCu/fZGy dT9T6t7keMy8QgTb/7pGQY7lA8ZoBuAgbQg80XETCUD4lsIB8lI884Cx4dcD/iI+cugP ValwI2gd3QNohWH5xoXHCcf1Jh2Y6uom+i3nABdwpSnv8Wy9sCo6feUfjHN/7zu7zhap 6aU021lF7MeGaAWSpfObg6wtza4GYKIXI0oTRZ1vtHMa7HUtkbGB1D4Vhk/wuWASTtwz AgFe+TWRcmRwrJpQLOQOdzl7a57Izwridn5/GQIAIldINdIipK7iyrPJ6pfXz5o7+KAe y+lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=v+UYL1lJMEimsIxPQMAwClByr6W4+EzjyE/vwfvpU4E=; b=kcM044COpFVhFXJkRehxgXrD8ZZLfQV9DZ7+HGgYwXYVp47bjBU6NMx9l2tgWUGETM StGWUBi/yB5UX1aZLwqzz7Stb0+gZpmMM/FJo5Qpygxg1eFjDkj58DjyoKrQU20ea03E ShreLEAlUH2qEmO59EGnfpsanbxTPWtSbpRzJvQjul/1JTOkmtdFYAssgghcM5q5ftkr z+S4Yy5a61N1g82jSaFB2OHnrZ9ma8ntOiTha1KcJvlTLiBFxAvNeHazdaT03AOZjRuq vii3kIRvVkT6Ahbf+5mh5JFAQ3CO8ysolqlAWCVWW5cLY2OnTNW/HQvxnZ2qYvUncYdO Xm7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=oWQVQFEx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y3si53378671pge.429.2019.08.08.03.16.53; Thu, 08 Aug 2019 03:17:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=oWQVQFEx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389907AbfHHKPP (ORCPT + 99 others); Thu, 8 Aug 2019 06:15:15 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:55542 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389756AbfHHKPP (ORCPT ); Thu, 8 Aug 2019 06:15:15 -0400 Received: by mail-wm1-f67.google.com with SMTP id f72so1811512wmf.5 for ; Thu, 08 Aug 2019 03:15:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=v+UYL1lJMEimsIxPQMAwClByr6W4+EzjyE/vwfvpU4E=; b=oWQVQFExEkqD49HHdq86d/5EnT0D3srhgWm5H7g9sSxBiejIOwa876X7+OWoIkqhjO D7lefyGhoJayobHcSloaoBuihf/zvTN9aDM+66dCHu1k6yFDNlwPE3I9BifwnQZuOeFK 3WT8xLSk5EH09smVEig6QEAhfXxAgnGrP/QyzT2VZosDzdycqOkjoDHTKUVLB0IhDnT7 yGykATt9nh56o683vCj8TKfu6x9LxmARAvq3dRApE/q3QowYCEB8UFu4RLS39SodB8CA FWcPm0bEoBPpAnqYLRzTl6GF4HHuGSVa3IYu1rvKyfj851VYyJe2qADdyRT3B9WntHQt Kj9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=v+UYL1lJMEimsIxPQMAwClByr6W4+EzjyE/vwfvpU4E=; b=m/Z90mZ7eyNSH08Fo5qns4lz2C6tpxhEuMJtusK8BQe5mNRxAAIk1nmG7UAZMA/3gd JSuVOceHhcDx8JFm5PAJqPVl0FUbKAFzZmqgH7wPUluhqJ1nlqaCnCJ+UYLE5dwWmyOk 4CpvnwdXelwE1a8og+PU4zKjXZDK7C6jeAWKsuJVM+qY/r1ZJ0KYHe2vCDShgGEOavWO 7dweu6zL90H6OAwaRAzH/mUJe/DJNA6iKRHJ1kfE5mZGr9xTrcpF3+TRMs966TzZHeua ZnyAU3+ae/PNAgtDyYJqyidubiIJO2i0yD5589MJjGcstY/1bhQKK/WbV2R1yuFQQVSj 1U3A== X-Gm-Message-State: APjAAAUj2Q/LV2DTlemJT/WwVpCNtQNCOmnHdNFdsvtchEMPHAJxr6VS Em1ayoPHE1ki7qpsj7gxIx6tzcrMr1R6apGXEV3Bxw== X-Received: by 2002:a1c:3d89:: with SMTP id k131mr3224593wma.24.1565259312766; Thu, 08 Aug 2019 03:15:12 -0700 (PDT) MIME-Version: 1.0 References: <1565251121-28490-1-git-send-email-vincent.chen@sifive.com> <1565251121-28490-2-git-send-email-vincent.chen@sifive.com> In-Reply-To: <1565251121-28490-2-git-send-email-vincent.chen@sifive.com> From: Anup Patel Date: Thu, 8 Aug 2019 15:45:00 +0530 Message-ID: Subject: Re: [PATCH 1/2] riscv: Correct the initialized flow of FP register To: Vincent Chen Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv , "linux-kernel@vger.kernel.org List" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 8, 2019 at 1:30 PM Vincent Chen wrote: > > The following two reasons cause FP registers are sometimes not > initialized before starting the user program. > 1. Currently, the FP context is initialized in flush_thread() function > and we expect these initial values to be restored to FP register when > doing FP context switch. However, the FP context switch only occurs in > switch_to function. Hence, if this process does not be scheduled out > and scheduled in before entering the user space, the FP registers > have no chance to initialize. > 2. In flush_thread(), the state of reg->sstatus.FS inherits from the > parent. Hence, the state of reg->sstatus.FS may be dirty. If this > process is scheduled out during flush_thread() and initializing the > FP register, the fstate_save() in switch_to will corrupt the FP context > which has been initialized until flush_thread(). > > To solve the 1st case, the initialization of the FP register will be > completed in start_thread(). It makes sure all FP registers are initialized > before starting the user program. For the 2nd case, the state of > reg->sstatus.FS in start_thread will be set to SR_FS_OFF to prevent this > process from corrupting FP context in doing context save. The FP state is > set to SR_FS_INITIAL in start_trhead(). > > Tested on both QEMU and HiFive Unleashed using BBL + Linux. > > Signed-off-by: Vincent Chen > --- > arch/riscv/include/asm/switch_to.h | 6 ++++++ > arch/riscv/kernel/process.c | 13 +++++++++++-- > 2 files changed, 17 insertions(+), 2 deletions(-) > > diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h > index 853b65e..d5fe573 100644 > --- a/arch/riscv/include/asm/switch_to.h > +++ b/arch/riscv/include/asm/switch_to.h > @@ -19,6 +19,12 @@ static inline void __fstate_clean(struct pt_regs *regs) > regs->sstatus |= (regs->sstatus & ~(SR_FS)) | SR_FS_CLEAN; > } > > +static inline void fstate_off(struct task_struct *task, > + struct pt_regs *regs) > +{ > + regs->sstatus = (regs->sstatus & ~(SR_FS)) | SR_FS_OFF; The SR_FS_OFF is 0x0 so no need for ORing it. > +} > + > static inline void fstate_save(struct task_struct *task, > struct pt_regs *regs) > { > diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c > index f23794b..e3077ee 100644 > --- a/arch/riscv/kernel/process.c > +++ b/arch/riscv/kernel/process.c > @@ -64,8 +64,16 @@ void start_thread(struct pt_regs *regs, unsigned long pc, > unsigned long sp) > { > regs->sstatus = SR_SPIE; > - if (has_fpu) > + if (has_fpu) { > regs->sstatus |= SR_FS_INITIAL; > +#ifdef CONFIG_FPU > + /* > + * Restore the initial value to the FP register > + * before starting the user program. > + */ > + fstate_restore(current, regs); > +#endif > + } > regs->sepc = pc; > regs->sp = sp; > set_fs(USER_DS); > @@ -75,10 +83,11 @@ void flush_thread(void) > { > #ifdef CONFIG_FPU > /* > - * Reset FPU context > + * Reset FPU state and context > * frm: round to nearest, ties to even (IEEE default) > * fflags: accrued exceptions cleared > */ > + fstate_off(current, task_pt_regs(current)); > memset(¤t->thread.fstate, 0, sizeof(current->thread.fstate)); > #endif > } > -- > 2.7.4 > Apart from above minor comment, looks good to me. Reviewed-by: Anup Patel Regards, Anup