From patchwork Tue Sep 13 06:31:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Kuohai X-Patchwork-Id: 12974457 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 BF0F3C54EE9 for ; Tue, 13 Sep 2022 06:27:41 +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=2MvHnLG/pzjT7Z yEA7DtFku/sPXXkyLe64UX4WB83CKKJaB6bMJvWewBLOuzX6T+v7SEKNWuax7qEuiR9rVJahDmX2/ 3xGQMmb4CFJDt9IRsZNG3NeIbF/C4OtRzCXaGvUELkZmj+SR80CArnyx+Si0KeQXTbIHbLGi3Xztv gRWOXsPrM+6zNWM6MxwGS6oKj6N5W2i6Aa7wyw4URamVo0OAcqlnmEGsojJpJa5gNi9VcPqvSmdnZ 5jfxV98A13rWKkkiuXe9NrXgqbuucSjezqcIKC94/GMuyuaH4s+55IBv8uw3JJFRbP7rSgpJr9oQs OPb0f4tee1z1uA2YeODQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oXzMx-002yXP-ID; Tue, 13 Sep 2022 06:26:03 +0000 Received: from dggsgout12.his.huawei.com ([45.249.212.56]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oXzLM-002wQF-0A for linux-arm-kernel@lists.infradead.org; Tue, 13 Sep 2022 06:24:27 +0000 Received: from mail02.huawei.com (unknown [172.30.67.169]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4MRYKT0r7Sz6S2hm for ; Tue, 13 Sep 2022 14:22:21 +0800 (CST) Received: from k01.huawei.com (unknown [10.67.174.197]) by APP4 (Coremail) with SMTP id gCh0CgCHSYkDIiBjT3sKAw--.4735S6; Tue, 13 Sep 2022 14:24:12 +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 4/4] ftrace: Fix dead loop caused by direct call in ftrace selftest Date: Tue, 13 Sep 2022 02:31:46 -0400 Message-Id: <20220913063146.74750-5-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220913063146.74750-1-xukuohai@huaweicloud.com> References: <20220913063146.74750-1-xukuohai@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgCHSYkDIiBjT3sKAw--.4735S6 X-Coremail-Antispam: 1UD129KBjvJXoW7uFy3WrWUXr4fCr43XF47urg_yoW8tFWDpa s3urnrKr15AF4kKas7u3W8CryUAwn8A343Kw1UG3sYvrZ8AryUKrZ2vrn7Z34DJa95C3y3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MR-646709E3 X-CRM114-CacheID: sfid-20220912_232424_752120_006820A8 X-CRM114-Status: GOOD ( 13.65 ) 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