Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp542329imn; Tue, 26 Jul 2022 03:00:48 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tjLOD7CtSjrWiTtP6w61Iuzvzh8qKvZbG6ciCQyrQb+XGhv6PBQ2163bo4RJVV4z6+AyXo X-Received: by 2002:a05:6402:54:b0:43b:5cbd:d5db with SMTP id f20-20020a056402005400b0043b5cbdd5dbmr18188602edu.264.1658829648659; Tue, 26 Jul 2022 03:00:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658829648; cv=none; d=google.com; s=arc-20160816; b=XeiqRJNlfHHOY7uGPJPy3Pm8BcLqWTvgQcxp88vEUno0aahlQ04UNsA/BEweBqdrdo GDldokrtlLaJ+aoxGR3/LHIPkKnKPi3vM8OHbg/dCYyEcRIxsdz+UHiR+AxXxO0imX0k ECyS1hDeu0aJQJ32tc+Ew1XPsGgaJ8nkuM85eQYzIJIcyuGxLYt75vRjAaYlU4mGBRGr Dv69hBDkD56PAxVSnV7wJfp3dmYOJGpeSjY2s7NFYzSpTXzNF/k8II61SuNBH/edn2AH ywqty4M3ztpldatdu5oFjhTsuHZyTmFDVLiHpdketKhqdBhGXTXCo29XpX325MomVpEn Lq6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=plrt28d2jmNTXoMbxu+vZ/2uLGRkrgD3ZIbANkDKEvw=; b=o2c4OiYR5XVV+LUhcq0iHaVuX7wPDyrVY37qR/dt+81Ek48OurRUQkkVFVEzonQbub F+w5BCa2MtVXZ3AzCyzyuPXBIiKTnKW9Se+UJ99oyx1+DO/9MrrrdUElJ8+pwImy4TZl obbIvGo5FoQp4sYc63Cu6Nz5oNpwqpKxQCtDgB52PldXnv0+h9OaWwJI9xkvazNowsXk AHzyVcm/IyE7KjqrrC4T+Za2ewjMqQFYwUtVlgSFaa8otSJXm3qfEr+haDD7dmiO+Rzj YADvE+qoFBsGhMEA9Kl/c6FV83XN+EPsvBDCqcejRpjNhjPfTkLMVArLsuMHxjcG89D2 dbQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b="j7V/dY/Q"; 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=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id js7-20020a17090797c700b0072b0f6f1456si1620589ejc.612.2022.07.26.03.00.22; Tue, 26 Jul 2022 03:00:48 -0700 (PDT) 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=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b="j7V/dY/Q"; 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=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238535AbiGZJuj (ORCPT + 99 others); Tue, 26 Jul 2022 05:50:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238332AbiGZJuh (ORCPT ); Tue, 26 Jul 2022 05:50:37 -0400 Received: from pandora.armlinux.org.uk (pandora.armlinux.org.uk [IPv6:2001:4d48:ad52:32c8:5054:ff:fe00:142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD4DC1ADA6; Tue, 26 Jul 2022 02:50:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2019; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=plrt28d2jmNTXoMbxu+vZ/2uLGRkrgD3ZIbANkDKEvw=; b=j7V/dY/QBbYtUTiMt9KviWnJ6U 71oODiVdS9SpAKvMWFSNQiJRIs7fIDW1QwjbyGd9gL40JoYFIm8DjYoOKrtjwTSJ82X42oh9GmPbm XVR7tMPrLSflxNBaCPMNYGZ1dd1A6VajuSij4MDVDQlprSoDSBYlNeyiZ+9Gj0h0kbZKqA0CNFqIV n2HNltEjO/+rravhaSCr6vIAiPb0CfZc7R34sbQMC8zZx4FetCOJs3+Tlocir5KKtOmMDZ+FX5Yq8 uPsG+Z++gj/Fv4TTyAXCAJ+AhRRI58ZJyhLuUK6Dk5qbcOfwl/gplYRfIsWsy6/ttd3DEYjIxrQlN zPI2LMMw==; Received: from shell.armlinux.org.uk ([fd8f:7570:feb6:1:5054:ff:fe00:4ec]:33566) by pandora.armlinux.org.uk with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1oGHCA-0003wb-DY; Tue, 26 Jul 2022 10:49:42 +0100 Received: from linux by shell.armlinux.org.uk with local (Exim 4.94.2) (envelope-from ) id 1oGHBx-00017g-HI; Tue, 26 Jul 2022 10:49:29 +0100 Date: Tue, 26 Jul 2022 10:49:29 +0100 From: "Russell King (Oracle)" To: Li Huafei Cc: Linus Walleij , ardb@kernel.org, will@kernel.org, mark.rutland@arm.com, broonie@kernel.org, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, arnd@arndb.de, rostedt@goodmis.org, nick.hawkins@hpe.com, john@phrozen.org, mhiramat@kernel.org, ast@kernel.org, linyujun809@huawei.com, ndesaulniers@google.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: Re: [PATCH 3/5] ARM: stacktrace: Allow stack trace saving for non-current tasks Message-ID: References: <20220712021527.109921-1-lihuafei1@huawei.com> <20220712021527.109921-4-lihuafei1@huawei.com> <1288c73b-cf29-707d-47cb-4e2737300a29@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1288c73b-cf29-707d-47cb-4e2737300a29@huawei.com> Sender: Russell King (Oracle) X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_NONE 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 On Tue, Jul 26, 2022 at 05:12:39PM +0800, Li Huafei wrote: > > > On 2022/7/18 17:07, Linus Walleij wrote: > > On Tue, Jul 12, 2022 at 4:18 AM Li Huafei wrote: > > > >> The current ARM implementation of save_stack_trace_tsk() does not allow > >> saving stack trace for non-current tasks, which may limit the scenarios > >> in which stack_trace_save_tsk() can be used. Like other architectures, > >> or like ARM's unwind_backtrace(), we can leave it up to the caller to > >> ensure that the task that needs to be unwound is not running. > >> > >> Signed-off-by: Li Huafei > > > > That sounds good, but: > > > >> if (tsk != current) { > >> -#ifdef CONFIG_SMP > >> - /* > >> - * What guarantees do we have here that 'tsk' is not > >> - * running on another CPU? For now, ignore it as we > >> - * can't guarantee we won't explode. > >> - */ > >> - return; > >> -#else > >> + /* task blocked in __switch_to */ > > > > The commit text is not consistent with the comment you are removing. > > > > The commit is talking about "non-current" tasks which is one thing, > > but the code is avoiding any tasks under SMP because they may be > > running on another CPU. So you need to update the commit > > message to say something like "non-current or running on another CPU". > > > > If this condition will be checked at call sites in following patches, > > then mention > > that in the commit as well, so we know the end result is that we do > > not break it, > > The generic code stack_trace_save_tsk() does not have this check, and by > 'caller' I mean the caller of stack_trace_save_tsk(), expecting the > 'caller' to ensure that the task is not running. So in effect this check > has been dropped and there is no more guarantee. Sorry for not > clarifying the change here. Can you prove in every case that the thread we're being asked to unwind is not running? I don't think you can. There are things like proc_pid_stack() in procfs and the stack traces in sysrq-t which have attempted to unwind everything whether it's running or not. So no, there is no guarantee that the thread is blocked in __switch_to(). > But can we assume that the user should know that the stacktrace is > unreliable for a task that is running on another CPU? If not, I should > remove this patch and keep the check. It's not about "unreliable" stack traces, it's about the unwinder killing the kernel. The hint is this: frame.fp = thread_saved_fp(tsk); frame.sp = thread_saved_sp(tsk); frame.lr = 0; /* recovered from the stack */ frame.pc = thread_saved_pc(tsk); These access the context saved by the scheduler when the task is sleeping. When the thread is running, these saved values will be the state when the thread last slept. However, with the thread running, the stack could now contain any data what so ever, and could change at any moment. Whether the unwind-table unwinder is truely safe in such a situation is unknown - we try to ensure that it won't do anything stupid, but proving that is a hard task, and we've recently had issues with the unwinder even without that. So, allowing this feels like we're opening the door to DoS attacks from userspace, where userspace sits there reading /proc/*/stack of some thread running on a different CPU waiting for the kernel to oops itself, possibly holding a lock, resulting in the system dying. These decisions need to be made by architecture code not generic code, particularly where the method of unwinding is architecture specific and thus may have criteria defining when its safe to do so. -- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!