From patchwork Tue Jan 2 12:37:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 13509001 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 525CDC46CD2 for ; Tue, 2 Jan 2024 12:38:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8AAF66B0295; Tue, 2 Jan 2024 07:38:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 85ABF6B0296; Tue, 2 Jan 2024 07:38:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7224F6B0297; Tue, 2 Jan 2024 07:38:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 62AE86B0295 for ; Tue, 2 Jan 2024 07:38:03 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 312C9140796 for ; Tue, 2 Jan 2024 12:38:03 +0000 (UTC) X-FDA: 81634323246.23.D50D7B5 Received: from xry111.site (xry111.site [89.208.246.23]) by imf09.hostedemail.com (Postfix) with ESMTP id 810BA14000C for ; Tue, 2 Jan 2024 12:38:01 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=xry111.site header.s=default header.b=BTgilUQ6; dmarc=pass (policy=reject) header.from=xry111.site; spf=pass (imf09.hostedemail.com: domain of xry111@xry111.site designates 89.208.246.23 as permitted sender) smtp.mailfrom=xry111@xry111.site ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704199081; a=rsa-sha256; cv=none; b=PcLqvUC5w5X/j7yyy2Pi+GoLZOH5NpEPbaHRl5cOGCVak30yk4Btj7Nj0B6XH6bNq4TiDk ZUz1/zUjqMNKv85EBb1R3pSUIyyHHHSMp0tgh/53tkZPV9lmY5RGaMh2Q9h2Fpc9rmnDVp U7XV349szcplEkSsdArhz7eHXCM/pCg= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=xry111.site header.s=default header.b=BTgilUQ6; dmarc=pass (policy=reject) header.from=xry111.site; spf=pass (imf09.hostedemail.com: domain of xry111@xry111.site designates 89.208.246.23 as permitted sender) smtp.mailfrom=xry111@xry111.site ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704199081; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=KzE00UcCkA6zqAZDYN/iBHeYpcLH2O9z3NNWHdDRLAo=; b=SZ8aXFR9QzeH2P2tcMiTxeJtqKrpDuYxQ6c78DjLdUIhvZOLDGci4oQkDeKAs7b43R5sIJ uGum8Xs3V6BAGMkykNQXPObV8o+UogcsReiw4TU88PfSPowBIBjsRyA0bOwMdwHDHkwbXW fZt/7NAdeVm0hTDOWrmZ2CrRGgZ4l9U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1704199077; bh=ei3L/hMRlfNY7Sz9SmZ4DfXD98IpAmHi1yZnfBh/zq8=; h=From:To:Cc:Subject:Date:From; b=BTgilUQ6d6Nhlffji4EuRffpQbwj5zKUAVdh1QrfJBokQNNr263l+XmZ3oloR5grM szxThG4GcKMu1GQxiYesEcu0yZhL4SlgbrAnlckyk6byN3iVvXlst4LYk9BnGp7Rbf OAl17T5daR/pwWCkvEGxbuX28lsp318dr11H3CdE= Received: from stargazer.. (unknown [113.200.174.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id C76556707D; Tue, 2 Jan 2024 07:37:52 -0500 (EST) From: Xi Ruoyao To: Huacai Chen , WANG Xuerui , Jiaxun Yang Cc: Eric Biederman , Kees Cook , Tiezhu Yang , Jinyang He , loongarch@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Xi Ruoyao , stable@vger.kernel.org, linux-mips@vger.kernel.org Subject: [PATCH v3] LoongArch: Fix and simplify fcsr initialization on execve Date: Tue, 2 Jan 2024 20:37:07 +0800 Message-ID: <20240102123706.6099-2-xry111@xry111.site> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 810BA14000C X-Stat-Signature: g66d6ewte75knihm7bke316tnqy9ro8y X-HE-Tag: 1704199081-598080 X-HE-Meta: U2FsdGVkX18dMIXbvGAY30GZrF5r3rYEYAgOrdIG20qx4jdZ+KgJr48in217iit4A+0hGCIOU5E//H8hqiiEDkcq5A9kDOktp4MMfKeRgifg+rtuWhwz2PidJ6HkbmTMBnwqwyaQN0+NY2mhwcBU5g3TdWv+P0XeHMx/3V97extXzjHkUpZP0kLBGwl5xv7e6KBGMLaLqupbrXsVbb5ZAoMVEhgSMd+C2E7hl4moTJP9NY4n4gUzXx+dk0H4kvX62dV2z/kUrwxOumCtLTCM73poZaVfA4nRXNUo6BjVuBlHO+/GJtAUE9MiDna/IGsDd7yCs93lkEKEl2GUbF6TYqECD8kbyiOZjoHDIQeSag+FEtuoduV1VQa3Cs3O/v3ihnTb9t+EhIjXbuFuAt37G8pC0l3JNMSGABzi5eap6VKpCokzP5p6TUKLXxm2h0vfPh1cvtlVcmETQks9CmA6ZdqMxBDpLBahjTZ+xw9XLfys3zunfYVfUdKKMDzcsyUqHhPDkjuwHRTrvlsL0j91r5UgVYyxuYzd+vbadZO1zuiI0Hdhq86xfZ08jZ2kxc6mDK8JdDQvppJvi+APxouxUPcQKI/7IEE5mkM4OHcnDMJlPPyADCfhr6ucIOWu+E+6GSjWFTYeHwM1QVpnb05Jam5nyrm9cvYBWsTc2clXCSq/Y1G3vnQt1+gIoVKUCjMgtFanCKOMH4K/YvA7J8b+MHuKs+y61acbsyoz5MS+fydPgLWFGijbXwu3+WrLuxVeROXyXZJKC4CBw0+GgTD+wO8I8yM98LOhOj+/9c1B/qHmXsYtZqD1nyzYTtrByDTxiBwCnTqbmKgJUmkwhnrfQwa4l4gw+LLdIF2I/fc/n4Q/w3bbredy0jGkX+HKHA82yy+pxXK5D48+49+O5Cz8+wUMz1TQrCFR5kVfT/WpTyAGNSwpaSjOc8lf1Rwgk1fQE+MyrzveFRvvo6413Rj L8wRTBeo lIN1AwKS5MMG+/yWQl1eKOGOwaQ60UhUSkwwpQLyuMlSgU+5V9AC0lDjrLHP4KwuV3YmQj1zjySMgNEP6WCSgU6AWx3GEs0iQGiMWpcIDH0PWw3Un1Q7ZHfDHLbBGApXuBDJDdO8QsZNts2IRK4PpnNV4dIRCPM+xrhB4x1hLD5o4DF3u+ady+Yo1Fu9nvlzvmFDPsEg4+WoPxj+Oj9/2n+b5MPt/8jVRXg7ttE8MqcgZlahs9yHN3Qt0F0K1u18XEhkXnBn3bnFuJSR+DT9A6K1d71LexP9uKLKX+z2N0T8Eh/Tx0nwpZ383meRLOfzYn3QtbMHUT/p4gNlycakm5NN/2Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. I've ran a similar test on MIPS with mainline kernel and it turns out MIPS is buggy too. Anyway MIPS do this for supporting different FP flavors (NaN encodings etc.) which do not exist on LoongArch. So for LoongArch, we can simply remove the current->thread.fpu.fcsr setting from SET_PERSONALITY2 and do it in start_thread, after lose_fpu(0). I'll leave the job to fix MIPS for MIPS maintainers. The while loop failing with the mainline kernel has survived one hour after this change on LoongArch. Closes: https://github.com/loongson-community/discussions/issues/7 Fixes: 803b0fc5c3f2 ("LoongArch: Add process management") Link: https://lore.kernel.org/linux-mips/7a6aa1bbdbbe2e63ae96ff163fab0349f58f1b9e.camel@xry111.site/ Cc: stable@vger.kernel.org Cc: linux-mips@vger.kernel.org Signed-off-by: Xi Ruoyao --- v2 -> v3: - Update the commit message to mention MIPS is buggy too. - Replace tabs in the commit message with whitespaces. - No code change. 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/asm/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 pt_regs *regs); do { \ current->thread.vdso = &vdso_info; \ \ - loongarch_set_personality_fcsr(state); \ - \ if (personality(current->personality) != PER_LINUX) \ set_personality(PER_LINUX); \ } while (0) @@ -259,7 +257,6 @@ do { \ clear_thread_flag(TIF_32BIT_ADDR); \ \ current->thread.vdso = &vdso_info; \ - loongarch_set_personality_fcsr(state); \ \ p = personality(current->personality); \ if (p != PER_LINUX32 && p != PER_LINUX) \ @@ -340,6 +337,4 @@ extern int arch_elf_pt_proc(void *ehdr, void *phdr, struct 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, void *_interp_ehdr, { return 0; } - -void loongarch_set_personality_fcsr(struct arch_elf_state *state) -{ - current->thread.fpu.fcsr = boot_cpu_data.fpu_csr0; -} diff --git a/arch/loongarch/kernel/process.c b/arch/loongarch/kernel/process.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 pc, unsigned long sp) clear_used_math(); regs->csr_era = pc; regs->regs[3] = sp; + current->thread.fpu.fcsr = boot_cpu_data.fpu_csr0; } void flush_thread(void)