Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp220691pxb; Fri, 15 Oct 2021 04:18:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPV+TtklwUHPf27thP7WRrPgq6+svWXLyzdEdoWDuUniRXuUjbSMpHfOd5oaQJoWQJSKqn X-Received: by 2002:a17:902:ce8c:b0:13f:9678:230c with SMTP id f12-20020a170902ce8c00b0013f9678230cmr803076plg.62.1634296680830; Fri, 15 Oct 2021 04:18:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634296680; cv=none; d=google.com; s=arc-20160816; b=vf+f6t0CBxF2iUYT0FG50si7h6CNurISbw37aeqXncicbHHAYSd5K2TVmUmK6Jj3ri Yjp2BDQ0Gb35BvYXyWp2AJbMoYNezRry3BT9lOG2YIkxFj6NbcP099IFRTIlPHaC/Hf8 qQnfWmTb8x/ArW/lUOIGnJdtBRmhmN54+0xfnE+PjJA30dJOeFUo7OKtwzHSNSLgA2hd ZRWKtCn0W2haJe1Y1NVPAxm6YtYs1d7f61uHqF+/CP6cW5mGIFW21uCgCO/8SQ6RHMfy xl0+uLEkNcLNSWVvTTY/4++I+6CqC/1V7WwvrcDfdn9tQiPL9OKQ09wROUQiLJLSbHWf u9vQ== 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=57BcPNouzyU3TXGJbaeOWDPyH6v6sumOk7RFMlmMA60=; b=tKUA4k7hG0bFhDz5S7PiBtioXGI1jdpHavLKha53pAU4QJ0zzEIxSt0Koh2JqTnQMa nr39lZOmnWgggjKwpqasbLlshQdkJfkQJpE7HPHWJzqlvXfinResolQ2chJht92KqKW2 EmSFQ0P21od/Zd1nBshXuTXyi/Ovr0C7YBhkStW8gikuwyV+mnc9PyiYoGmYXNj2G49H nFBbdhcP9qi7+F1BWGRTFNbmBgpbFWIX5OXJMU2K0z/CbO++XKRGwJSyc6nUKLZWQNn2 PF3RIGLtktXfFfKluShtwT7q+cOhWk+rS6IKNRTql5KQYHqrznkcCAWLzOnKr86s4XKo S1Tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=N8UKReYy; 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 w124si3185961pfw.314.2021.10.15.04.17.47; Fri, 15 Oct 2021 04:18:00 -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=N8UKReYy; 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 S235292AbhJODBj (ORCPT + 99 others); Thu, 14 Oct 2021 23:01:39 -0400 Received: from linux.microsoft.com ([13.77.154.182]:58266 "EHLO linux.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235173AbhJODBS (ORCPT ); Thu, 14 Oct 2021 23:01:18 -0400 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id 053F320B9D22; Thu, 14 Oct 2021 19:59:11 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 053F320B9D22 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1634266752; bh=57BcPNouzyU3TXGJbaeOWDPyH6v6sumOk7RFMlmMA60=; h=From:To:Subject:Date:In-Reply-To:References:From; b=N8UKReYyrLkMZmQe4vdPcnOVNgOCBlITHGlMcOyzBfvwrLC7xOJg/jklvIxh6/2cT BFeAFinNihtRj9BWJ5X/QSQ8vCr8jx0e8soT5Fe1Du8Q6M/QK0Gt32k6psdlCrEKtb UGeWuOY01GTkUTs1mheBCAG0ARG6sIuiT7qUodJ4= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v10 09/11] arm64: Make the unwind loop in unwind() similar to other architectures Date: Thu, 14 Oct 2021 21:58:45 -0500 Message-Id: <20211015025847.17694-10-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211015025847.17694-1-madvenka@linux.microsoft.com> References: <20211015025847.17694-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" Change the loop in unwind() =========================== Change the unwind loop in unwind() to: unwind_start(&frame, fp, pc); while (unwind_continue(tsk, &frame, fn, data)) unwind_next(tsk, &frame); New function unwind_continue() ============================== Define a new function unwind_continue() that is used in the unwind loop to check for conditions that terminate a stack trace. The conditions checked are: - If the bottom of the stack has been reached, terminate. - If the consume_entry() function returns false, the caller of unwind has asked to terminate the stack trace. So, terminate. - If unwind_next() failed for some reason (like stack corruption), terminate. Do not return an error value from unwind_next() =============================================== We want to check for terminating conditions only in unwind_continue() from the unwinder loop. So, do not return an error value from unwind_next(). Simply set a flag in the stackframe and check the flag in unwind_continue(). Signed-off-by: Madhavan T. Venkataraman --- arch/arm64/include/asm/stacktrace.h | 3 ++ arch/arm64/kernel/stacktrace.c | 78 ++++++++++++++++++----------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index c239f357d779..ba2180c7d5cd 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -49,6 +49,8 @@ struct stack_info { * * @graph: When FUNCTION_GRAPH_TRACER is selected, holds the index of a * replacement lr value in the ftrace graph stack. + * + * @failed: Unwind failed. */ struct stackframe { unsigned long fp; @@ -59,6 +61,7 @@ struct stackframe { #ifdef CONFIG_FUNCTION_GRAPH_TRACER int graph; #endif + bool failed; }; extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index f4f3575f71fd..8e9e6f38c975 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -54,6 +54,7 @@ static void notrace unwind_start(struct stackframe *frame, unsigned long fp, bitmap_zero(frame->stacks_done, __NR_STACK_TYPES); frame->prev_fp = 0; frame->prev_type = STACK_TYPE_UNKNOWN; + frame->failed = false; } NOKPROBE_SYMBOL(unwind_start); @@ -65,24 +66,26 @@ NOKPROBE_SYMBOL(unwind_start); * records (e.g. a cycle), determined based on the location and fp value of A * and the location (but not the fp value) of B. */ -static int notrace unwind_next(struct task_struct *tsk, - struct stackframe *frame) +static void notrace unwind_next(struct task_struct *tsk, + struct stackframe *frame) { unsigned long fp = frame->fp; struct stack_info info; - /* Final frame; nothing to unwind */ - if (fp == (unsigned long)task_pt_regs(tsk)->stackframe) - return -ENOENT; - - if (fp & 0x7) - return -EINVAL; + if (fp & 0x7) { + frame->failed = true; + return; + } - if (!on_accessible_stack(tsk, fp, 16, &info)) - return -EINVAL; + if (!on_accessible_stack(tsk, fp, 16, &info)) { + frame->failed = true; + return; + } - if (test_bit(info.type, frame->stacks_done)) - return -EINVAL; + if (test_bit(info.type, frame->stacks_done)) { + frame->failed = true; + return; + } /* * As stacks grow downward, any valid record on the same stack must be @@ -98,8 +101,10 @@ static int notrace unwind_next(struct task_struct *tsk, * stack. */ if (info.type == frame->prev_type) { - if (fp <= frame->prev_fp) - return -EINVAL; + if (fp <= frame->prev_fp) { + frame->failed = true; + return; + } } else { set_bit(frame->prev_type, frame->stacks_done); } @@ -124,19 +129,44 @@ static int notrace unwind_next(struct task_struct *tsk, * So replace it to an original value. */ ret_stack = ftrace_graph_get_ret_stack(tsk, frame->graph++); - if (WARN_ON_ONCE(!ret_stack)) - return -EINVAL; + if (WARN_ON_ONCE(!ret_stack)) { + frame->failed = true; + return; + } frame->pc = ret_stack->ret; } #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ frame->pc = ptrauth_strip_insn_pac(frame->pc); - - return 0; } NOKPROBE_SYMBOL(unwind_next); +static bool notrace unwind_continue(struct task_struct *task, + struct stackframe *frame, + stack_trace_consume_fn consume_entry, + void *cookie) +{ + if (frame->failed) { + /* PC is suspect. Cannot consume it. */ + return false; + } + + if (!consume_entry(cookie, frame->pc)) { + /* Caller terminated the unwind. */ + frame->failed = true; + return false; + } + + if (frame->fp == (unsigned long)task_pt_regs(task)->stackframe) { + /* Final frame; nothing to unwind */ + return false; + } + return true; +} + +NOKPROBE_SYMBOL(unwind_continue); + static void notrace unwind(struct task_struct *tsk, unsigned long fp, unsigned long pc, bool (*fn)(void *, unsigned long), @@ -145,16 +175,8 @@ static void notrace unwind(struct task_struct *tsk, struct stackframe frame; unwind_start(&frame, fp, pc); - - while (1) { - int ret; - - if (!fn(data, frame.pc)) - break; - ret = unwind_next(tsk, &frame); - if (ret < 0) - break; - } + while (unwind_continue(tsk, &frame, fn, data)) + unwind_next(tsk, &frame); } NOKPROBE_SYMBOL(unwind); -- 2.25.1