From patchwork Thu Nov 10 16:14:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 18197 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp241098wru; Thu, 10 Nov 2022 08:23:24 -0800 (PST) X-Google-Smtp-Source: AMsMyM7QjAp/+SSwd5yy9krLgPLW+DlO5XAx3a1xcPqPlirv13LUq7pFvaRSaflUvRACHMm86vyj X-Received: by 2002:a05:6a02:113:b0:43f:3554:ff9c with SMTP id bg19-20020a056a02011300b0043f3554ff9cmr56933570pgb.578.1668097403862; Thu, 10 Nov 2022 08:23:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668097403; cv=none; d=google.com; s=arc-20160816; b=o0foyxSVrVZk1jXq5KHU6GYHL9ErjiKVhnFbyqxz7FehBh6RqEYO1bfDX80YLD/3Rj Vh5Z2+Dk2ukuhE3e08LBqZG7diWEkAgjZcq+CHSKrxpuwkmJqgzkhiJiJWsiwL/nZwP6 sEmYBuyf57unnUPrtEPYRX5YYHCa8Bm3iAbZW4MYMc7th5iBgLC1Fv+GugizeLXm6Uo5 OFbNQJFM3PrMjLg3J55DIDZy3X7ZPPSThRA7XbX5DfrIcXoQYy2wDNgmKL2Mwa3+7gRV qWbAqSCJfxP8OE8x1kxvujCc8AjeZ4elEYg0u9IB/TtJxndTPWHNPmOEEo9epktqJWOR s0bA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=qqUz/+ZIm84a5MGcesgiYEEHMgajdSK9pGpAekBbKYs=; b=qLquDEWJyZB6IKYPh32LFOoLesyPC9zok68wnwsGxnHBB2t3XyCSAU/Z4+KOHY6ByB now3npzo2KOyjtl2TGDhm8eV4LIMAi+8PPm5e6XhD9G2rlqWLuPwhKpc7bpmj+2DYXGf 101B0dBXk6rNOADnmV7M/u+g2OiyBrQTMmjVv5lgWxhABsigaDzvl8WY6ZvDlFZKJFBw aCANcTjCciiLgAbCvywxth/z96WriBnQoG7pk2mHswLwMEMd9YD2FVHGNjyZLRV9vQBT sUzKxIva3Cre2NOD+VxTbA8bacM0xNFAqNioO02MwAiNCdd2U8wd9fbn2kgjJm1DjY9F xByQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=LV2HHKg6; dkim=neutral (no key) header.i=@linutronix.de; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e19-20020a63e013000000b0046ec0715bd9si21268745pgh.818.2022.11.10.08.23.09; Thu, 10 Nov 2022 08:23:23 -0800 (PST) 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=@linutronix.de header.s=2020 header.b=LV2HHKg6; dkim=neutral (no key) header.i=@linutronix.de; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230082AbiKJQOl (ORCPT + 99 others); Thu, 10 Nov 2022 11:14:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229701AbiKJQOg (ORCPT ); Thu, 10 Nov 2022 11:14:36 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85A4AEE0A; Thu, 10 Nov 2022 08:14:35 -0800 (PST) Date: Thu, 10 Nov 2022 16:14:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668096873; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qqUz/+ZIm84a5MGcesgiYEEHMgajdSK9pGpAekBbKYs=; b=LV2HHKg6XAO4C166KK4EmouHK9tldM9DHcLrVLaQnbVsAfnNNXLhWw5LUPwrlMxDW0ECAn XpEO/3OF9jQL9S5/6W+ejOq3J7cgnDWJkq6gwrIVtPdK2DyD272a6CsGLeg3hyC7Gja3Oa QQDIDNPhMPk1D4wuwvdKTownZHPssXA5/FkiC7fERbJ09mnfIORW3OEOOBWGGOlvqQJdPW HHTNGcNAWHxD2bKZEglL0aPLxHMwXcEVpUczchNG9qDa3FikBxU5rem46nP5I10lNj8X+l pbb26xlKnDiuQS20lV9AckjFD6gVkYkp/O65Sm4hXaJRFy4tcb2ft9aXKBY/QA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668096873; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qqUz/+ZIm84a5MGcesgiYEEHMgajdSK9pGpAekBbKYs=; b=QuwqpX568h8yWO/HVgMJaAs8olnVLnX6xJrxSqEMAdtO8rZq1NKTUJlBTdQY3Q8tWYe5Nx ov1Y0/wsitzTmbBw== From: "tip-bot2 for Mel Gorman" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] x86/fpu: Drop fpregs lock before inheriting FPU permissions Cc: Mike Galbraith , Mel Gorman , Borislav Petkov , Thomas Gleixner , , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20221110124400.zgymc2lnwqjukgfh@techsingularity.net> References: <20221110124400.zgymc2lnwqjukgfh@techsingularity.net> MIME-Version: 1.0 Message-ID: <166809687209.4906.5542537765622302733.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749126903103944777?= X-GMAIL-MSGID: =?utf-8?q?1749126903103944777?= The following commit has been merged into the x86/urgent branch of tip: Commit-ID: 36b038791e1e2baea892e9276588815fd14894b4 Gitweb: https://git.kernel.org/tip/36b038791e1e2baea892e9276588815fd14894b4 Author: Mel Gorman AuthorDate: Thu, 10 Nov 2022 12:44:00 Committer: Borislav Petkov CommitterDate: Thu, 10 Nov 2022 16:57:38 +01:00 x86/fpu: Drop fpregs lock before inheriting FPU permissions Mike Galbraith reported the following against an old fork of preempt-rt but the same issue also applies to the current preempt-rt tree. BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:46 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1, name: systemd preempt_count: 1, expected: 0 RCU nest depth: 0, expected: 0 Preemption disabled at: fpu_clone CPU: 6 PID: 1 Comm: systemd Tainted: G E (unreleased) Call Trace: dump_stack_lvl ? fpu_clone __might_resched rt_spin_lock fpu_clone ? copy_thread ? copy_process ? shmem_alloc_inode ? kmem_cache_alloc ? kernel_clone ? __do_sys_clone ? do_syscall_64 ? __x64_sys_rt_sigprocmask ? syscall_exit_to_user_mode ? do_syscall_64 ? syscall_exit_to_user_mode ? do_syscall_64 ? syscall_exit_to_user_mode ? do_syscall_64 ? exc_page_fault ? entry_SYSCALL_64_after_hwframe Mike says: The splat comes from fpu_inherit_perms() being called under fpregs_lock(), and us reaching the spin_lock_irq() therein due to fpu_state_size_dynamic() returning true despite static key __fpu_state_size_dynamic having never been enabled. Mike's assessment looks correct. fpregs_lock on a PREEMPT_RT kernel disables preemption so calling spin_lock_irq() in fpu_inherit_perms() is unsafe. This problem exists since commit 9e798e9aa14c ("x86/fpu: Prepare fpu_clone() for dynamically enabled features"). Even though the original bug report should not have enabled the paths at all, the bug still exists. fpregs_lock is necessary when editing the FPU registers or a task's FP state but it is not necessary for fpu_inherit_perms(). The only write of any FP state in fpu_inherit_perms() is for the new child which is not running yet and cannot context switch or be borrowed by a kernel thread yet. Hence, fpregs_lock is not protecting anything in the new child until clone() completes and can be dropped earlier. The siglock still needs to be acquired by fpu_inherit_perms() as the read of the parent's permissions has to be serialised. [ bp: Cleanup splat. ] Fixes: 9e798e9aa14c ("x86/fpu: Prepare fpu_clone() for dynamically enabled features") Reported-by: Mike Galbraith Signed-off-by: Mel Gorman Signed-off-by: Borislav Petkov Reviewed-by: Thomas Gleixner Cc: Link: https://lore.kernel.org/r/20221110124400.zgymc2lnwqjukgfh@techsingularity.net --- arch/x86/kernel/fpu/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 3b28c5b..d00db56 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -605,9 +605,9 @@ int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal) if (test_thread_flag(TIF_NEED_FPU_LOAD)) fpregs_restore_userregs(); save_fpregs_to_fpstate(dst_fpu); + fpregs_unlock(); if (!(clone_flags & CLONE_THREAD)) fpu_inherit_perms(dst_fpu); - fpregs_unlock(); /* * Children never inherit PASID state.