Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2440468pxu; Sat, 28 Nov 2020 14:13:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJzaPWovdorboR6XteKfAf6sbZea4GW1m0pMORkShs2nsw+LsMGg4tu5KWFH8R+sCCR6wJ6c X-Received: by 2002:a50:c050:: with SMTP id u16mr14582627edd.177.1606601608562; Sat, 28 Nov 2020 14:13:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606601608; cv=none; d=google.com; s=arc-20160816; b=uvBJJYjTVWmjUhd39Xm6amPK4kULNm9/K9PyWBpqcu3sAXc81iOlVIAddTuAK8Fjej Gr6OonXdbStWyO+tSPLE6EYEPAaR1s7m54APBMlqzYNBRwzYqnU0YtCU72GXBAOzONYC 3mHiDeJlCN9DfW6QJorb/vkjSmUTMEOQQmYueCEbZytDfkKVFoLf0TSBtCjVeYx9glPn jrdDUDQDRNGkp7Td4Y896+x2ws0R7yt/CWVzkxpdeU2mdl8bwYEtUqF3yzz1zx3C+Zug 0kwzSvZtySZbDdZdDlrUIHapvlFb4TEHah6MqkHrNZ9YU7bAaZkMOzmtZTVxtkBaVl1Y izng== 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:cc:to:from; bh=72fOojdJL1aKA71js1lrzKyuqYWVnDfgdadMPty/eE4=; b=KI1GgUvdzskSnxRfyxUPMB3ndtwIb9hmNcaiInJW6U6yDyZ87CByGRV0XNyGL4Yj8S wKDCmDxSUBd2F07baIVUVjgKe7fK5vPpz8XIw/fqa4OSQwTTgpFuaN1bIlEJFsTjLOys HfAMqQ4tFlKFYYAB8HC2rmDcI0BhmphMM2UirVZT0bQnwEEp0OtwbwOsd4ZArk0p3ajr 6dqwy4txvRrHuH51ZIVjsuinsdBtvGzSSE0LpGs1qTWtEDGGvjEh9AAJ6h8jXHhvOAR5 0D1MEGIti3OKFUhr6CtESeYsJ9uMYQVO2wWcWWjwIAAGMiywjnj9wrl30qaMqhKTstJz oP/w== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q3si7797147edr.472.2020.11.28.14.13.06; Sat, 28 Nov 2020 14:13:28 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389586AbgK1VuD (ORCPT + 99 others); Sat, 28 Nov 2020 16:50:03 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:9067 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728452AbgK1SFo (ORCPT ); Sat, 28 Nov 2020 13:05:44 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4CjhC42BVqzLw1Z; Sat, 28 Nov 2020 14:18:40 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.487.0; Sat, 28 Nov 2020 14:19:04 +0800 From: Chen Huang To: CC: , , , , , , Subject: [PATCH v2] riscv: stacktrace: fix stackframe without ra on the top Date: Sat, 28 Nov 2020 06:22:42 +0000 Message-ID: <20201128062242.825448-1-chenhuang5@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <22dbe32a-0757-97ad-8885-0f2ba55bb743@huawei.com> References: <22dbe32a-0757-97ad-8885-0f2ba55bb743@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.112.125] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a function doesn't have a callee, then it will not push ra into the stack, such as lkdtm_BUG() function: addi sp,sp,-16 sd s0,8(sp) addi s0,sp,16 ebreak The struct stackframe use {fp,ra} to get information from stack, if walk_stackframe() with pr_regs, we will obtain wrong value and bad stacktrace, [] lkdtm_BUG+0x6/0x8 ---[ end trace 18da3fbdf08e25d5 ]--- Correct the next fp and pc, after that, full stacktrace shown as expects, [] lkdtm_BUG+0x6/0x8 [] lkdtm_do_action+0x14/0x1c [] direct_entry+0xc0/0x10a [] full_proxy_write+0x42/0x6a [] vfs_write+0x7e/0x214 [] ksys_write+0x98/0xc0 [] sys_write+0xe/0x16 [] ret_from_syscall+0x0/0x2 ---[ end trace 61917f3d9a9fadcd ]--- Signed-off-by: Chen Huang Signed-off-by: Kefeng Wang --- arch/riscv/kernel/stacktrace.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c index 595342910c3f..3842fac26e85 100644 --- a/arch/riscv/kernel/stacktrace.c +++ b/arch/riscv/kernel/stacktrace.c @@ -57,9 +57,14 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, /* Unwind stack frame */ frame = (struct stackframe *)fp - 1; sp = fp; - fp = frame->fp; - pc = ftrace_graph_ret_addr(current, NULL, frame->ra, - (unsigned long *)(fp - 8)); + if (regs && (pc == regs->epc) && (frame->fp & 0x7)) { + fp = frame->ra; + pc = regs->ra; + } else { + fp = frame->fp; + pc = ftrace_graph_ret_addr(current, NULL, frame->ra, + (unsigned long *)(fp - 8)); + } } } -- 2.17.1