From patchwork Tue Sep 13 16:27:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Kuohai X-Patchwork-Id: 12974957 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 53936ECAAD8 for ; Tue, 13 Sep 2022 16:11:49 +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:MIME-Version:References:In-Reply-To: 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: List-Owner; bh=mivw3z2RkWxAX5nvOBB9e5HVXAnHQgwvJPa8lX8iJJw=; b=06eu5RUM/ymXUk 4VJy4pfCSrAFq3BHKfaXFqFAf4BQIKWS6ks8cP4/GyfKNmoS9BSXJWmCdxSdUAUZdgCwSgbYQ0Ts0 h/6ycWhatG+awsCSd9QiEroZ3hNMqXacwnZ4Q6pLQEBVD64jxr7OWLqk1ranpz5pL8Rx2gTrFxmvA +JQI65mQgf1S0igl5X86v4RT3FEGkI9DQo4oJIoHwLNNHpL/owwlj1YNFOZxuF0hJq/BEtjm7h8uy d+6OrnqiTRbLYYi7yOaLL6oGMrFbmt9jru1jdCOVAJRa5Uljod+8FNHwZea2he8es1kwz+PhJj7yB MwMXEBQchk5S0WQWC69g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oY8Uk-00D4ab-35; Tue, 13 Sep 2022 16:10:42 +0000 Received: from dggsgout11.his.huawei.com ([45.249.212.51]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oY8Ue-00D4Rd-6c for linux-arm-kernel@lists.infradead.org; Tue, 13 Sep 2022 16:10:39 +0000 Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4MRpL13pb8zKHXh for ; Wed, 14 Sep 2022 00:08:41 +0800 (CST) Received: from k01.huawei.com (unknown [10.67.174.197]) by APP4 (Coremail) with SMTP id gCh0CgBHB4dxqyBjcEAeAw--.28569S6; Wed, 14 Sep 2022 00:10:33 +0800 (CST) From: Xu Kuohai To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: Mark Rutland , Florent Revest , Catalin Marinas , Will Deacon , Jean-Philippe Brucker , Steven Rostedt , Ingo Molnar , Oleg Nesterov , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Zi Shen Lim , Pasha Tatashin , Ard Biesheuvel , Marc Zyngier , Guo Ren , Masami Hiramatsu Subject: [PATCH bpf-next v2 4/4] ftrace: Fix dead loop caused by direct call in ftrace selftest Date: Tue, 13 Sep 2022 12:27:32 -0400 Message-Id: <20220913162732.163631-5-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220913162732.163631-1-xukuohai@huaweicloud.com> References: <20220913162732.163631-1-xukuohai@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgBHB4dxqyBjcEAeAw--.28569S6 X-Coremail-Antispam: 1UD129KBjvJXoW7uFy3WrWUXr4fCr43XF47urg_yoW8tFWDpa s3urnrKr15AF4kKas7u3W8CryUAwn8A343Kw1UG3sYvrZ8AryUKrZ2vrn7Z34DJa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E 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220913_091036_823945_079EA442 X-CRM114-Status: GOOD ( 13.66 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Xu Kuohai After direct call is enabled for arm64, ftrace selftest enters a dead loop: : 00 bti c 01 mov x9, x30 : 02 bl ----------> ret | lr/x30 is 03, return to 03 | 03 mov w0, #0x0 <-----------------------------| | | | dead loop! | | | 04 ret ---- lr/x30 is still 03, go back to 03 ----| The reason is that when the direct caller trace_direct_tramp() returns to the patched function trace_selftest_dynamic_test_func(), lr is still the address after the instrumented instruction in the patched function, so when the patched function exits, it returns to itself! To fix this issue, we need to restore lr before trace_direct_tramp() exits, so use a dedicated trace_direct_tramp() for arm64. Reported-by: Li Huafei Signed-off-by: Xu Kuohai Acked-by: Steven Rostedt (Google) --- arch/arm64/include/asm/ftrace.h | 4 ++++ kernel/trace/trace_selftest.c | 2 ++ 2 files changed, 6 insertions(+) diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrace.h index b07a3c24f918..15247f73bf54 100644 --- a/arch/arm64/include/asm/ftrace.h +++ b/arch/arm64/include/asm/ftrace.h @@ -128,6 +128,10 @@ static inline bool arch_syscall_match_sym_name(const char *sym, #define ftrace_dummy_tramp ftrace_dummy_tramp extern void ftrace_dummy_tramp(void); +#ifdef CONFIG_FTRACE_SELFTEST +#define trace_direct_tramp ftrace_dummy_tramp +#endif /* CONFIG_FTRACE_SELFTEST */ + #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ #endif /* ifndef __ASSEMBLY__ */ diff --git a/kernel/trace/trace_selftest.c b/kernel/trace/trace_selftest.c index a2d301f58ced..092239bc373c 100644 --- a/kernel/trace/trace_selftest.c +++ b/kernel/trace/trace_selftest.c @@ -785,8 +785,10 @@ static struct fgraph_ops fgraph_ops __initdata = { }; #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS +#ifndef trace_direct_tramp noinline __noclone static void trace_direct_tramp(void) { } #endif +#endif /* * Pretty much the same than for the function tracer from which the selftest