Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp65898rdb; Thu, 1 Feb 2024 02:08:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IE1UxIFZtweApnhny3z5hFJ7ZztpRp3RRnrFgVsgOKH3bbapyR3dOZWoKIGg6R1tJhbIwF5 X-Received: by 2002:a05:620a:28d1:b0:785:43f2:9860 with SMTP id l17-20020a05620a28d100b0078543f29860mr2431135qkp.3.1706782084598; Thu, 01 Feb 2024 02:08:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706782084; cv=pass; d=google.com; s=arc-20160816; b=rWzNdLOq7p/vFZ+YJ2UrAOrWr4o50QfXYEr3BCIedOR3j5idJ1UsGiJ/xL2NG4/kTg CvrB7Tvk+T3GM1MnhjNL3bks5Y9+doI5Hb9EcxNTCieFNNUgMIatffCs2ggbXKkbVcPE giNNa9EZWFzGRJ6Tyskatji83CBpGTNxeJQrFBwfJLjcGDA6aQFx6Q2Vz30iQZPzIyMe TGz8s2HEK9Rz+XNILdsMVX2abqDwrsIC/3Ju8U1BLHfr+C25NF4ITvYpT+jXzKJ5vZB4 gnc49xDFm7LSPbfuyNJk06NMYqniikksQKkOUHu/JBJ7wS0koM4HR2VqPI8I1NfPKIzO Ya9A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=/S2qlLP6NZkhtdSd9hvtTFu0OR0z5l+We79TQy8S4qU=; fh=WyGCK0FIIYdtDUpqckWwp+b4U6t2sMk8/5SuJ5eeNLs=; b=OvT+nQSA4D+lf/Gwgh8L9b3ImjXfzcJuGzYfctJ7STjMfBF2t0tSmaZYj7Fqba1qp8 aAOYZ4bon/8YrPW9s8qK/yQ21BbZvjwC0Zt/n2QmHCIydJ2PDmRpSa3NzfSodHpH8KBO uMEzF6qwBWJzkd4CvJZBmpE0E1dRISDt3IHYrnwfZCx4j0y1n91mvDOdjpdy1w5kGeje k4DiDW0gTCxGdoqe42bxQwzqh7svfIMEq9F1r2iMYYw7LT6/4+5I+ZHfUk7EF9ZMF5om DF9qaWOAJYvd3oTJARvRhq+fdNR0J7XUvOU2+WzwtNuTmKfQWxFGBRxaDLmDO6wyM1Tj J3kw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-47938-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47938-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCXDptC9Ykusdo0hja2VsIpEa7DW4wPiW/2sLKvrn/cTe+0ijvgcoKxhwb5tAQVig+owpwhzlZxETBMTXGZqA5XQfQGRA5f9chKoN09aWw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id b4-20020a05620a126400b00783f86e2ad2si9045572qkl.401.2024.02.01.02.08.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 02:08:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47938-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-47938-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47938-linux.lists.archive=gmail.com@vger.kernel.org" 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 2E5D51C210D8 for ; Thu, 1 Feb 2024 10:08:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 65EA515B0F6; Thu, 1 Feb 2024 10:07:57 +0000 (UTC) Received: from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net [217.70.183.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CAE34D9E7 for ; Thu, 1 Feb 2024 10:07:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.193 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706782076; cv=none; b=drYipehoR2LkHeWp43cDrgNV7xEoJlILOj+b33SiHuIPiaWfoZQ67VxdtFi6gXtGcmGat6y1QbGJEqgQGTbfJ8caQ1CkI+3NB6xSUVtZ8Df7EJZtiM0FaZwzWPgrLXAlBj+1k5uo1pNjljfpSc1KdstKyfTXbvmBovL91jIUX90= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706782076; c=relaxed/simple; bh=H3NlgKMwCfq774/B0cUI4gDakYJQeaoj9l7F/QS62HE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=SFqUJ1sgqV1kGuYSKoQc6CDOUsGHrMEz0Lmjt/cAhEl0jPYnpnkIxVsIcNsFZKI4zvjAxRxhkVzd4460ev26L37h75x3UJrdBdGVGOdJpchw72cHIhsgjFkFKarhBg1VWRVTdd8V7eBMxnp6k1Q1W5DCdw7GuSRtv2m1lrWOt+c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr; spf=pass smtp.mailfrom=ghiti.fr; arc=none smtp.client-ip=217.70.183.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ghiti.fr Received: by mail.gandi.net (Postfix) with ESMTPSA id 5B34E24000C; Thu, 1 Feb 2024 10:07:49 +0000 (UTC) Message-ID: <4bf64df4-43a4-4207-b7d5-ff07adb98193@ghiti.fr> Date: Thu, 1 Feb 2024 11:07:48 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] riscv: add CALLER_ADDRx support Content-Language: en-US To: Zong Li , Conor Dooley Cc: palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org References: <20231205085959.32177-1-zong.li@sifive.com> <139cdbd6-73d9-4452-94ce-825689b7c0c8@ghiti.fr> From: Alexandre Ghiti In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-GND-Sasl: alex@ghiti.fr On 01/02/2024 09:43, Zong Li wrote: > On Thu, Feb 1, 2024 at 1:10 AM Alexandre Ghiti 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 >>> --- >>> 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 >>> +#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 = 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 > 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