Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp2000416rdb; Mon, 9 Oct 2023 09:21:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEvfntkrWNcDJMp9Q87nuCET/v5aJSkf4li5YgyfzMjWWwf2UZhJZC29BQNT1C0tqOQrXqI X-Received: by 2002:a17:90a:a085:b0:276:e14a:4991 with SMTP id r5-20020a17090aa08500b00276e14a4991mr12455218pjp.2.1696868463931; Mon, 09 Oct 2023 09:21:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696868463; cv=none; d=google.com; s=arc-20160816; b=CtjUOqwiaBGBzIDmNf2UUrN+2QUEq+4wnMhY9mSBRMJhf56pQ9xXJaRAKDtPjbjpaB SiVz6vl5usXmwgw5/wrKUrH6uG5LtsSpKq3+wrx8Cc5FA2TuPtOs+jrpf9HsGehkQjQ7 mSH+hfRDIVPg29yZqNGGi4OfgIjIXbgn0caWrH6CKnCNWXT/zeqxgN0M/ZjKj6c/36dN NfQxPIzAmmIWR2N7CjuGcfshVAvzfFWw8EXYYCF0uGEnNpzharN24zEeTDiVrtcPPvnr jSuCFrK60Mbd/1RdXKYuzOGhS30kN0n6UjA9RLEhUaYYS9KVt4F4KtgpXJo60boLtEBP eUAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=T0FniyFfifdCrkzWp80VUh+eKcwZnexChrXjRuoWv5Q=; fh=2yrGsK1OnlD2c04EkO/R1z4r7P4KTbmP/ZL/wrLwUB4=; b=bvZJ7z56i+7eLSJh3I/LhfuWUbKbX4LIA6VehE9osLs42geSaVsjedG/lFsuqht3Qb Y9u7rZaI0Epk84aN+iKWbGrbi4WnI0II0PudIkPpCquOIik8E3KmIwlqbriknrc3ECSv ehMTRX3XPi23Fg6Bwz6bAVHviVQbS5aSflboRiwi5nZ4UtNbcxYe6x0+bEOe/ng26R0w Md48a/Mo6W3tgtgxX4v/zeEB0rzeW0tV8Cdwn/PGluarT+VZiUg789bWnFJ54XXt/voq 0pc10h+PZ7Ys1sLR5ToUN7zB/p9sDaFSj7iosvFi2YF/+xvkoWx6lHA/O2dJN1iX+eg+ hqMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=a0xU1lr7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id nv8-20020a17090b1b4800b0026c198d8182si12777043pjb.105.2023.10.09.09.21.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 09:21:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=a0xU1lr7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 23CD78028F99; Mon, 9 Oct 2023 09:21:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376782AbjJIQUo (ORCPT + 99 others); Mon, 9 Oct 2023 12:20:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346619AbjJIQUl (ORCPT ); Mon, 9 Oct 2023 12:20:41 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BBE6A6; Mon, 9 Oct 2023 09:20:37 -0700 (PDT) Received: from localhost.localdomain (unknown [116.71.10.238]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: usama.anjum) by madras.collabora.co.uk (Postfix) with ESMTPSA id 028206607038; Mon, 9 Oct 2023 17:20:32 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1696868436; bh=KAhhKO4g/GCRCekMIhf+cXHcdkyda+W3DvR4reXU71Q=; h=From:To:Cc:Subject:Date:From; b=a0xU1lr7jX1hDVGJIPpbK+z/xLeaBa5BctJgH0/H9L9DDq3NqBjL2VPpkeUu1QTdj JXUOdxSX4SsLLYZaO1ggNwlUOGkVIrHjLk924CjzPhqhEyXM0JE/cvlWFi/FDqbPOR 3OePQjYqNv1wOTrw/tJ3rjFqKqQXR6y6qL+KJqvzL/Z1AeuKOsZyBvtNoHgcJf8G0i n7Wsb/tpkvCxaaFaSWQdyBO7EAB+Xccws80sKknJyI4ZX4x/Zaf5BK8oa3nZvLT0yd HMUeepbvH9gOi9/BCrdOmO/bFAaF/h6Uq154ezlE7XfnL34HoEWVjOk6SBb50VjpyT U36WuxfVjevWg== From: Muhammad Usama Anjum To: Greg Kroah-Hartman , Jiri Slaby , Ingo Molnar Cc: Muhammad Usama Anjum , kernel@collabora.com, stable@vger.kernel.org, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v4] tty/sysrq: replace smp_processor_id() with get_cpu() Date: Mon, 9 Oct 2023 21:20:20 +0500 Message-Id: <20231009162021.3607632-1-usama.anjum@collabora.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 09 Oct 2023 09:21:00 -0700 (PDT) X-Spam-Level: ** The smp_processor_id() shouldn't be called from preemptible code. Instead use get_cpu() and put_cpu() which disables preemption in addition to getting the processor id. Enable preemption back after calling schedule_work() to make sure that the work gets scheduled on all cores other than the current core. We want to avoid a scenario where current core's stack trace is printed multiple times and one core's stack trace isn't printed because of scheduling of current task. This fixes the following bug: [ 119.143590] sysrq: Show backtrace of all active CPUs [ 119.143902] BUG: using smp_processor_id() in preemptible [00000000] code: bash/873 [ 119.144586] caller is debug_smp_processor_id+0x20/0x30 [ 119.144827] CPU: 6 PID: 873 Comm: bash Not tainted 5.10.124-dirty #3 [ 119.144861] Hardware name: QEMU QEMU Virtual Machine, BIOS 2023.05-1 07/22/2023 [ 119.145053] Call trace: [ 119.145093] dump_backtrace+0x0/0x1a0 [ 119.145122] show_stack+0x18/0x70 [ 119.145141] dump_stack+0xc4/0x11c [ 119.145159] check_preemption_disabled+0x100/0x110 [ 119.145175] debug_smp_processor_id+0x20/0x30 [ 119.145195] sysrq_handle_showallcpus+0x20/0xc0 [ 119.145211] __handle_sysrq+0x8c/0x1a0 [ 119.145227] write_sysrq_trigger+0x94/0x12c [ 119.145247] proc_reg_write+0xa8/0xe4 [ 119.145266] vfs_write+0xec/0x280 [ 119.145282] ksys_write+0x6c/0x100 [ 119.145298] __arm64_sys_write+0x20/0x30 [ 119.145315] el0_svc_common.constprop.0+0x78/0x1e4 [ 119.145332] do_el0_svc+0x24/0x8c [ 119.145348] el0_svc+0x10/0x20 [ 119.145364] el0_sync_handler+0x134/0x140 [ 119.145381] el0_sync+0x180/0x1c0 Cc: jirislaby@kernel.org Cc: stable@vger.kernel.org Fixes: 47cab6a722d4 ("debug lockups: Improve lockup detection, fix generic arch fallback") Signed-off-by: Muhammad Usama Anjum --- Changes since v3: - Update commit message to explain why preemption reenabling must happen after calling schedule_work() Changes since v2: - Add changelog and resend Changes since v1: - Add "Cc: stable@vger.kernel.org" tag --- drivers/tty/sysrq.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c index 23198e3f1461a..6b4a28bcf2f5f 100644 --- a/drivers/tty/sysrq.c +++ b/drivers/tty/sysrq.c @@ -262,13 +262,14 @@ static void sysrq_handle_showallcpus(u8 key) if (in_hardirq()) regs = get_irq_regs(); - pr_info("CPU%d:\n", smp_processor_id()); + pr_info("CPU%d:\n", get_cpu()); if (regs) show_regs(regs); else show_stack(NULL, NULL, KERN_INFO); schedule_work(&sysrq_showallcpus); + put_cpu(); } } -- 2.40.1