Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp2907811pxy; Mon, 3 May 2021 10:39:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcVkVKuTFEoIf7NJpgVMgnqFirIhgBqXcIEk4QwH3FUanGYXBrlF7xnHlsCQ5ANZ3jB7Gf X-Received: by 2002:a17:902:365:b029:ed:2596:e6ac with SMTP id 92-20020a1709020365b02900ed2596e6acmr21265069pld.31.1620063578554; Mon, 03 May 2021 10:39:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620063578; cv=none; d=google.com; s=arc-20160816; b=C5BePYSB5z/Yh2F9/NXPIjzO8ELVQ+a1I4xPO4MnzwoxsFm7h4v2VmNPeQqxETM8eD tifEEbBBikGHAKlcLmkJXmYGVm40uHXPd9WpHelVR7xGQt0igUsN6Az/YQEG8VWZBX04 8A/MlwjSSdwPV/uJIIrJmzDf1YVQYNZxL2YXDZK/m9IIc7Q7bZXmSruTmDXD0YtQ7SgN hCBaITHwbRm8QgZ9swAPCLD6eVhIMugwYL/Nh/JMjga1CnNwW6bpr5EhtXorELC1cs97 s/WpVUZDL4HA1M6pjfP6RPS0KpYDpqAvhAGJzWwNxhW8ctDWU2Re5lCo2Mjz2WLvfTnS TVEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dkim-filter; bh=bYCJMeYNqN5Rl7cucEEXyAKwSqecSENtHSd9RFOfCiw=; b=Ve7BqZ5VNWsAKl3sF7sERgqZs14uxjlQh9ahAxsQycXGE5CEZUiX4LyrCC07qJ8bMY U26KiSd5S0HjpoUKYVT2H/3DviA5deCFOdVkgwKFL7N4lXi++peDnnlkNkt3U2zVCopS yN5MWYiGhGFS8C0MCZ9eYlIDdsHx/lu8lEdLcYD1cvEYUAIGsdWM9yvScZaNrTej7wsq Z3renzkUKVQhKZtkf4JFnkuk6/TSeFHRZt2xJ4pKAq1YkWY9xoq1J0FEluqX2CRlmp5j N06Cta4zpIp6Ot9/qVWhW0Vp4t8bfDMm70d8+OTvGi7R98n2VdJYMqjjNSXtgNvuswA/ UNqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b="n6j/xBMh"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c24si379964pgj.407.2021.05.03.10.39.23; Mon, 03 May 2021 10:39:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b="n6j/xBMh"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231953AbhECRh3 (ORCPT + 99 others); Mon, 3 May 2021 13:37:29 -0400 Received: from linux.microsoft.com ([13.77.154.182]:54548 "EHLO linux.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231618AbhECRhV (ORCPT ); Mon, 3 May 2021 13:37:21 -0400 Received: from x64host.home (unknown [47.187.223.33]) by linux.microsoft.com (Postfix) with ESMTPSA id 0CEFF20B8016; Mon, 3 May 2021 10:36:26 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 0CEFF20B8016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1620063387; bh=bYCJMeYNqN5Rl7cucEEXyAKwSqecSENtHSd9RFOfCiw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=n6j/xBMhadoUBM8BC5SAejnw2WorVnwcXyM8I9FLKnihE+3sp1AzlpoKaNnOspqUJ StwncmZF/PXyvbXi0ZHShyESiDMjPTSHsmW7r9aD5340M4yFz6AiYf6DQPegeBU9a5 g/w6xnSg0NPRwqikcnZoeokiAmcNA1n44Dn6vup0= From: madvenka@linux.microsoft.com To: broonie@kernel.org, jpoimboe@redhat.com, mark.rutland@arm.com, jthierry@redhat.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, pasha.tatashin@soleen.com, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [RFC PATCH v3 4/4] arm64: Handle funtion graph tracer better in the unwinder Date: Mon, 3 May 2021 12:36:15 -0500 Message-Id: <20210503173615.21576-5-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210503173615.21576-1-madvenka@linux.microsoft.com> References: <65cf4dfbc439b010b50a0c46ec500432acde86d6> <20210503173615.21576-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Madhavan T. Venkataraman" The Function Graph Tracer modifies the return address of a traced function to a return trampoline (return_to_handler()) to gather tracing data on function return. When the unwinder encounters return_to_handler(), it calls ftrace_graph_get_ret_stack() to lookup the original return address in the return address stack. This lookup will succeed as long as the unwinder is invoked when the traced function is executing. However, when the traced function returns and control goes to return_to_handler(), this lookup will not succeed because: - the return address on the stack would not be return_to_handler. It would be return_to_handler+someoffset. To solve this, get the address range for return_to_handler() by looking up its symbol table entry and check if frame->pc falls in the range. This is also required for the unwinder to maintain the index into the return address stack correctly as it unwinds through Function Graph trace return trampolines. - the original return address will be popped off the return address stack at some point. From this point till the end of return_to_handler(), the lookup will not succeed. The stack trace is unreliable in that window. On arm64, each return address stack entry also stores the FP of the caller of the traced function. Compare the FP in the current frame with the entry that is looked up. If the FP matches, then, all is well. Else, it is in the window. mark the stack trace unreliable. Although it is possible to close the window mentioned above, it is not worth it. It is a tiny window. Signed-off-by: Madhavan T. Venkataraman --- arch/arm64/include/asm/stacktrace.h | 3 ++ arch/arm64/kernel/stacktrace.c | 60 ++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index f1eab6b029f7..e70a2a6451db 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -69,6 +69,7 @@ extern void walk_stackframe(struct task_struct *tsk, struct stackframe *frame, bool (*fn)(void *, unsigned long), void *data); extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, const char *loglvl); +extern void init_ranges(void); DECLARE_PER_CPU(unsigned long *, irq_stack_ptr); @@ -154,6 +155,8 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, static inline void start_backtrace(struct stackframe *frame, unsigned long fp, unsigned long pc) { + init_ranges(); + frame->fp = fp; frame->pc = pc; #ifdef CONFIG_FUNCTION_GRAPH_TRACER diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 33e174160f9b..7504aec79faa 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -26,6 +26,9 @@ struct code_range { struct code_range sym_code_ranges[] = { + /* unwindable ranges */ + { (unsigned long)return_to_handler, 0 }, + /* non-unwindable ranges */ { (unsigned long)__entry_text_start, (unsigned long)__entry_text_end }, @@ -48,6 +51,33 @@ struct code_range sym_code_ranges[] = { /* sentinel */ } }; +void init_ranges(void) +{ + static char sym[KSYM_NAME_LEN]; + static bool inited = false; + struct code_range *range; + unsigned long pc, size, offset; + + if (inited) + return; + + for (range = sym_code_ranges; range->start; range++) { + if (range->end) + continue; + + pc = (unsigned long)range->start; + if (kallsyms_lookup(pc, &size, &offset, NULL, sym)) { + range->start = pc - offset; + range->end = range->start + size; + } else { + /* Range will only include one instruction */ + range->start = pc; + range->end = pc + 4; + } + } + inited = true; +} + static struct code_range *lookup_range(unsigned long pc) { struct code_range *range; @@ -149,19 +179,29 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) #ifdef CONFIG_FUNCTION_GRAPH_TRACER if (tsk->ret_stack && - frame->pc == (unsigned long)return_to_handler) { + range->start == (unsigned long)return_to_handler) { struct ftrace_ret_stack *ret_stack; /* - * This is a case where function graph tracer has - * modified a return address (LR) in a stack frame - * to hook a function return. - * So replace it to an original value. + * Either the function graph tracer has modified a return + * address (LR) in a stack frame to the return trampoline. + * Or, the return trampoline itself is executing upon the + * return of a traced function. Lookup the original return + * address and replace frame->pc with it. + * + * However, the return trampoline pops the original return + * address off the return address stack at some point. So, + * there is a small window towards the end of the return + * trampoline where the lookup will fail. In that case, + * mark the stack trace as unreliable and proceed. */ - ret_stack = ftrace_graph_get_ret_stack(tsk, frame->graph++); - if (WARN_ON_ONCE(!ret_stack)) - return -EINVAL; - frame->pc = ret_stack->ret; - frame->pc = ptrauth_strip_insn_pac(frame->pc); + ret_stack = ftrace_graph_get_ret_stack(tsk, frame->graph); + if (!ret_stack || frame->fp != ret_stack->fp) { + frame->reliable = false; + } else { + frame->pc = ret_stack->ret; + frame->pc = ptrauth_strip_insn_pac(frame->pc); + frame->graph++; + } return 0; } #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ -- 2.25.1