From patchwork Wed Nov 8 11:29:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 13449853 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 704ABC4332F for ; Wed, 8 Nov 2023 11:29:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:References:MIME-Version:Message-Id:Date :Subject:Cc:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: List-Owner; bh=JfeojaCO5AxH31/2vDnYR5RNXzOZlo8Our7kZLr89tA=; b=a5pipg+zR1jh0/ GDaJ3u5jbXMOOgR2fz9U22RJTOUcThk5ER7uCChhztbcZNoJ6cvaeKlgCfk0U1D/zf2rH8z3mlEYk pd3HXTlpzj/5B560JVCYKNnGOPL1qFmLUvcbv/xZ5UnSVGDV1NlzrxhwuKz2dX1yVTx7uQ785rYkx U+nnxxPzP3ZcbMDryWvECtjmIwtX5Qz/+XUoyFXnaThA9mIzXCqQr27VXaKF8GNglz63sQl2sTmHF 5TN42OVD6+d+X51YaF8kFQPp+Qiav4naQxjlm557w8yMC/24HcxJJr5yMWuFPn6/fCdoj3dZOl/Uz vEI2AJmFUR7hdg/CTorg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r0gkZ-003ZU5-2f; Wed, 08 Nov 2023 11:29:35 +0000 Received: from mailout1.w1.samsung.com ([210.118.77.11]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r0gkV-003ZSR-1T for linux-riscv@lists.infradead.org; Wed, 08 Nov 2023 11:29:33 +0000 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout1.w1.samsung.com (KnoxPortal) with ESMTP id 20231108112923euoutp0139f87b313437260d20b72941be350ebf~Vol8ouY_m2144021440euoutp01I for ; Wed, 8 Nov 2023 11:29:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20231108112923euoutp0139f87b313437260d20b72941be350ebf~Vol8ouY_m2144021440euoutp01I DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1699442964; bh=li73imUSCXRL1KLtY7SVTzDJF8se41kHvK+GR9zfnlY=; h=From:To:Cc:Subject:Date:References:From; b=padvNFtCYxF+Sh5dBt2tBmy4dqtIygcxAvvVP+HZMxMVlJD70DbBJAMuefeBOvk4z LRJDQsmUSXZw7HICFDa8mGGa2JSU3WM7l+MwEY9BAFdQj75VqwC5H8aEEx7X1wFJWi AGCJKEHMDY9iVx5IYP2JgrUyuj96ZSNCfqn5IZ44= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20231108112923eucas1p1d0be2cb9e1c6432a5391da0cfb60ff31~Vol8TdNcR1900119001eucas1p1Y; Wed, 8 Nov 2023 11:29:23 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id 5D.8F.11320.3117B456; Wed, 8 Nov 2023 11:29:23 +0000 (GMT) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20231108112923eucas1p27474e921565e3f175e27e6598f0b71c3~Vol783UbC0768707687eucas1p2T; Wed, 8 Nov 2023 11:29:23 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20231108112923eusmtrp292e05c11017f017fb2659021c103da80~Vol78MlCn1145311453eusmtrp2C; Wed, 8 Nov 2023 11:29:23 +0000 (GMT) X-AuditID: cbfec7f4-993ff70000022c38-e8-654b711342dd Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id 6E.4E.25043.3117B456; Wed, 8 Nov 2023 11:29:23 +0000 (GMT) Received: from AMDC4653.digital.local (unknown [106.120.51.32]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20231108112922eusmtip159190efd4fc28d99b4abf2982c2ba574~Vol7Mj0zD1977919779eusmtip1d; Wed, 8 Nov 2023 11:29:22 +0000 (GMT) From: Marek Szyprowski To: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Marek Szyprowski , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Andrew Jones , Heiko Stuebner , Evan Green , Jisheng Zhang , =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PATCH] riscv: fix potential panic during CPU hot-plug Date: Wed, 8 Nov 2023 12:29:09 +0100 Message-Id: <20231108112909.591987-1-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrCKsWRmVeSWpSXmKPExsWy7djP87rChd6pBhd+KlncmqRusfX3LHaL hSeTLaZs2sFucWSKncX/R69ZLZqPrWezuLxrDpvFts8tbBZrj9xlt3h5uYfZom0WvwOPx5uX L1k8Dnd8YffYtKqTzWPzknqPOz+WMnrcmBXh0bdlFaPHpebr7B7br81j9vj7cyuLx+dNcgHc UVw2Kak5mWWpRfp2CVwZO+d8Zio4p1WxqX0jUwNjq2oXIyeHhICJxIWLu5i6GLk4hARWMEos ffmWEcL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qPOz+WMnrcmBXh0bdlFaPHpebr7B7br81j9vj7cyuLx+dNcgHc UXo2RfmlJakKGfnFJbZK0YYWRnqGlhZ6RiaWeobG5rFWRqZK+nY2Kak5mWWpRfp2CXoZO+d8 Zio4p1WxqX0jUwNjq2oXIyeHhICJxLEtOxm7GLk4hASWMkrc+LaaESIhI3FyWgMrhC0s8eda FxtE0SdGif9/VjKBJNgEDCW63oIkODlEBJwlZv2bzwxSxCywklni7LLj7CAJYQF7iU0bp4NN ZRFQlTj0+zlYA6+AncSz4+1Q2+Ql9h88ywwRF5Q4OfMJC4jNDBRv3jqbeQIj3ywkqVlIUgsY mVYxiqSWFuem5xYb6RUn5haX5qXrJefnbmIERtK2Yz+37GBc+eqj3iFGJg7GQ4wSHMxKIrx/ 7T1ShXhTEiurUovy44tKc1KLDzGaAt03kVlKNDkfGMt5JfGGZgamhiZmlgamlmbGSuK8ngUd iUIC6YklqdmpqQWpRTB9TBycUg1MvG/yL+3XmJVnW/418sTX+qq2nDc6dvOvBzLEHjSIvFPM G7JvhnX+waftK/Ia5HWe+x8Q9v3zbjebrO2fu+rfop8vmdy6gy9t+Y8Td+8wVjFdc9jzXvdx IaPO9vIt1u4OsjVTay2X7Ni0OZfX+GTrSaM0fpUXfZ/c1pqJFs3ey5fyymB+3RtL/ocKNdPq JpT9mSn4OTw3HZg65a2eh0/8ESz9r976ctabnGeSSXkShyOmsMu2zfku8ec2Y1j/TNYG98e/ qg49+Tij6vp+7V1rtSSXyscUNua6739imvJzQVyBG3fVbmP5S781RB592yAub1rJ8HP1ix07 2TtCTee+Knht9SWmQvd2iDBftp0SS3FGoqEWc1FxIgA0BeAvLQMAAA== X-CMS-MailID: 20231108112923eucas1p27474e921565e3f175e27e6598f0b71c3 X-Msg-Generator: CA X-RootMTR: 20231108112923eucas1p27474e921565e3f175e27e6598f0b71c3 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20231108112923eucas1p27474e921565e3f175e27e6598f0b71c3 References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231108_032931_760187_1DAEA1CF X-CRM114-Status: GOOD ( 20.82 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Commit 584ea6564bca ("RISC-V: Probe for unaligned access speed") added a test for unaligned access speed. It is being performed when given CPU is onlined. Then, another test for misaligned access emulation has been added in commit 71c54b3d169d ("riscv: report misaligned accesses emulation to hwprobe"). This unaligned access speed doesn't really change after the boot, so it is sufficient to do this test only once. This has been partially added by commit c20d36cc2a20 ("riscv: don't probe unaligned access speed if already done"), but this optimisation works only if RISCV_HWPROBE_MISALIGNED_EMULATED is returned by the latter check. Otherwise the 'misaligned_access_speed' pcpu varliable is overwritten with RISCV_HWPROBE_MISALIGNED_UNKNOWN value, what makes the first check to be always performed. Recently I've noticed that the first check introduced a regression in the CPU hot-plug mechanism. This can be observed as a following panic on QEmu: CPU1: off cpu1: Ratio of byte access time to unaligned word access is 7.00, unaligned accesses are fast CPU1: off CPU1: failed to come online ------------[ cut here ]------------ WARNING: CPU: 0 PID: 215 at kernel/smp.c:437 __flush_smp_call_function_queue+0x90/0x292 Modules linked in: CPU: 0 PID: 215 Comm: bash Not tainted 6.5.0-rc1+ #7524 Hardware name: riscv-virtio,qemu (DT) epc : __flush_smp_call_function_queue+0x90/0x292 ra : smpcfd_dying_cpu+0xe/0x1c ... [] __flush_smp_call_function_queue+0x90/0x292 [] smpcfd_dying_cpu+0xe/0x1c [] cpuhp_invoke_callback+0x124/0x322 [] _cpu_up+0x218/0x322 [] cpu_up+0x5c/0x98 [] cpu_device_up+0x14/0x1c [] cpu_subsys_online+0x10/0x18 [] device_online+0x56/0x72 [] online_store+0x8c/0xae [] dev_attr_store+0xe/0x1a [] sysfs_kf_write+0x2e/0x4c [] kernfs_fop_write_iter+0xe8/0x14e [] vfs_write+0x2a4/0x3e0 [] ksys_write+0x5e/0xc8 [] sys_write+0xe/0x16 [] do_trap_ecall_u+0xe0/0xf4 [] ret_from_exception+0x0/0x64 irq event stamp: 12335 hardirqs last enabled at (12335): [] console_unlock+0x156/0x186 hardirqs last disabled at (12334): [] console_unlock+0x140/0x186 softirqs last enabled at (12154): [] __do_softirq+0x3b0/0x470 softirqs last disabled at (12147): [] __irq_exit_rcu+0xa6/0xd0 ---[ end trace 0000000000000000 ]--- ------------[ cut here ]------------ kernel BUG at kernel/irq_work.c:245! Kernel BUG [#1] Modules linked in: CPU: 0 PID: 215 Comm: bash Tainted: G W 6.5.0-rc1+ #7524 Hardware name: riscv-virtio,qemu (DT) epc : irq_work_run_list+0x30/0x32 ra : irq_work_run+0x2a/0x4c ... [] irq_work_run_list+0x30/0x32 [] irq_work_run+0x2a/0x4c [] smpcfd_dying_cpu+0x12/0x1c [] cpuhp_invoke_callback+0x124/0x322 [] _cpu_up+0x218/0x322 [] cpu_up+0x5c/0x98 [] cpu_device_up+0x14/0x1c [] cpu_subsys_online+0x10/0x18 [] device_online+0x56/0x72 [] online_store+0x8c/0xae [] dev_attr_store+0xe/0x1a [] sysfs_kf_write+0x2e/0x4c [] kernfs_fop_write_iter+0xe8/0x14e [] vfs_write+0x2a4/0x3e0 [] ksys_write+0x5e/0xc8 [] sys_write+0xe/0x16 [] do_trap_ecall_u+0xe0/0xf4 [] ret_from_exception+0x0/0x64 Code: 8526 6084 f0ef f71f fce5 60e2 6442 64a2 6105 8082 (9002) 1101 ---[ end trace 0000000000000000 ]--- Kernel panic - not syncing: Fatal exception in interrupt SMP: stopping secondary CPUs SMP: failed to stop secondary CPUs 0-1 ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]--- To avoid calling those checks in the CPU hot-plug paths again and again, simply move the check at the beginning of the check_unaligned_access() function and rely on the value determined during the system boot. Fixes: 584ea6564bca ("RISC-V: Probe for unaligned access speed") Fixes: c20d36cc2a20 ("riscv: don't probe unaligned access speed if already done") Fixes: 71c54b3d169d ("riscv: report misaligned accesses emulation to hwprobe") Signed-off-by: Marek Szyprowski Reviewed-by: Evan Green --- arch/riscv/kernel/cpufeature.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 2f51dc4eaf77..3c502840a548 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -571,13 +571,13 @@ void check_unaligned_access(int cpu) void *src; long speed = RISCV_HWPROBE_MISALIGNED_SLOW; - if (check_unaligned_access_emulated(cpu)) - return; - /* We are already set since the last check */ if (per_cpu(misaligned_access_speed, cpu) != RISCV_HWPROBE_MISALIGNED_UNKNOWN) return; + if (check_unaligned_access_emulated(cpu)) + return; + page = alloc_pages(GFP_NOWAIT, get_order(MISALIGNED_BUFFER_SIZE)); if (!page) { pr_warn("Can't alloc pages to measure memcpy performance");