Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp732525rdd; Tue, 9 Jan 2024 19:33:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IFVM9WkwXljlZGQ6ft/J1n+Bj4FwCY9DIrXuOX1+bVfX1F5lbAaKTOoh695kJKm7urdOWfo X-Received: by 2002:a17:906:e209:b0:a2a:18ad:ebb4 with SMTP id gf9-20020a170906e20900b00a2a18adebb4mr233180ejb.62.1704857615826; Tue, 09 Jan 2024 19:33:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704857615; cv=none; d=google.com; s=arc-20160816; b=eQVRSGPPtyj06Mifiex7OV0dP7/J+FxheM72cawq3r79piamOgiafWxyd20WTDBnXE atsMYfQ5KVUsxynEbE9bKLqgGTV6fBTUl/qh4qCX3DLyWeinXDnScruYEsVNMkziPbRS A9HnJeESk0V6erj6D9z9F98izeb6WSI9gJxGdJe2JLQhULn3d+AsrIKNuwtDc/EYCH1q tg4HUVKhFJWIzsZEQ21xBFMiM924tlOE+9P4Cwdy9yRy6gWUsdDvNnP1iruugDVIQz1x 57qqQapGj8qGgvjsxwpLBzFUNAI8+xll2VpdEgxgzXEGqvOcBBWp8OT6juhQsJHl7SqV mVRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=TTJAAReDmCgeUKZ+956Wk5F3FplJWRu8shC9LpiGkOg=; fh=qfTC4Dyf7nCtBX8ssYJJjvkgkhtn2DfW3RBUWlHAc9M=; b=jbeI8IvwlZ4f18uxmMbJE2trpAetMKKYRB5CYNmjv0Jmmxt4EfcFepMdfj4/DXu+QD 5sXDO3HUR6+k0XCJDGkr3ZyIftDHViZ0MEi6JaikXvZWUmMcZDrYa84CD0VzAdRQAs79 FwSqF7mM6rvB10i28qYTIQYj7sVgFzPcxKl8goD9rvQygFxPvQ2SoHugzFOcvdxB2uNZ WNCNaZrBJENkAcz6qG2O26HvB6m21Wui0vooB8P+zCrQYYFry1e+FjKxab03wlJ3ApJv GnVklGVmma80gP1VbZtqQle2vQzkf4JJ9QDULQqdnlDApPtKcquMG0jZx2Y00GS4HDfE HOFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=jAWRYXFH; spf=pass (google.com: domain of linux-kernel+bounces-21689-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21689-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id i14-20020a170906250e00b00a2af5d0924asi1367106ejb.893.2024.01.09.19.33.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 19:33:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21689-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=jAWRYXFH; spf=pass (google.com: domain of linux-kernel+bounces-21689-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21689-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 67D101F26836 for ; Wed, 10 Jan 2024 03:33:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 587846AA0; Wed, 10 Jan 2024 03:33:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="jAWRYXFH" Received: from mail-io1-f53.google.com (mail-io1-f53.google.com [209.85.166.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE47C63A0 for ; Wed, 10 Jan 2024 03:33:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-io1-f53.google.com with SMTP id ca18e2360f4ac-7bee328027bso39998839f.1 for ; Tue, 09 Jan 2024 19:33:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1704857605; x=1705462405; darn=vger.kernel.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=TTJAAReDmCgeUKZ+956Wk5F3FplJWRu8shC9LpiGkOg=; b=jAWRYXFHAYfURSxZ91SQzcTSTZ+PVcnaxL3h55bLmGbMhqF19eqLrDiJNY64fv5XbH 8xU8qECCr4wfFPq09ZlOCA7MJcIKtP/6+Ny4K/Vgpn/woALjM0CxtupioccUplkFWsBP 3k9XygQGrGiy5tw0U0W2gcNTLA/I1GUnrMnmv9wNjrt0caS6C7tha1SKD0SrZ1ma69eE Cg7Rfsybxpp8U4R/NjgD/0UgdlvdUTBiK5NShkFWIUrra31kW3o2nR6DNYun/96Eoco5 FlLY5TcHG2BOzW0RcPRw91i9FBAIBNDCEBohSHq1PM2IUi1kUo9XYXW3A14h0kKqA+RT 7m/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704857605; x=1705462405; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TTJAAReDmCgeUKZ+956Wk5F3FplJWRu8shC9LpiGkOg=; b=f+FIWxUbGJ3MlDOxRNktJlw3KkNFYiYLq3zIxcRM5t+vmfz2hHFW/AaKYx66voiDkB fXQ8twWM3i5ynxKAJObjolFjWx8e2rycPOQlEi64o9xTASIJJSL96PsuCRUfYgKC5fe7 tbLXOV/gWoMZP/Hk4m3fYyQKaCf3Pm/ZsuyElHJDkX87Gx2tj23cZeg4HJ+7WrxWMStu +VXZUaujERBbqpkJi7ZgzlbyrXwmyBMbho4f+ZlAm4zZ18/t232kKTn9gLMk76GBt5hR gTPqFu3T88HFGVaHhr2YvBzy/6xEPRyX11xNzIUP/4oKR1shnw+1MoGmZXScySOu4TnM uTmQ== X-Gm-Message-State: AOJu0YylIeTTzbH9tQKLeuJm5S6oxZ/bIHP0flppORS+vzCyzKOHwDbw uCqlpbPoDshaFjpitGc/IfJ5FNEPQNx7mPBgPwS3kSl1tJ7prg== X-Received: by 2002:a6b:5f05:0:b0:7be:d961:6b0a with SMTP id t5-20020a6b5f05000000b007bed9616b0amr107497iob.18.1704857604934; Tue, 09 Jan 2024 19:33:24 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231205085959.32177-1-zong.li@sifive.com> In-Reply-To: From: Zong Li Date: Wed, 10 Jan 2024 11:33:14 +0800 Message-ID: Subject: Re: [PATCH] riscv: add CALLER_ADDRx support To: palmer@dabbelt.com, Palmer Dabbelt , paul.walmsley@sifive.com, aou@eecs.berkeley.edu, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Dec 29, 2023 at 2:34=E2=80=AFPM Zong Li wrote: > > On Tue, Dec 5, 2023 at 5:00=E2=80=AFPM 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 =3D=3D 0, we n= eed > > 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 > > --- > > 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/f= trace.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 =3D $(CC_FLAGS_FTRACE) > > CFLAGS_REMOVE_patch.o =3D $(CC_FLAGS_FTRACE) > > CFLAGS_REMOVE_sbi.o =3D $(CC_FLAGS_FTRACE) > > +CFLAGS_REMOVE_return_address.o =3D $(CC_FLAGS_FTRACE) > > endif > > CFLAGS_syscall_table.o +=3D $(call cc-option,-Wno-override-init,) > > CFLAGS_compat_syscall_table.o +=3D $(call cc-option,-Wno-override-init= ,) > > @@ -46,6 +47,7 @@ obj-y +=3D irq.o > > obj-y +=3D process.o > > obj-y +=3D ptrace.o > > obj-y +=3D reset.o > > +obj-y +=3D return_address.o > > obj-y +=3D setup.o > > obj-y +=3D signal.o > > obj-y +=3D syscall_table.o > > diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/ret= urn_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 > > +#include > > +#include > > + > > +struct return_address_data { > > + unsigned int level; > > + void *addr; > > +}; > > + > > +static bool save_return_addr(void *d, unsigned long pc) > > +{ > > + struct return_address_data *data =3D d; > > + > > + if (!data->level) { > > + data->addr =3D (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 =3D level + 3; > > + data.addr =3D 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