Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1919839imu; Tue, 6 Nov 2018 06:26:11 -0800 (PST) X-Google-Smtp-Source: AJdET5cvs7YJb2+9rzkeeiAvueOiZ/LfdRZM0DwzqrY+Aw9nPtoTCIg/BfxzTSTBN0tggx/qdgX2 X-Received: by 2002:a63:f515:: with SMTP id w21mr4883763pgh.220.1541514371783; Tue, 06 Nov 2018 06:26:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541514371; cv=none; d=google.com; s=arc-20160816; b=j37Z96jlrDaMlsvh5M8VgD1Uj8rxzqg48TzbIxw3h7inHwxSpaQbE+PePmIviAs/xO PNk8fBigjbtm0anBGkmJ62cahzCsPU/yxcbZdZPHjgoJ+rzZ6mpZWkZvydPePiYo9Poj fk3WJyOmxgR6JQMTUJ3f7SoZTR/coCRsVgejiFFRhX7CbdT0QTBYXnsZW8HGwu5iNX3J XK8gDGFiyBN5BR33UF1o24HJyPvMtXgcpNSGoiptzITO8hqdWwG6smEaF2eC/auOoZW7 jBXkHv/EN6fvCuTSuMuHMFH8o4dzcxcQxCG+/IKUMwU6QomxCgeYL7zPg5uSCkOddzL0 5l/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=XkHev280RwbEffBo5619lLMbmc3EvZZe89tt5fwClNY=; b=e5qktF6BxzNHl2vn3Johwz6H5Cp4ypIvR7KkJb4O5bs4J5Zf42Yfeix8IgCtK9hViy Unbn3SY251Qh6mN1LvPogQwMkU8BE4tbYsekIolK5t2B++QNeoVTEXBt+1X5Q/uxhd2I NQIBh93lKUMXvfHjPBCPuH7vOxp26ve1J8maynjR4vDqV0ct8U9H/fxX88t94B3hpWJ/ U8sgHrwoDK+91MWDTP0m1o9QNrrDShy9HHsdTbW6veW1bwV/id66cDEYHOYE6CPCe+qS Gb9I8PvzIPsJAQlo3KtssY0PjM0SzxeIovHujf1tISy5H9IoxLlFx0O1/bo5kfcSUZ2W roUg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 3-v6si16567878plm.136.2018.11.06.06.25.50; Tue, 06 Nov 2018 06:26:11 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388737AbeKFXuU (ORCPT + 99 others); Tue, 6 Nov 2018 18:50:20 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:34580 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388702AbeKFXuT (ORCPT ); Tue, 6 Nov 2018 18:50:19 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2C8A480D; Tue, 6 Nov 2018 06:24:52 -0800 (PST) Received: from e103592.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A76E33F5BD; Tue, 6 Nov 2018 06:24:50 -0800 (PST) Date: Tue, 6 Nov 2018 14:24:48 +0000 From: Dave Martin To: Mark Rutland Cc: Daniel Thompson , Zhaoyang Huang , Catalin Marinas , Will Deacon , Michael Weiser , James Morse , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] arch/arm64 : fix error in dump_backtrace Message-ID: <20181106142446.GB3505@e103592.cambridge.arm.com> References: <1541488775-29610-1-git-send-email-huangzhaoyang@gmail.com> <20181106083901.erezwtcomiijvdrk@salmiak> <20181106085751.hrp7qkp53cftgew6@holly.lan> <20181106110019.36ps3tyakvocwst4@lakrids.cambridge.arm.com> <20181106113247.GF3500@e103592.cambridge.arm.com> <20181106122932.652iuh3c4qhe63gl@lakrids.cambridge.arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181106122932.652iuh3c4qhe63gl@lakrids.cambridge.arm.com> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 06, 2018 at 12:29:33PM +0000, Mark Rutland wrote: > On Tue, Nov 06, 2018 at 11:32:50AM +0000, Dave P Martin wrote: > > On Tue, Nov 06, 2018 at 11:00:19AM +0000, Mark Rutland wrote: > > > On Tue, Nov 06, 2018 at 08:57:51AM +0000, Daniel Thompson wrote: > > > > On Tue, Nov 06, 2018 at 08:39:01AM +0000, Mark Rutland wrote: > > > > > On Tue, Nov 06, 2018 at 03:19:35PM +0800, Zhaoyang Huang wrote: > > > > > > From: Zhaoyang Huang > > > > > > > > > > > > In some cases, the instruction of "bl foo1" will be the last one of the > > > > > > foo2[1], which will cause the lr be the first instruction of the adjacent > > > > > > foo3[2]. Hence, the backtrace will show the weird result as bellow[3]. > > > > > > The patch will fix it by miner 4 of the lr when dump_backtrace > > > > > > > > > > This has come up in the past (and a similar patch has been applied, then > > > > > reverted). > > > > > > > > > > In general, we don't know that a function call was made via BL, and therefore > > > > > cannot know that LR - 4 is the address of the caller. The caller could set up > > > > > the LR as it likes, then B or BR to the callee, and depending on how the basic > > > > > blocks get laid out in memory, LR - 4 might point at something completely > > > > > different. > > > > > > > > > > More ideally, the compiler wouldn't end a function with a BL. When does that > > > > > happen, and is there some way we could arrange for that to not happen? e.g. > > > > > somehow pad a NOP after the BL. > > > > > > > > It's a consequence of having __noreturn isn't it? __noreturn frees the > > > > compiler from the burden of having to produce a valid return stack... so > > > > it doesn't and unwinding becomes hard. > > > > > > In that case, the compiler could equally just use B rather than BL, > > > which this patch doesn't solve. > > > > > > The documentation for the GCC noreturn attribute [1] says: > > > > > > | In order to preserve backtraces, GCC will never turn calls to noreturn > > > | functions into tail calls. > > > > > > ... so clearly it's not intended to mess up backtracing. > > > > Which is a bit odd, since every call to a noreturn function is a tail- > > call by definition, and other tail-calls are typically optimised to a B > > (thus interfering with backtracing in all except the noreturn case). > > > > Avoiding this would require a change to the compiler, and since there's > > no obvious correct answer anyway, I guess we shouldn't hold our breath. > > > > > IIUC we mostly use noreturn to prevent warings about uninitialised > > > variables and such after a call to a noreturn function. I think > > > optimization is a secondary concern. > > > > Agreed. > > > > > We could ask the GCC folk if they can ensure that a noreturn function > > > call leave thes LR pointing into the caller, e.g. by padding with a NOP: > > > > > > BL > > > NOP > > > > > > That seems cheap enough, and would keep backtraces reliable. > > > > -fpatchable-function-entry=1,1 does almost the right thing, by > > inserting 1 NOP at the start of each function, and putting the function > > entry point after that (1) NOP. > > Neat hack, but unfortunately insufficient in general since: > > * The next function may be notrace or asm, and hence will not have a > preceding NOP. Usually not, but yes, it would be true for some cases. > * There may not be a next function (e.g. for the final instruction in > .text), and the LR value may point at some data symbol. Ditto. > * This relies on the preceding NOP being accounted as part of the > previous function, which feels like a bug given we should have the > function size somewhere. I assumed that the function sizes (in the ELF symbol senses) are simply ignored and the kernel presumes that each function ends where the next function starts. If so, the NOP would be accounted with the preceding function (if any). I may be misremembering how the kallsyms determines symbol sizes though. > Generally, I think that trying to bodge around the exiting behaviour is > going to cause just as many problems as it solves, and worse, makes it > harder to consistently analyse a backtrace. > > IMO, we shouldn't change the kernel here. Agreed. -fpatchable-function-entry is an interesting hack here, but doesn't solve the whole problem and partly works by accident (unfortunately). Cheers ---Dave