Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6473885rdb; Tue, 2 Jan 2024 03:17:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IGK03i+YsuzFigYNI/QGbHPSwo1u0/Mm7BwhJP3dimb/vPxHA58tF0OdSxJWqMmFX03vc2z X-Received: by 2002:a05:620a:2093:b0:781:4e17:7d75 with SMTP id e19-20020a05620a209300b007814e177d75mr11816760qka.109.1704194223057; Tue, 02 Jan 2024 03:17:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704194223; cv=none; d=google.com; s=arc-20160816; b=IV1je1RhAvuaDGiK9xkK8XmQzayWPFpOYarOHGQwfp87q31DtWO6zdnmSUShLPIKf6 yTEIkJ61H/hOKMp5DA/iHNJBP8NKUjjSCfyKKHYTlJy63NwrHZQY9EsMjyiyNfdDR+KI PShkiSeyBbvF78+AL01/nxrMGdsby6v6NFlT7Uu5q6R3oSvTjW1dBPreh++t9FgrDVQW c8WA50PS+K7ZqUJuur+NO0P8SoVQ1MAz4GCFeXj/gS10Ckas+P/jGsR9ieeF2jgG5w7B dkVdkCsHv/rKfloSsSFg6LEVB0vJPIN+liwX88nmrAgTUoeupRRGbFg/Hiqwm1hmr5Vc LBcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=ZnJwVDvjRupAlgztJKJPO9rm+AOYU0WF1cQQZkrOyhw=; fh=G3EDyY2gNteNvheoRM2pZwDXRRhOZ0BR5+7JIHkLQxs=; b=fyrdytOv4lKEBcpAvbAHcNxZGBE70ZtnztFfaAF/je4XBP8IJoHgjkeua62KBjWtBw eAYIYiwdWoczjSZ9IjrTkbs/FcC2OuPK9h5R+O3l8yiqI+wqp89mr0+9b9eRYPjxrsdR rnZp+oA6kpOvjslsr3Q2LHJ/43M/G6x4JMo+S8+0dqPFsNIbSDwj25EtqYsdvsYavPRw 7USsWc2htDiiApQYJ+HLYHhFLiy4KsZm2PL3hJyyhq7Z+9RhOf+UMVbMn19vhBLg7d8v xJmMjFqKUYnagLBLV3Iq+BxFZypzUpuyHSH3LvzJDvbc0Tg9WBGC46rmhmgrGvwGwCtd 2BHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rKmx+O3G; spf=pass (google.com: domain of linux-kernel+bounces-14277-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14277-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u8-20020a05620a430800b00781daefe4ffsi1422642qko.123.2024.01.02.03.17.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 03:17:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14277-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rKmx+O3G; spf=pass (google.com: domain of linux-kernel+bounces-14277-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14277-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 861291C218F2 for ; Tue, 2 Jan 2024 11:17:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EA107DF61; Tue, 2 Jan 2024 11:16:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rKmx+O3G" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21B68DDC7; Tue, 2 Jan 2024 11:16:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC65FC43391; Tue, 2 Jan 2024 11:16:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1704194212; bh=wH3UN+QEBeHo1CxZ6/L0nHaYUK8LzrtPCHlYvTqoAPQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=rKmx+O3G4u6tD/av1MVTGyVxCkd2s7aCJQMOAf5BmsZGJ2kac2aZZOirU3ltVF+xU BY2I1h4URbBV3ME3JYpYc/Lk7bneh1cTHtl6Qjv0k9fveNELZ6dSabLVupVUgQi3Hm sMXBGCCQsdVSeBQyp7+t9TNe9aI6HfuXY7p/xYhIwrJ6crJV6QVhWkeTMuJQqbkz5q sNLGt01zACtu7LZBl3P9NyS6Shd9TJ938UU64KsL9R/gGuwdsjdxkjo2BMlmmEtZxT PcdBKIAk2mDsoUuSm9isVjYr0aLeufNAyB6JCuGJSB7EoKW1+QfFl/s3toWaFJeDEr ubPjbOhP1PLVw== Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a282cb36d37so74783266b.3; Tue, 02 Jan 2024 03:16:52 -0800 (PST) X-Gm-Message-State: AOJu0YxbnANKJ+Uu1fCx+3omXVbBJGyz4w1+EbkBTROfOCcMo2hH1mhx udbmg2rggF8M5Am6D015zbh2zZUMgnvfpGTsDpc= X-Received: by 2002:a17:907:61a2:b0:a26:8ee9:9b31 with SMTP id mt34-20020a17090761a200b00a268ee99b31mr708210ejc.4.1704194211016; Tue, 02 Jan 2024 03:16:51 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240102101711.10872-2-xry111@xry111.site> In-Reply-To: <20240102101711.10872-2-xry111@xry111.site> From: Huacai Chen Date: Tue, 2 Jan 2024 19:16:43 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2] LoongArch: Fix and simplify fcsr initialization on execve To: Xi Ruoyao Cc: WANG Xuerui , Eric Biederman , Kees Cook , Tiezhu Yang , Jinyang He , Jiaxun Yang , loongarch@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Jan 2, 2024 at 6:17=E2=80=AFPM Xi Ruoyao wrote= : > > There has been a lingering bug in LoongArch Linux systems causing some > GCC tests to intermittently fail (see Closes link). I've made a minimal > reproducer: > > zsh% cat measure.s > .align 4 > .globl _start > _start: > movfcsr2gr $a0, $fcsr0 > bstrpick.w $a0, $a0, 16, 16 > beqz $a0, .ok > break 0 > .ok: > li.w $a7, 93 > syscall 0 > zsh% cc mesaure.s -o measure -nostdlib > zsh% echo $((1.0/3)) > 0.33333333333333331 > zsh% while ./measure; do ; done > > This while loop should not stop as POSIX is clear that execve must set > fenv to the default, where FCSR should be zero. But in fact it will > just stop after running for a while (normally less than 30 seconds). > Note that "$((1.0/3))" is needed to reproduce the issue because it > raises FE_INVALID and makes fcsr0 non-zero. > > The problem is we are relying on SET_PERSONALITY2 to reset > current->thread.fpu.fcsr. But SET_PERSONALITY2 is executed before > start_thread which calls lose_fpu(0). We can see if kernel preempt is > enabled, we may switch to another thread after SET_PERSONALITY2 but > before lose_fpu(0). Then bad thing happens: during the thread switch > the value of the fcsr0 register is stored into current->thread.fpu.fcsr, > making it dirty again. > > The issue can be fixed by setting current->thread.fpu.fcsr after > lose_fpu(0) because lose_fpu clears TIF_USEDFPU, then the thread > switch won't touch current->thread.fpu.fcsr. > > The only other architecture setting FCSR in SET_PERSONALITY2 is MIPS. > They do this for supporting different FP flavors (NaN encodings etc). > which do not exist on LoongArch. I'm not sure how MIPS evades the issue > (or maybe it's just buggy too) but I'll investigate it later. You have already investigated it, so should this message be updated? Huacai > > For LoongArch, just remove the current->thread.fpu.fcsr setting from > SET_PERSONALITY2 and do it in start_thread, after lose_fpu(0). > > The while loop failing with the mainline kernel has survived one hour > after this change. > > Closes: https://github.com/loongson-community/discussions/issues/7 > Fixes: 803b0fc5c3f2 ("LoongArch: Add process management") > Cc: stable@vger.kernel.org > Signed-off-by: Xi Ruoyao > --- > > v1 -> v2: > - Still set current->thread.fpu.fcsr to boot_cpu_data.fpu_csr0 instead > of constant 0. > > arch/loongarch/include/asm/elf.h | 5 ----- > arch/loongarch/kernel/elf.c | 5 ----- > arch/loongarch/kernel/process.c | 1 + > 3 files changed, 1 insertion(+), 10 deletions(-) > > diff --git a/arch/loongarch/include/asm/elf.h b/arch/loongarch/include/as= m/elf.h > index 9b16a3b8e706..f16bd42456e4 100644 > --- a/arch/loongarch/include/asm/elf.h > +++ b/arch/loongarch/include/asm/elf.h > @@ -241,8 +241,6 @@ void loongarch_dump_regs64(u64 *uregs, const struct p= t_regs *regs); > do { \ > current->thread.vdso =3D &vdso_info; = \ > \ > - loongarch_set_personality_fcsr(state); \ > - \ > if (personality(current->personality) !=3D PER_LINUX) = \ > set_personality(PER_LINUX); \ > } while (0) > @@ -259,7 +257,6 @@ do { = \ > clear_thread_flag(TIF_32BIT_ADDR); \ > \ > current->thread.vdso =3D &vdso_info; = \ > - loongarch_set_personality_fcsr(state); \ > \ > p =3D personality(current->personality); = \ > if (p !=3D PER_LINUX32 && p !=3D PER_LINUX) = \ > @@ -340,6 +337,4 @@ extern int arch_elf_pt_proc(void *ehdr, void *phdr, s= truct file *elf, > extern int arch_check_elf(void *ehdr, bool has_interpreter, void *interp= _ehdr, > struct arch_elf_state *state); > > -extern void loongarch_set_personality_fcsr(struct arch_elf_state *state)= ; > - > #endif /* _ASM_ELF_H */ > diff --git a/arch/loongarch/kernel/elf.c b/arch/loongarch/kernel/elf.c > index 183e94fc9c69..0fa81ced28dc 100644 > --- a/arch/loongarch/kernel/elf.c > +++ b/arch/loongarch/kernel/elf.c > @@ -23,8 +23,3 @@ int arch_check_elf(void *_ehdr, bool has_interpreter, v= oid *_interp_ehdr, > { > return 0; > } > - > -void loongarch_set_personality_fcsr(struct arch_elf_state *state) > -{ > - current->thread.fpu.fcsr =3D boot_cpu_data.fpu_csr0; > -} > diff --git a/arch/loongarch/kernel/process.c b/arch/loongarch/kernel/proc= ess.c > index 767d94cce0de..3f9cae615f52 100644 > --- a/arch/loongarch/kernel/process.c > +++ b/arch/loongarch/kernel/process.c > @@ -92,6 +92,7 @@ void start_thread(struct pt_regs *regs, unsigned long p= c, unsigned long sp) > clear_used_math(); > regs->csr_era =3D pc; > regs->regs[3] =3D sp; > + current->thread.fpu.fcsr =3D boot_cpu_data.fpu_csr0; > } > > void flush_thread(void) > -- > 2.43.0 >