From patchwork Mon Mar 15 16:57:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12140099 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37376C433E0 for ; Mon, 15 Mar 2021 17:01:33 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D2A3F64DE0 for ; Mon, 15 Mar 2021 17:01:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D2A3F64DE0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; 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:To:From:Reply-To:Cc:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=jFefturVfjiVoM7WhOY1zPW3V5yEUyy0jNPUMposATc=; b=KJhYtObWkPFngdST8SLUU5Ble n3akujMrGpL5jXhxyavNLhsfeyoRg2wXimAnE5ixszk18oVPH8AuwXcMrHjb+KQe1aHn8Js00E2HX B1c8wjmHi69Z8WwJRvH1HTZyxxEoJfFpRauywQSUt6Opar4JKH3JYE28aLpLlc+Zn5KNl1ovDq7vM 6gbL3YFOkIviud78w/n7kvlf1wLPvRrbBF7i7Uo7xPuNJ795cRP5vYMT6FsbSZ2VLlXWaf+xVWYIc yjLsrRQVq6lUYXHC3Gep3IqKPnBRBzV/PSxngDSBGABQX7f6Wt7gP2QmqlNe1iGVp2Ab+Hv/xicnV 5NmDfjBbg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lLqZ9-00GQAF-Qi; Mon, 15 Mar 2021 16:59:39 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lLqXq-00GPos-Ep for linux-arm-kernel@lists.infradead.org; Mon, 15 Mar 2021 16:58:22 +0000 Received: from x64host.home (unknown [47.187.194.202]) by linux.microsoft.com (Postfix) with ESMTPSA id AF36C20B26FC; Mon, 15 Mar 2021 09:58:14 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com AF36C20B26FC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1615827495; bh=TPMn1YTK7zzFM7ywn8W1+RGydSCGcujwty9kEX/anVI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Nx301mR2PlY4Cw21cjxPBVPLKynmTKQ3bXAU1qjHdCodL6b9mqIIkgNKjbfH55NgH ICEPIZtTE9mGjfBxI85Gand/ox/arvN5X8A55FxaktvHkThxUlGy8u3VJvQcZAW8K0 8Q4EJfVX1W29IbQUoKEuo5d0iBV7x8bLbf8v/818= From: madvenka@linux.microsoft.com To: broonie@kernel.org, mark.rutland@arm.com, jpoimboe@redhat.com, jthierry@redhat.com, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [RFC PATCH v2 5/8] arm64: Detect an FTRACE frame and mark a stack trace unreliable Date: Mon, 15 Mar 2021 11:57:57 -0500 Message-Id: <20210315165800.5948-6-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210315165800.5948-1-madvenka@linux.microsoft.com> References: <5997dfe8d261a3a543667b83c902883c1e4bd270> <20210315165800.5948-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210315_165820_476368_22A30900 X-CRM114-Status: GOOD ( 18.46 ) 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: "Madhavan T. Venkataraman" When CONFIG_DYNAMIC_FTRACE_WITH_REGS is enabled and tracing is activated for a function, the ftrace infrastructure is called for the function at the very beginning. Ftrace creates two frames: - One for the traced function - One for the caller of the traced function That gives a reliable stack trace while executing in the ftrace infrastructure code. When ftrace returns to the traced function, the frames are popped and everything is back to normal. However, in cases like live patch, execution is redirected to a different function when ftrace returns. A stack trace taken while still in the ftrace infrastructure code will not show the target function. The target function is the real function that we want to track. So, if an FTRACE frame is detected on the stack, just mark the stack trace as unreliable. Signed-off-by: Madhavan T. Venkataraman --- arch/arm64/kernel/entry-ftrace.S | 2 ++ arch/arm64/kernel/stacktrace.c | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/arch/arm64/kernel/entry-ftrace.S b/arch/arm64/kernel/entry-ftrace.S index b3e4f9a088b1..1ec8c5180fc0 100644 --- a/arch/arm64/kernel/entry-ftrace.S +++ b/arch/arm64/kernel/entry-ftrace.S @@ -74,6 +74,8 @@ /* Create our frame record within pt_regs. */ stp x29, x30, [sp, #S_STACKFRAME] add x29, sp, #S_STACKFRAME + ldr w17, =FTRACE_FRAME + str w17, [sp, #S_FRAME_TYPE] .endm SYM_CODE_START(ftrace_regs_caller) diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 6ae103326f7b..594806a0c225 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -23,6 +23,7 @@ static void check_if_reliable(unsigned long fp, struct stackframe *frame, { struct pt_regs *regs; unsigned long regs_start, regs_end; + unsigned long caller_fp; /* * If the stack trace has already been marked unreliable, just @@ -68,6 +69,38 @@ static void check_if_reliable(unsigned long fp, struct stackframe *frame, frame->reliable = false; return; } + +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS + /* + * When tracing is active for a function, the ftrace code is called + * from the function even before the frame pointer prolog and + * epilog. ftrace creates a pt_regs structure on the stack to save + * register state. + * + * In addition, ftrace sets up two stack frames and chains them + * with other frames on the stack. One frame is pt_regs->stackframe + * that is for the traced function. The other frame is set up right + * after the pt_regs structure and it is for the caller of the + * traced function. This is done to ensure a proper stack trace. + * + * If the ftrace code returns to the traced function, then all is + * fine. But if it transfers control to a different function (like + * in livepatch), then a stack walk performed while still in the + * ftrace code will not find the target function. + * + * So, mark the stack trace as unreliable if an ftrace frame is + * detected. + */ + if (regs->frame_type == FTRACE_FRAME && frame->fp == regs_end && + frame->fp < info->high) { + /* Check the traced function's caller's frame. */ + caller_fp = READ_ONCE_NOCHECK(*(unsigned long *)(frame->fp)); + if (caller_fp == regs->regs[29]) { + frame->reliable = false; + return; + } + } +#endif } /*