Message ID | 20231205085959.32177-1-zong.li@sifive.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3296501vqy; Tue, 5 Dec 2023 01:00:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IFi5QW1fT6IO7Cw/VqIhve0Vlh4Venwe+QjZtiFC0e+XVTrm+JvgcuuerL3WnnkxE1PysuL X-Received: by 2002:a17:902:d305:b0:1d0:700b:3f79 with SMTP id b5-20020a170902d30500b001d0700b3f79mr1052700plc.51.1701766823776; Tue, 05 Dec 2023 01:00:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701766823; cv=none; d=google.com; s=arc-20160816; b=OSz1MY27Ezk46uNV41MFyQzvtwty/UJiS+OiinKDXHlgWKKm5zRSSPxnDqDIxJmiX4 fx2BunALUxtXZuDbzNYhLlKDz8EduWSdlagayRqZVmF9cVzLGG7iUA1XSOOO3vWaJHqA zJmaxf05O0HnuPNFBimz/pjZofh1iHvUFF/vWhCeljL76biRHGAf7JfxsrpyzyLLOXWm ytYTvzWz4MLuTcdiPt516bnyKgGGKGMAr3tszt1JZbtozFG+k8AOpdWas5Dh9abEB1ve pyjeAIivmSELqXUXgmnVe2AR/7x/vyRccdI10Fe3pVhnqfIaH+lW4le7GcWjKCnu7zzy BP/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=Wi6K2Brpiu9Fka3m9z0bXgXfpslNvz1u3mb0b6dJW6o=; fh=oYEYdJA79gR45iwIb16CMKPTkgEmpKJXMMpr8SpVOVg=; b=YRzrfLGWh/R9EIs2iyxAutzs2LJ+ZnhyfGQKcFMnBv2WvENgkgC2Fpf59UxvM97/4X CTj6FvjefKNgloisDAVV3+wlDGjqD6cW4WkyCoUuIHLrp5qyz/X/I6zwACJdHlj1iPJx 4D7um0JiLmUG74r19f/AFWDbMYalSESStS/vnwh3DHNvYcnLvrgntTyzydgu8mpzBnR0 lPUnRXnzdhlbnyndXpI4QLCthuWe0VdhmlvHNTcsod+X/gUGWvE8m7w1iGVFe6ZKjmrQ Uq3Bv8pGduPMkkKWW2u8Zchl+/S5qDMYOkO01u8dTcNo44gXPHWKkiC+yqRFmast7zTX zSiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=m544HOAu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id l10-20020a170903244a00b001cf570ea116si9827063pls.353.2023.12.05.01.00.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 01:00:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=m544HOAu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5FEB0801F8B9; Tue, 5 Dec 2023 01:00:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234920AbjLEJAE (ORCPT <rfc822;chrisfriedt@gmail.com> + 99 others); Tue, 5 Dec 2023 04:00:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234860AbjLEJAB (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 5 Dec 2023 04:00:01 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57401184 for <linux-kernel@vger.kernel.org>; Tue, 5 Dec 2023 01:00:05 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1d04c097e34so29954765ad.0 for <linux-kernel@vger.kernel.org>; Tue, 05 Dec 2023 01:00:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1701766804; x=1702371604; darn=vger.kernel.org; h=message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Wi6K2Brpiu9Fka3m9z0bXgXfpslNvz1u3mb0b6dJW6o=; b=m544HOAuNFcIu+ul2XhWjOVdgMp4Ozi4RmvgDecZb0y12mlA8Vek9manj1OR2nm44J GYDQyGHXvEUANx3ha22Bii8l0rBDL9hr9n3vlBmiH6pSW/Uu+JmRP4M0e6aoI+n3jtio q8p4J1/pAQlbtXgLTEXkwaLo+Z5/zCxo5aOtyhAXKZnPOt97RsXQ9dTSn831Sg4WzRlg WrPBlKpLwM2rqQaSV1NbtwhiACufxIgAU/N1hMQK02liHQxVBii41Dw4wDQl99Fdtj6z iANx+dB52azxEIbzjnVE+XH5kdT7lqLRhlHr1U/k2XQmnVFBMEH+cX9XbJWOTuG6rQiy QsKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701766804; x=1702371604; h=message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Wi6K2Brpiu9Fka3m9z0bXgXfpslNvz1u3mb0b6dJW6o=; b=G26V+ek9L5wUXUWwiBtrhAfFOUnPT8a24qJpB2iDwh8RDhhApPzadS9Rqdr3xBW2ve bpejDk9NyKhGzi6oUGTe//Wy47Dip9jy0WOoJmQTI4q6/Uh5cYQY1DMYQ5PQmdBKjpHo kzY4E1bpb9hcLZRq+2/qwS8YMKDNfGMZWtvRq5XkOE9gYtY8loeeXJACl8oqv6a8X/48 B4yzurNPoO8t0BKZN3A7L3f+CFI12bQoSNav5YmJYfjr1NTG0K/UoWAE9ZRv0P2XGlSU qK6oEjAynLrkv9Mt24vjjS5IgANmmBrnBuTpyM8kVK8DhzgHu8HB2JZEfYjpk0wIQhF7 L6Uw== X-Gm-Message-State: AOJu0YzszvdBryRmPlDdDx8pGLLnYvOtrsdlqV8MSOOLI4wFOFOI0ohS cuFEqIP0ooyyoTxCT+cFPX01YA== X-Received: by 2002:a17:902:ecca:b0:1cf:f868:5b8c with SMTP id a10-20020a170902ecca00b001cff8685b8cmr1228712plh.8.1701766804259; Tue, 05 Dec 2023 01:00:04 -0800 (PST) Received: from hsinchu26.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id q1-20020a17090311c100b001cfc9c926b7sm6134629plh.75.2023.12.05.01.00.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 01:00:04 -0800 (PST) From: Zong Li <zong.li@sifive.com> To: palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li <zong.li@sifive.com> Subject: [PATCH] riscv: add CALLER_ADDRx support Date: Tue, 5 Dec 2023 08:59:59 +0000 Message-Id: <20231205085959.32177-1-zong.li@sifive.com> X-Mailer: git-send-email 2.17.1 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 05 Dec 2023 01:00:20 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784431848684599306 X-GMAIL-MSGID: 1784431848684599306 |
Series |
riscv: add CALLER_ADDRx support
|
|
Commit Message
Zong Li
Dec. 5, 2023, 8:59 a.m. UTC
CALLER_ADDRx returns caller's address at specified level, they are used
for several tracers. These macros eventually use
__builtin_return_address(n) to get the caller's address if arch doesn't
define their own implementation.
In RISC-V, __builtin_return_address(n) only works when n == 0, we need
to walk the stack frame to get the caller's address at specified level.
data.level started from 'level + 3' due to the call flow of getting
caller's address in RISC-V implementation. If we don't have additional
three iteration, the level is corresponding to follows:
callsite -> return_address -> arch_stack_walk -> walk_stackframe
| | | |
level 3 level 2 level 1 level 0
Signed-off-by: Zong Li <zong.li@sifive.com>
---
arch/riscv/include/asm/ftrace.h | 5 ++++
arch/riscv/kernel/Makefile | 2 ++
arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++
3 files changed, 55 insertions(+)
create mode 100644 arch/riscv/kernel/return_address.c
Comments
On Tue, Dec 5, 2023 at 5:00 PM Zong Li <zong.li@sifive.com> wrote: > > CALLER_ADDRx returns caller's address at specified level, they are used > for several tracers. These macros eventually use > __builtin_return_address(n) to get the caller's address if arch doesn't > define their own implementation. > > In RISC-V, __builtin_return_address(n) only works when n == 0, we need > to walk the stack frame to get the caller's address at specified level. > > data.level started from 'level + 3' due to the call flow of getting > caller's address in RISC-V implementation. If we don't have additional > three iteration, the level is corresponding to follows: > > callsite -> return_address -> arch_stack_walk -> walk_stackframe > | | | | > level 3 level 2 level 1 level 0 > > Signed-off-by: Zong Li <zong.li@sifive.com> > --- > arch/riscv/include/asm/ftrace.h | 5 ++++ > arch/riscv/kernel/Makefile | 2 ++ > arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ > 3 files changed, 55 insertions(+) > create mode 100644 arch/riscv/kernel/return_address.c > > diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h > index 2b2f5df7ef2c..42777f91a9c5 100644 > --- a/arch/riscv/include/asm/ftrace.h > +++ b/arch/riscv/include/asm/ftrace.h > @@ -25,6 +25,11 @@ > > #define ARCH_SUPPORTS_FTRACE_OPS 1 > #ifndef __ASSEMBLY__ > + > +extern void *return_address(unsigned int level); > + > +#define ftrace_return_address(n) return_address(n) > + > void MCOUNT_NAME(void); > static inline unsigned long ftrace_call_adjust(unsigned long addr) > { > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > index fee22a3d1b53..40d054939ae2 100644 > --- a/arch/riscv/kernel/Makefile > +++ b/arch/riscv/kernel/Makefile > @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE > CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) > CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) > CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) > +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) > endif > CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) > CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) > @@ -46,6 +47,7 @@ obj-y += irq.o > obj-y += process.o > obj-y += ptrace.o > obj-y += reset.o > +obj-y += return_address.o > obj-y += setup.o > obj-y += signal.o > obj-y += syscall_table.o > diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c > new file mode 100644 > index 000000000000..c2008d4aa6e5 > --- /dev/null > +++ b/arch/riscv/kernel/return_address.c > @@ -0,0 +1,48 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * This code come from arch/arm64/kernel/return_address.c > + * > + * Copyright (C) 2023 SiFive. > + */ > + > +#include <linux/export.h> > +#include <linux/kprobes.h> > +#include <linux/stacktrace.h> > + > +struct return_address_data { > + unsigned int level; > + void *addr; > +}; > + > +static bool save_return_addr(void *d, unsigned long pc) > +{ > + struct return_address_data *data = d; > + > + if (!data->level) { > + data->addr = (void *)pc; > + return false; > + } > + > + --data->level; > + > + return true; > +} > +NOKPROBE_SYMBOL(save_return_addr); > + > +void *return_address(unsigned int level) > +{ > + struct return_address_data data; > + > + data.level = level + 3; > + data.addr = NULL; > + > + arch_stack_walk(save_return_addr, &data, current, NULL); > + > + if (!data.level) > + return data.addr; > + else > + return NULL; > + > +} > +EXPORT_SYMBOL_GPL(return_address); > +NOKPROBE_SYMBOL(return_address); > -- > 2.17.1 > Hi Palmer and all, I was wondering whether this patch is good for everyone? Thanks
On Fri, Dec 29, 2023 at 2:34 PM Zong Li <zong.li@sifive.com> wrote: > > On Tue, Dec 5, 2023 at 5:00 PM Zong Li <zong.li@sifive.com> wrote: > > > > CALLER_ADDRx returns caller's address at specified level, they are used > > for several tracers. These macros eventually use > > __builtin_return_address(n) to get the caller's address if arch doesn't > > define their own implementation. > > > > In RISC-V, __builtin_return_address(n) only works when n == 0, we need > > to walk the stack frame to get the caller's address at specified level. > > > > data.level started from 'level + 3' due to the call flow of getting > > caller's address in RISC-V implementation. If we don't have additional > > three iteration, the level is corresponding to follows: > > > > callsite -> return_address -> arch_stack_walk -> walk_stackframe > > | | | | > > level 3 level 2 level 1 level 0 > > > > Signed-off-by: Zong Li <zong.li@sifive.com> > > --- > > arch/riscv/include/asm/ftrace.h | 5 ++++ > > arch/riscv/kernel/Makefile | 2 ++ > > arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ > > 3 files changed, 55 insertions(+) > > create mode 100644 arch/riscv/kernel/return_address.c > > > > diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h > > index 2b2f5df7ef2c..42777f91a9c5 100644 > > --- a/arch/riscv/include/asm/ftrace.h > > +++ b/arch/riscv/include/asm/ftrace.h > > @@ -25,6 +25,11 @@ > > > > #define ARCH_SUPPORTS_FTRACE_OPS 1 > > #ifndef __ASSEMBLY__ > > + > > +extern void *return_address(unsigned int level); > > + > > +#define ftrace_return_address(n) return_address(n) > > + > > void MCOUNT_NAME(void); > > static inline unsigned long ftrace_call_adjust(unsigned long addr) > > { > > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > > index fee22a3d1b53..40d054939ae2 100644 > > --- a/arch/riscv/kernel/Makefile > > +++ b/arch/riscv/kernel/Makefile > > @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE > > CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) > > CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) > > CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) > > +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) > > endif > > CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) > > CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) > > @@ -46,6 +47,7 @@ obj-y += irq.o > > obj-y += process.o > > obj-y += ptrace.o > > obj-y += reset.o > > +obj-y += return_address.o > > obj-y += setup.o > > obj-y += signal.o > > obj-y += syscall_table.o > > diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c > > new file mode 100644 > > index 000000000000..c2008d4aa6e5 > > --- /dev/null > > +++ b/arch/riscv/kernel/return_address.c > > @@ -0,0 +1,48 @@ > > +// SPDX-License-Identifier: GPL-2.0-only > > +/* > > + * This code come from arch/arm64/kernel/return_address.c > > + * > > + * Copyright (C) 2023 SiFive. > > + */ > > + > > +#include <linux/export.h> > > +#include <linux/kprobes.h> > > +#include <linux/stacktrace.h> > > + > > +struct return_address_data { > > + unsigned int level; > > + void *addr; > > +}; > > + > > +static bool save_return_addr(void *d, unsigned long pc) > > +{ > > + struct return_address_data *data = d; > > + > > + if (!data->level) { > > + data->addr = (void *)pc; > > + return false; > > + } > > + > > + --data->level; > > + > > + return true; > > +} > > +NOKPROBE_SYMBOL(save_return_addr); > > + > > +void *return_address(unsigned int level) > > +{ > > + struct return_address_data data; > > + > > + data.level = level + 3; > > + data.addr = NULL; > > + > > + arch_stack_walk(save_return_addr, &data, current, NULL); > > + > > + if (!data.level) > > + return data.addr; > > + else > > + return NULL; > > + > > +} > > +EXPORT_SYMBOL_GPL(return_address); > > +NOKPROBE_SYMBOL(return_address); > > -- > > 2.17.1 > > > > Hi Palmer and all, > I was wondering whether this patch is good for everyone? Thanks Hi Palmer, Is there any chance to include this patch in 6.8-rc1? Thanks
On Wed, Jan 10, 2024 at 11:33 AM Zong Li <zong.li@sifive.com> wrote: > > On Fri, Dec 29, 2023 at 2:34 PM Zong Li <zong.li@sifive.com> wrote: > > > > On Tue, Dec 5, 2023 at 5:00 PM Zong Li <zong.li@sifive.com> wrote: > > > > > > CALLER_ADDRx returns caller's address at specified level, they are used > > > for several tracers. These macros eventually use > > > __builtin_return_address(n) to get the caller's address if arch doesn't > > > define their own implementation. > > > > > > In RISC-V, __builtin_return_address(n) only works when n == 0, we need > > > to walk the stack frame to get the caller's address at specified level. > > > > > > data.level started from 'level + 3' due to the call flow of getting > > > caller's address in RISC-V implementation. If we don't have additional > > > three iteration, the level is corresponding to follows: > > > > > > callsite -> return_address -> arch_stack_walk -> walk_stackframe > > > | | | | > > > level 3 level 2 level 1 level 0 > > > > > > Signed-off-by: Zong Li <zong.li@sifive.com> > > > --- > > > arch/riscv/include/asm/ftrace.h | 5 ++++ > > > arch/riscv/kernel/Makefile | 2 ++ > > > arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ > > > 3 files changed, 55 insertions(+) > > > create mode 100644 arch/riscv/kernel/return_address.c > > > > > > diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h > > > index 2b2f5df7ef2c..42777f91a9c5 100644 > > > --- a/arch/riscv/include/asm/ftrace.h > > > +++ b/arch/riscv/include/asm/ftrace.h > > > @@ -25,6 +25,11 @@ > > > > > > #define ARCH_SUPPORTS_FTRACE_OPS 1 > > > #ifndef __ASSEMBLY__ > > > + > > > +extern void *return_address(unsigned int level); > > > + > > > +#define ftrace_return_address(n) return_address(n) > > > + > > > void MCOUNT_NAME(void); > > > static inline unsigned long ftrace_call_adjust(unsigned long addr) > > > { > > > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > > > index fee22a3d1b53..40d054939ae2 100644 > > > --- a/arch/riscv/kernel/Makefile > > > +++ b/arch/riscv/kernel/Makefile > > > @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE > > > CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) > > > CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) > > > CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) > > > +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) > > > endif > > > CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) > > > CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) > > > @@ -46,6 +47,7 @@ obj-y += irq.o > > > obj-y += process.o > > > obj-y += ptrace.o > > > obj-y += reset.o > > > +obj-y += return_address.o > > > obj-y += setup.o > > > obj-y += signal.o > > > obj-y += syscall_table.o > > > diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c > > > new file mode 100644 > > > index 000000000000..c2008d4aa6e5 > > > --- /dev/null > > > +++ b/arch/riscv/kernel/return_address.c > > > @@ -0,0 +1,48 @@ > > > +// SPDX-License-Identifier: GPL-2.0-only > > > +/* > > > + * This code come from arch/arm64/kernel/return_address.c > > > + * > > > + * Copyright (C) 2023 SiFive. > > > + */ > > > + > > > +#include <linux/export.h> > > > +#include <linux/kprobes.h> > > > +#include <linux/stacktrace.h> > > > + > > > +struct return_address_data { > > > + unsigned int level; > > > + void *addr; > > > +}; > > > + > > > +static bool save_return_addr(void *d, unsigned long pc) > > > +{ > > > + struct return_address_data *data = d; > > > + > > > + if (!data->level) { > > > + data->addr = (void *)pc; > > > + return false; > > > + } > > > + > > > + --data->level; > > > + > > > + return true; > > > +} > > > +NOKPROBE_SYMBOL(save_return_addr); > > > + > > > +void *return_address(unsigned int level) > > > +{ > > > + struct return_address_data data; > > > + > > > + data.level = level + 3; > > > + data.addr = NULL; > > > + > > > + arch_stack_walk(save_return_addr, &data, current, NULL); > > > + > > > + if (!data.level) > > > + return data.addr; > > > + else > > > + return NULL; > > > + > > > +} > > > +EXPORT_SYMBOL_GPL(return_address); > > > +NOKPROBE_SYMBOL(return_address); > > > -- > > > 2.17.1 > > > > > > > Hi Palmer and all, > > I was wondering whether this patch is good for everyone? Thanks > > Hi Palmer, > Is there any chance to include this patch in 6.8-rc1? Thanks Hi Palmer, I'm not sure if this patch is a feature or bug fix, would you consider it for 6.8-rcX? Thanks
On Thu, Jan 18, 2024 at 8:50 AM Zong Li <zong.li@sifive.com> wrote: > > On Wed, Jan 10, 2024 at 11:33 AM Zong Li <zong.li@sifive.com> wrote: > > > > On Fri, Dec 29, 2023 at 2:34 PM Zong Li <zong.li@sifive.com> wrote: > > > > > > On Tue, Dec 5, 2023 at 5:00 PM Zong Li <zong.li@sifive.com> wrote: > > > > > > > > CALLER_ADDRx returns caller's address at specified level, they are used > > > > for several tracers. These macros eventually use > > > > __builtin_return_address(n) to get the caller's address if arch doesn't > > > > define their own implementation. > > > > > > > > In RISC-V, __builtin_return_address(n) only works when n == 0, we need > > > > to walk the stack frame to get the caller's address at specified level. > > > > > > > > data.level started from 'level + 3' due to the call flow of getting > > > > caller's address in RISC-V implementation. If we don't have additional > > > > three iteration, the level is corresponding to follows: > > > > > > > > callsite -> return_address -> arch_stack_walk -> walk_stackframe > > > > | | | | > > > > level 3 level 2 level 1 level 0 > > > > > > > > Signed-off-by: Zong Li <zong.li@sifive.com> > > > > --- > > > > arch/riscv/include/asm/ftrace.h | 5 ++++ > > > > arch/riscv/kernel/Makefile | 2 ++ > > > > arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ > > > > 3 files changed, 55 insertions(+) > > > > create mode 100644 arch/riscv/kernel/return_address.c > > > > > > > > diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h > > > > index 2b2f5df7ef2c..42777f91a9c5 100644 > > > > --- a/arch/riscv/include/asm/ftrace.h > > > > +++ b/arch/riscv/include/asm/ftrace.h > > > > @@ -25,6 +25,11 @@ > > > > > > > > #define ARCH_SUPPORTS_FTRACE_OPS 1 > > > > #ifndef __ASSEMBLY__ > > > > + > > > > +extern void *return_address(unsigned int level); > > > > + > > > > +#define ftrace_return_address(n) return_address(n) > > > > + > > > > void MCOUNT_NAME(void); > > > > static inline unsigned long ftrace_call_adjust(unsigned long addr) > > > > { > > > > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > > > > index fee22a3d1b53..40d054939ae2 100644 > > > > --- a/arch/riscv/kernel/Makefile > > > > +++ b/arch/riscv/kernel/Makefile > > > > @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE > > > > CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) > > > > CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) > > > > CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) > > > > +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) > > > > endif > > > > CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) > > > > CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) > > > > @@ -46,6 +47,7 @@ obj-y += irq.o > > > > obj-y += process.o > > > > obj-y += ptrace.o > > > > obj-y += reset.o > > > > +obj-y += return_address.o > > > > obj-y += setup.o > > > > obj-y += signal.o > > > > obj-y += syscall_table.o > > > > diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c > > > > new file mode 100644 > > > > index 000000000000..c2008d4aa6e5 > > > > --- /dev/null > > > > +++ b/arch/riscv/kernel/return_address.c > > > > @@ -0,0 +1,48 @@ > > > > +// SPDX-License-Identifier: GPL-2.0-only > > > > +/* > > > > + * This code come from arch/arm64/kernel/return_address.c > > > > + * > > > > + * Copyright (C) 2023 SiFive. > > > > + */ > > > > + > > > > +#include <linux/export.h> > > > > +#include <linux/kprobes.h> > > > > +#include <linux/stacktrace.h> > > > > + > > > > +struct return_address_data { > > > > + unsigned int level; > > > > + void *addr; > > > > +}; > > > > + > > > > +static bool save_return_addr(void *d, unsigned long pc) > > > > +{ > > > > + struct return_address_data *data = d; > > > > + > > > > + if (!data->level) { > > > > + data->addr = (void *)pc; > > > > + return false; > > > > + } > > > > + > > > > + --data->level; > > > > + > > > > + return true; > > > > +} > > > > +NOKPROBE_SYMBOL(save_return_addr); > > > > + > > > > +void *return_address(unsigned int level) > > > > +{ > > > > + struct return_address_data data; > > > > + > > > > + data.level = level + 3; > > > > + data.addr = NULL; > > > > + > > > > + arch_stack_walk(save_return_addr, &data, current, NULL); > > > > + > > > > + if (!data.level) > > > > + return data.addr; > > > > + else > > > > + return NULL; > > > > + > > > > +} > > > > +EXPORT_SYMBOL_GPL(return_address); > > > > +NOKPROBE_SYMBOL(return_address); > > > > -- > > > > 2.17.1 > > > > > > > > > > Hi Palmer and all, > > > I was wondering whether this patch is good for everyone? Thanks > > > > Hi Palmer, > > Is there any chance to include this patch in 6.8-rc1? Thanks > > Hi Palmer, > I'm not sure if this patch is a feature or bug fix, would you consider > it for 6.8-rcX? Thanks Hi Palmer, Sorry for pinging you again. I'm not sure if you saw this patch. The irqsoff and wakeup tracer will use CALLER_ADDR1 and CALLER_ADDR2 to obtain the caller's return address, but we are currently encountering an issue in the RISC-V port where the wrong caller is identified. I guess you can easily reproduce the situation using ftrace. Could you share your thoughts on this when you have the time to take a look? Thanks
Hi Zong, On 31/01/2024 15:24, Zong Li wrote: > On Thu, Jan 18, 2024 at 8:50 AM Zong Li <zong.li@sifive.com> wrote: >> On Wed, Jan 10, 2024 at 11:33 AM Zong Li <zong.li@sifive.com> wrote: >>> On Fri, Dec 29, 2023 at 2:34 PM Zong Li <zong.li@sifive.com> wrote: >>>> On Tue, Dec 5, 2023 at 5:00 PM Zong Li <zong.li@sifive.com> wrote: >>>>> CALLER_ADDRx returns caller's address at specified level, they are used >>>>> for several tracers. These macros eventually use >>>>> __builtin_return_address(n) to get the caller's address if arch doesn't >>>>> define their own implementation. >>>>> >>>>> In RISC-V, __builtin_return_address(n) only works when n == 0, we need >>>>> to walk the stack frame to get the caller's address at specified level. >>>>> >>>>> data.level started from 'level + 3' due to the call flow of getting >>>>> caller's address in RISC-V implementation. If we don't have additional >>>>> three iteration, the level is corresponding to follows: >>>>> >>>>> callsite -> return_address -> arch_stack_walk -> walk_stackframe >>>>> | | | | >>>>> level 3 level 2 level 1 level 0 >>>>> >>>>> Signed-off-by: Zong Li <zong.li@sifive.com> >>>>> --- >>>>> arch/riscv/include/asm/ftrace.h | 5 ++++ >>>>> arch/riscv/kernel/Makefile | 2 ++ >>>>> arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ >>>>> 3 files changed, 55 insertions(+) >>>>> create mode 100644 arch/riscv/kernel/return_address.c >>>>> >>>>> diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h >>>>> index 2b2f5df7ef2c..42777f91a9c5 100644 >>>>> --- a/arch/riscv/include/asm/ftrace.h >>>>> +++ b/arch/riscv/include/asm/ftrace.h >>>>> @@ -25,6 +25,11 @@ >>>>> >>>>> #define ARCH_SUPPORTS_FTRACE_OPS 1 >>>>> #ifndef __ASSEMBLY__ >>>>> + >>>>> +extern void *return_address(unsigned int level); >>>>> + >>>>> +#define ftrace_return_address(n) return_address(n) >>>>> + >>>>> void MCOUNT_NAME(void); >>>>> static inline unsigned long ftrace_call_adjust(unsigned long addr) >>>>> { >>>>> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile >>>>> index fee22a3d1b53..40d054939ae2 100644 >>>>> --- a/arch/riscv/kernel/Makefile >>>>> +++ b/arch/riscv/kernel/Makefile >>>>> @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE >>>>> CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) >>>>> CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) >>>>> CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) >>>>> +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) >>>>> endif >>>>> CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) >>>>> CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) >>>>> @@ -46,6 +47,7 @@ obj-y += irq.o >>>>> obj-y += process.o >>>>> obj-y += ptrace.o >>>>> obj-y += reset.o >>>>> +obj-y += return_address.o >>>>> obj-y += setup.o >>>>> obj-y += signal.o >>>>> obj-y += syscall_table.o >>>>> diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c >>>>> new file mode 100644 >>>>> index 000000000000..c2008d4aa6e5 >>>>> --- /dev/null >>>>> +++ b/arch/riscv/kernel/return_address.c >>>>> @@ -0,0 +1,48 @@ >>>>> +// SPDX-License-Identifier: GPL-2.0-only >>>>> +/* >>>>> + * This code come from arch/arm64/kernel/return_address.c >>>>> + * >>>>> + * Copyright (C) 2023 SiFive. >>>>> + */ >>>>> + >>>>> +#include <linux/export.h> >>>>> +#include <linux/kprobes.h> >>>>> +#include <linux/stacktrace.h> >>>>> + >>>>> +struct return_address_data { >>>>> + unsigned int level; >>>>> + void *addr; >>>>> +}; >>>>> + >>>>> +static bool save_return_addr(void *d, unsigned long pc) >>>>> +{ >>>>> + struct return_address_data *data = d; >>>>> + >>>>> + if (!data->level) { >>>>> + data->addr = (void *)pc; >>>>> + return false; >>>>> + } >>>>> + >>>>> + --data->level; >>>>> + >>>>> + return true; >>>>> +} >>>>> +NOKPROBE_SYMBOL(save_return_addr); >>>>> + >>>>> +void *return_address(unsigned int level) >>>>> +{ >>>>> + struct return_address_data data; >>>>> + >>>>> + data.level = level + 3; >>>>> + data.addr = NULL; >>>>> + >>>>> + arch_stack_walk(save_return_addr, &data, current, NULL); >>>>> + >>>>> + if (!data.level) >>>>> + return data.addr; >>>>> + else >>>>> + return NULL; >>>>> + >>>>> +} >>>>> +EXPORT_SYMBOL_GPL(return_address); >>>>> +NOKPROBE_SYMBOL(return_address); >>>>> -- >>>>> 2.17.1 >>>>> >>>> Hi Palmer and all, >>>> I was wondering whether this patch is good for everyone? Thanks >>> Hi Palmer, >>> Is there any chance to include this patch in 6.8-rc1? Thanks >> Hi Palmer, >> I'm not sure if this patch is a feature or bug fix, would you consider >> it for 6.8-rcX? Thanks > Hi Palmer, > Sorry for pinging you again. I'm not sure if you saw this patch. The > irqsoff and wakeup tracer will use CALLER_ADDR1 and CALLER_ADDR2 to > obtain the caller's return address, but we are currently encountering > an issue in the RISC-V port where the wrong caller is identified. I > guess you can easily reproduce the situation using ftrace. Could you > share your thoughts on this when you have the time to take a look? > Thanks I'm not Palmer but I'll take a look at your patch today :) Thanks, Alex > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv
On Wed, Jan 31, 2024 at 10:46 PM Alexandre Ghiti <alex@ghiti.fr> wrote: > > Hi Zong, > > On 31/01/2024 15:24, Zong Li wrote: > > On Thu, Jan 18, 2024 at 8:50 AM Zong Li <zong.li@sifive.com> wrote: > >> On Wed, Jan 10, 2024 at 11:33 AM Zong Li <zong.li@sifive.com> wrote: > >>> On Fri, Dec 29, 2023 at 2:34 PM Zong Li <zong.li@sifive.com> wrote: > >>>> On Tue, Dec 5, 2023 at 5:00 PM Zong Li <zong.li@sifive.com> wrote: > >>>>> CALLER_ADDRx returns caller's address at specified level, they are used > >>>>> for several tracers. These macros eventually use > >>>>> __builtin_return_address(n) to get the caller's address if arch doesn't > >>>>> define their own implementation. > >>>>> > >>>>> In RISC-V, __builtin_return_address(n) only works when n == 0, we need > >>>>> to walk the stack frame to get the caller's address at specified level. > >>>>> > >>>>> data.level started from 'level + 3' due to the call flow of getting > >>>>> caller's address in RISC-V implementation. If we don't have additional > >>>>> three iteration, the level is corresponding to follows: > >>>>> > >>>>> callsite -> return_address -> arch_stack_walk -> walk_stackframe > >>>>> | | | | > >>>>> level 3 level 2 level 1 level 0 > >>>>> > >>>>> Signed-off-by: Zong Li <zong.li@sifive.com> > >>>>> --- > >>>>> arch/riscv/include/asm/ftrace.h | 5 ++++ > >>>>> arch/riscv/kernel/Makefile | 2 ++ > >>>>> arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ > >>>>> 3 files changed, 55 insertions(+) > >>>>> create mode 100644 arch/riscv/kernel/return_address.c > >>>>> > >>>>> diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h > >>>>> index 2b2f5df7ef2c..42777f91a9c5 100644 > >>>>> --- a/arch/riscv/include/asm/ftrace.h > >>>>> +++ b/arch/riscv/include/asm/ftrace.h > >>>>> @@ -25,6 +25,11 @@ > >>>>> > >>>>> #define ARCH_SUPPORTS_FTRACE_OPS 1 > >>>>> #ifndef __ASSEMBLY__ > >>>>> + > >>>>> +extern void *return_address(unsigned int level); > >>>>> + > >>>>> +#define ftrace_return_address(n) return_address(n) > >>>>> + > >>>>> void MCOUNT_NAME(void); > >>>>> static inline unsigned long ftrace_call_adjust(unsigned long addr) > >>>>> { > >>>>> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > >>>>> index fee22a3d1b53..40d054939ae2 100644 > >>>>> --- a/arch/riscv/kernel/Makefile > >>>>> +++ b/arch/riscv/kernel/Makefile > >>>>> @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE > >>>>> CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) > >>>>> CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) > >>>>> CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) > >>>>> +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) > >>>>> endif > >>>>> CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) > >>>>> CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) > >>>>> @@ -46,6 +47,7 @@ obj-y += irq.o > >>>>> obj-y += process.o > >>>>> obj-y += ptrace.o > >>>>> obj-y += reset.o > >>>>> +obj-y += return_address.o > >>>>> obj-y += setup.o > >>>>> obj-y += signal.o > >>>>> obj-y += syscall_table.o > >>>>> diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c > >>>>> new file mode 100644 > >>>>> index 000000000000..c2008d4aa6e5 > >>>>> --- /dev/null > >>>>> +++ b/arch/riscv/kernel/return_address.c > >>>>> @@ -0,0 +1,48 @@ > >>>>> +// SPDX-License-Identifier: GPL-2.0-only > >>>>> +/* > >>>>> + * This code come from arch/arm64/kernel/return_address.c > >>>>> + * > >>>>> + * Copyright (C) 2023 SiFive. > >>>>> + */ > >>>>> + > >>>>> +#include <linux/export.h> > >>>>> +#include <linux/kprobes.h> > >>>>> +#include <linux/stacktrace.h> > >>>>> + > >>>>> +struct return_address_data { > >>>>> + unsigned int level; > >>>>> + void *addr; > >>>>> +}; > >>>>> + > >>>>> +static bool save_return_addr(void *d, unsigned long pc) > >>>>> +{ > >>>>> + struct return_address_data *data = d; > >>>>> + > >>>>> + if (!data->level) { > >>>>> + data->addr = (void *)pc; > >>>>> + return false; > >>>>> + } > >>>>> + > >>>>> + --data->level; > >>>>> + > >>>>> + return true; > >>>>> +} > >>>>> +NOKPROBE_SYMBOL(save_return_addr); > >>>>> + > >>>>> +void *return_address(unsigned int level) > >>>>> +{ > >>>>> + struct return_address_data data; > >>>>> + > >>>>> + data.level = level + 3; > >>>>> + data.addr = NULL; > >>>>> + > >>>>> + arch_stack_walk(save_return_addr, &data, current, NULL); > >>>>> + > >>>>> + if (!data.level) > >>>>> + return data.addr; > >>>>> + else > >>>>> + return NULL; > >>>>> + > >>>>> +} > >>>>> +EXPORT_SYMBOL_GPL(return_address); > >>>>> +NOKPROBE_SYMBOL(return_address); > >>>>> -- > >>>>> 2.17.1 > >>>>> > >>>> Hi Palmer and all, > >>>> I was wondering whether this patch is good for everyone? Thanks > >>> Hi Palmer, > >>> Is there any chance to include this patch in 6.8-rc1? Thanks > >> Hi Palmer, > >> I'm not sure if this patch is a feature or bug fix, would you consider > >> it for 6.8-rcX? Thanks > > Hi Palmer, > > Sorry for pinging you again. I'm not sure if you saw this patch. The > > irqsoff and wakeup tracer will use CALLER_ADDR1 and CALLER_ADDR2 to > > obtain the caller's return address, but we are currently encountering > > an issue in the RISC-V port where the wrong caller is identified. I > > guess you can easily reproduce the situation using ftrace. Could you > > share your thoughts on this when you have the time to take a look? > > Thanks > > > I'm not Palmer but I'll take a look at your patch today :) > Hi Alexandre, I appreciate your assistance in reviewing this patch, Thanks a lot. :) > Thanks, > > Alex > > > > > > _______________________________________________ > > linux-riscv mailing list > > linux-riscv@lists.infradead.org > > http://lists.infradead.org/mailman/listinfo/linux-riscv
On 05/12/2023 09:59, Zong Li wrote: > CALLER_ADDRx returns caller's address at specified level, they are used > for several tracers. These macros eventually use > __builtin_return_address(n) to get the caller's address if arch doesn't > define their own implementation. > > In RISC-V, __builtin_return_address(n) only works when n == 0, we need > to walk the stack frame to get the caller's address at specified level. Is that a bug in gcc or something expected for riscv in general? > > data.level started from 'level + 3' due to the call flow of getting > caller's address in RISC-V implementation. If we don't have additional > three iteration, the level is corresponding to follows: > > callsite -> return_address -> arch_stack_walk -> walk_stackframe > | | | | > level 3 level 2 level 1 level 0 > > Signed-off-by: Zong Li <zong.li@sifive.com> > --- > arch/riscv/include/asm/ftrace.h | 5 ++++ > arch/riscv/kernel/Makefile | 2 ++ > arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ > 3 files changed, 55 insertions(+) > create mode 100644 arch/riscv/kernel/return_address.c > > diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h > index 2b2f5df7ef2c..42777f91a9c5 100644 > --- a/arch/riscv/include/asm/ftrace.h > +++ b/arch/riscv/include/asm/ftrace.h > @@ -25,6 +25,11 @@ > > #define ARCH_SUPPORTS_FTRACE_OPS 1 > #ifndef __ASSEMBLY__ > + > +extern void *return_address(unsigned int level); > + > +#define ftrace_return_address(n) return_address(n) > + > void MCOUNT_NAME(void); > static inline unsigned long ftrace_call_adjust(unsigned long addr) > { > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > index fee22a3d1b53..40d054939ae2 100644 > --- a/arch/riscv/kernel/Makefile > +++ b/arch/riscv/kernel/Makefile > @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE > CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) > CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) > CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) > +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) > endif > CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) > CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) > @@ -46,6 +47,7 @@ obj-y += irq.o > obj-y += process.o > obj-y += ptrace.o > obj-y += reset.o > +obj-y += return_address.o > obj-y += setup.o > obj-y += signal.o > obj-y += syscall_table.o > diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c > new file mode 100644 > index 000000000000..c2008d4aa6e5 > --- /dev/null > +++ b/arch/riscv/kernel/return_address.c > @@ -0,0 +1,48 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * This code come from arch/arm64/kernel/return_address.c > + * > + * Copyright (C) 2023 SiFive. > + */ > + > +#include <linux/export.h> > +#include <linux/kprobes.h> > +#include <linux/stacktrace.h> > + > +struct return_address_data { > + unsigned int level; > + void *addr; > +}; > + > +static bool save_return_addr(void *d, unsigned long pc) > +{ > + struct return_address_data *data = d; > + > + if (!data->level) { > + data->addr = (void *)pc; > + return false; > + } > + > + --data->level; > + > + return true; > +} > +NOKPROBE_SYMBOL(save_return_addr); > + > +void *return_address(unsigned int level) Maybe return_address() should be noinline to make sure it's not inlined as it would break the "+ 3"? Not sure it's necessary though. > +{ > + struct return_address_data data; > + > + data.level = level + 3; > + data.addr = NULL; > + > + arch_stack_walk(save_return_addr, &data, current, NULL); > + > + if (!data.level) > + return data.addr; > + else > + return NULL; > + > +} > +EXPORT_SYMBOL_GPL(return_address); > +NOKPROBE_SYMBOL(return_address); And I see that with this patch, ftrace_return_address() is now defined whether CONFIG_FRAME_POINTER is set or not, is that correct? This looks like a fix to me so that should go into -fixes with a Fixes tag (but we'll have to make sure that the "+ 3" is correct on all backports...): Fixes: 10626c32e382 ("riscv/ftrace: Add basic support") And you can finally add for your v2 (or not if you don't respin): Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> Thanks and sorry for the delay! Alex
On Thu, Feb 1, 2024 at 1:10 AM Alexandre Ghiti <alex@ghiti.fr> wrote: > > On 05/12/2023 09:59, Zong Li wrote: > > CALLER_ADDRx returns caller's address at specified level, they are used > > for several tracers. These macros eventually use > > __builtin_return_address(n) to get the caller's address if arch doesn't > > define their own implementation. > > > > In RISC-V, __builtin_return_address(n) only works when n == 0, we need > > to walk the stack frame to get the caller's address at specified level. > > > Is that a bug in gcc or something expected for riscv in general? > I think it isn't supported for riscv in general. > > > > > data.level started from 'level + 3' due to the call flow of getting > > caller's address in RISC-V implementation. If we don't have additional > > three iteration, the level is corresponding to follows: > > > > callsite -> return_address -> arch_stack_walk -> walk_stackframe > > | | | | > > level 3 level 2 level 1 level 0 > > > > Signed-off-by: Zong Li <zong.li@sifive.com> > > --- > > arch/riscv/include/asm/ftrace.h | 5 ++++ > > arch/riscv/kernel/Makefile | 2 ++ > > arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ > > 3 files changed, 55 insertions(+) > > create mode 100644 arch/riscv/kernel/return_address.c > > > > diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h > > index 2b2f5df7ef2c..42777f91a9c5 100644 > > --- a/arch/riscv/include/asm/ftrace.h > > +++ b/arch/riscv/include/asm/ftrace.h > > @@ -25,6 +25,11 @@ > > > > #define ARCH_SUPPORTS_FTRACE_OPS 1 > > #ifndef __ASSEMBLY__ > > + > > +extern void *return_address(unsigned int level); > > + > > +#define ftrace_return_address(n) return_address(n) > > + > > void MCOUNT_NAME(void); > > static inline unsigned long ftrace_call_adjust(unsigned long addr) > > { > > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > > index fee22a3d1b53..40d054939ae2 100644 > > --- a/arch/riscv/kernel/Makefile > > +++ b/arch/riscv/kernel/Makefile > > @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE > > CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) > > CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) > > CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) > > +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) > > endif > > CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) > > CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) > > @@ -46,6 +47,7 @@ obj-y += irq.o > > obj-y += process.o > > obj-y += ptrace.o > > obj-y += reset.o > > +obj-y += return_address.o > > obj-y += setup.o > > obj-y += signal.o > > obj-y += syscall_table.o > > diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c > > new file mode 100644 > > index 000000000000..c2008d4aa6e5 > > --- /dev/null > > +++ b/arch/riscv/kernel/return_address.c > > @@ -0,0 +1,48 @@ > > +// SPDX-License-Identifier: GPL-2.0-only > > +/* > > + * This code come from arch/arm64/kernel/return_address.c > > + * > > + * Copyright (C) 2023 SiFive. > > + */ > > + > > +#include <linux/export.h> > > +#include <linux/kprobes.h> > > +#include <linux/stacktrace.h> > > + > > +struct return_address_data { > > + unsigned int level; > > + void *addr; > > +}; > > + > > +static bool save_return_addr(void *d, unsigned long pc) > > +{ > > + struct return_address_data *data = d; > > + > > + if (!data->level) { > > + data->addr = (void *)pc; > > + return false; > > + } > > + > > + --data->level; > > + > > + return true; > > +} > > +NOKPROBE_SYMBOL(save_return_addr); > > + > > +void *return_address(unsigned int level) > > > Maybe return_address() should be noinline to make sure it's not inlined > as it would break the "+ 3"? Not sure it's necessary though. Yes, thanks for pointing it out. We should ensure it's not inlined in any case. I will send the next version. > > > > +{ > > + struct return_address_data data; > > + > > + data.level = level + 3; > > + data.addr = NULL; > > + > > + arch_stack_walk(save_return_addr, &data, current, NULL); > > + > > + if (!data.level) > > + return data.addr; > > + else > > + return NULL; > > + > > +} > > +EXPORT_SYMBOL_GPL(return_address); > > +NOKPROBE_SYMBOL(return_address); > > > And I see that with this patch, ftrace_return_address() is now defined > whether CONFIG_FRAME_POINTER is set or not, is that correct? Yes, that is what I understand. In this patch, the ftrace_return_address() is still defined to return_address() when CONFIG_FRAME_POINTER isn't enabled, and return_address still works because riscv port can walk stackframe without fp. > > This looks like a fix to me so that should go into -fixes with a Fixes > tag (but we'll have to make sure that the "+ 3" is correct on all > backports...): > > Fixes: 10626c32e382 ("riscv/ftrace: Add basic support") The return_address() invokes arch_stack_walk(), which enabled by the '5cb0080f1bfd ("riscv: Enable ARCH_STACKWALK")' I guess that we are not able to apply it on all backports. Is this right? "+3" is correct after enabling ARCH_STACKWALK. > > And you can finally add for your v2 (or not if you don't respin): > > Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> Thanks for the review, Alexandre. I will add it in v2:) > > Thanks and sorry for the delay! > > Alex >
On 01/02/2024 09:43, Zong Li wrote: > On Thu, Feb 1, 2024 at 1:10 AM Alexandre Ghiti <alex@ghiti.fr> wrote: >> On 05/12/2023 09:59, Zong Li wrote: >>> CALLER_ADDRx returns caller's address at specified level, they are used >>> for several tracers. These macros eventually use >>> __builtin_return_address(n) to get the caller's address if arch doesn't >>> define their own implementation. >>> >>> In RISC-V, __builtin_return_address(n) only works when n == 0, we need >>> to walk the stack frame to get the caller's address at specified level. >> >> Is that a bug in gcc or something expected for riscv in general? >> > I think it isn't supported for riscv in general. > >>> data.level started from 'level + 3' due to the call flow of getting >>> caller's address in RISC-V implementation. If we don't have additional >>> three iteration, the level is corresponding to follows: >>> >>> callsite -> return_address -> arch_stack_walk -> walk_stackframe >>> | | | | >>> level 3 level 2 level 1 level 0 >>> >>> Signed-off-by: Zong Li <zong.li@sifive.com> >>> --- >>> arch/riscv/include/asm/ftrace.h | 5 ++++ >>> arch/riscv/kernel/Makefile | 2 ++ >>> arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ >>> 3 files changed, 55 insertions(+) >>> create mode 100644 arch/riscv/kernel/return_address.c >>> >>> diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h >>> index 2b2f5df7ef2c..42777f91a9c5 100644 >>> --- a/arch/riscv/include/asm/ftrace.h >>> +++ b/arch/riscv/include/asm/ftrace.h >>> @@ -25,6 +25,11 @@ >>> >>> #define ARCH_SUPPORTS_FTRACE_OPS 1 >>> #ifndef __ASSEMBLY__ >>> + >>> +extern void *return_address(unsigned int level); >>> + >>> +#define ftrace_return_address(n) return_address(n) >>> + >>> void MCOUNT_NAME(void); >>> static inline unsigned long ftrace_call_adjust(unsigned long addr) >>> { >>> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile >>> index fee22a3d1b53..40d054939ae2 100644 >>> --- a/arch/riscv/kernel/Makefile >>> +++ b/arch/riscv/kernel/Makefile >>> @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE >>> CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) >>> CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) >>> CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) >>> +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) >>> endif >>> CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) >>> CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) >>> @@ -46,6 +47,7 @@ obj-y += irq.o >>> obj-y += process.o >>> obj-y += ptrace.o >>> obj-y += reset.o >>> +obj-y += return_address.o >>> obj-y += setup.o >>> obj-y += signal.o >>> obj-y += syscall_table.o >>> diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c >>> new file mode 100644 >>> index 000000000000..c2008d4aa6e5 >>> --- /dev/null >>> +++ b/arch/riscv/kernel/return_address.c >>> @@ -0,0 +1,48 @@ >>> +// SPDX-License-Identifier: GPL-2.0-only >>> +/* >>> + * This code come from arch/arm64/kernel/return_address.c >>> + * >>> + * Copyright (C) 2023 SiFive. >>> + */ >>> + >>> +#include <linux/export.h> >>> +#include <linux/kprobes.h> >>> +#include <linux/stacktrace.h> >>> + >>> +struct return_address_data { >>> + unsigned int level; >>> + void *addr; >>> +}; >>> + >>> +static bool save_return_addr(void *d, unsigned long pc) >>> +{ >>> + struct return_address_data *data = d; >>> + >>> + if (!data->level) { >>> + data->addr = (void *)pc; >>> + return false; >>> + } >>> + >>> + --data->level; >>> + >>> + return true; >>> +} >>> +NOKPROBE_SYMBOL(save_return_addr); >>> + >>> +void *return_address(unsigned int level) >> >> Maybe return_address() should be noinline to make sure it's not inlined >> as it would break the "+ 3"? Not sure it's necessary though. > Yes, thanks for pointing it out. We should ensure it's not inlined in > any case. I will send the next version. > >> >>> +{ >>> + struct return_address_data data; >>> + >>> + data.level = level + 3; >>> + data.addr = NULL; >>> + >>> + arch_stack_walk(save_return_addr, &data, current, NULL); >>> + >>> + if (!data.level) >>> + return data.addr; >>> + else >>> + return NULL; >>> + >>> +} >>> +EXPORT_SYMBOL_GPL(return_address); >>> +NOKPROBE_SYMBOL(return_address); >> >> And I see that with this patch, ftrace_return_address() is now defined >> whether CONFIG_FRAME_POINTER is set or not, is that correct? > Yes, that is what I understand. In this patch, the > ftrace_return_address() is still defined to return_address() when > CONFIG_FRAME_POINTER isn't enabled, and return_address still works > because riscv port can walk stackframe without fp. > >> This looks like a fix to me so that should go into -fixes with a Fixes >> tag (but we'll have to make sure that the "+ 3" is correct on all >> backports...): >> >> Fixes: 10626c32e382 ("riscv/ftrace: Add basic support") > The return_address() invokes arch_stack_walk(), which enabled by the > '5cb0080f1bfd ("riscv: Enable ARCH_STACKWALK")' > > I guess that we are not able to apply it on all backports. Is this > right? "+3" is correct after enabling ARCH_STACKWALK. 5cb0080f1bfd was introduced in v5.11, so that will make the backport possible up to 5.15, I guess that's ok, nobody will ever use a riscv kernel that old (?). So I'd add the Fixes tag I proposed above, and let the backport fail for < 5.15. @Conor any opinion? > >> And you can finally add for your v2 (or not if you don't respin): >> >> Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> > Thanks for the review, Alexandre. I will add it in v2:) Thanks, Alex > >> Thanks and sorry for the delay! >> >> Alex >> > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv
On Thu, Feb 01, 2024 at 11:07:48AM +0100, Alexandre Ghiti wrote: > 5cb0080f1bfd was introduced in v5.11, so that will make the backport possible up to 5.15, I guess that's ok, nobody will ever use a riscv kernel that old (?). > > So I'd add the Fixes tag I proposed above, and let the backport fail for < 5.15. @Conor any opinion? The stable kernels are 5.10 and 5.15, so there's no kernels that matter which have the bad commit and are < 5.15.
diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h index 2b2f5df7ef2c..42777f91a9c5 100644 --- a/arch/riscv/include/asm/ftrace.h +++ b/arch/riscv/include/asm/ftrace.h @@ -25,6 +25,11 @@ #define ARCH_SUPPORTS_FTRACE_OPS 1 #ifndef __ASSEMBLY__ + +extern void *return_address(unsigned int level); + +#define ftrace_return_address(n) return_address(n) + void MCOUNT_NAME(void); static inline unsigned long ftrace_call_adjust(unsigned long addr) { diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index fee22a3d1b53..40d054939ae2 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) @@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.o diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c new file mode 100644 index 000000000000..c2008d4aa6e5 --- /dev/null +++ b/arch/riscv/kernel/return_address.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * This code come from arch/arm64/kernel/return_address.c + * + * Copyright (C) 2023 SiFive. + */ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { + unsigned int level; + void *addr; +}; + +static bool save_return_addr(void *d, unsigned long pc) +{ + struct return_address_data *data = d; + + if (!data->level) { + data->addr = (void *)pc; + return false; + } + + --data->level; + + return true; +} +NOKPROBE_SYMBOL(save_return_addr); + +void *return_address(unsigned int level) +{ + struct return_address_data data; + + data.level = level + 3; + data.addr = NULL; + + arch_stack_walk(save_return_addr, &data, current, NULL); + + if (!data.level) + return data.addr; + else + return NULL; + +} +EXPORT_SYMBOL_GPL(return_address); +NOKPROBE_SYMBOL(return_address);