Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1702078rdb; Wed, 31 Jan 2024 06:47:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IELq4vf1nG5wM/PFwYGgaRJwekwdTbdZVnG04KAWQ4ktYwYE4+Ju4ETw8qd3LtMizTMvesf X-Received: by 2002:a17:906:af1a:b0:a35:3ce3:c490 with SMTP id lx26-20020a170906af1a00b00a353ce3c490mr1283870ejb.6.1706712420078; Wed, 31 Jan 2024 06:47:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706712420; cv=pass; d=google.com; s=arc-20160816; b=m/D3W87P4QMvducurRLdppJ5dbnlDccKL1KVlFDSy3rGlkYXicGVQLXnymtuTOq20i DnkNDeH2Ldbivt2ck2weJj6EMnPfO80Z/RNKDHe6/OvfVbVNRdNDnNl8kyTVxaaQPqgX TCjQgaEiGnfUsf5+dlGakTQMAUgROPeU9SEIR1tloewb4z9y951nb3SKd+gCESpvv6xy AjD4yEAO25v/TCyx9R3JBl3IqE1qwpMRvmhJfWqV3HufVulYXsPOrOs3EHAz2H8sjpz1 jNkOnzl8KK4kc3QgW6QOz2nWgqKXI5noHFkUouiGxw6OTfKff57HbSON2wsimUOIHTbE 98DA== 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:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=Cwxg8kL0Z9JMzCL6lR3y491zPAZ43rnYYsuo5KAnWo8=; fh=mQMlqehmMvEY/rkiaQVLwHfeUQAxpdM2nk6ZhaHjeYA=; b=WIHPmyOUYAvFzhk+YIuWQf9k24AvIAnwvXHmReH55VIHU1EHYOPiFaE5tx23CDnboa z+DuveHQS4eJTJAhezFHp/G6jaz0Z3Ag1kGWE9rW96uVTPtt8givKSdlcQeObBwih+1F I/H4UstZVpOLSTPyvAq1PgmdUozYU39p4z5gRM5fOHFv2e6I+uExztMvlbI11gC9tkSk jNuqQaRkoXIW74PmPYOfYk8PXh2iVVHvuRfhBeiQawgSRwGjCgYKQS1w2uDlg5pG8eti IOVKHKDZ7Uj8kegBAseAi1ZXA35DckGnqPGHzAXj3z+/wZPUGPQUbunFqNpoXPp7Lxny Q8Dw==; 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-46623-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46623-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCWLQ6QcGBeP5DALINPbtvNDRj02YYEROvGNHAcrkfkmL3i7+It6Kiyq5lCnvAmyUXFOsMdzXWrZG+3ZujiivVIYg0z3ElW73+ITaw5CAw== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id mc7-20020a170906eb4700b00a3613bb338fsi1916024ejb.227.2024.01.31.06.47.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 06:47:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46623-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; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-46623-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46623-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 4E49E1F26EA1 for ; Wed, 31 Jan 2024 14:46:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6C34A84A35; Wed, 31 Jan 2024 14:46:37 +0000 (UTC) Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 59F948287A for ; Wed, 31 Jan 2024 14:46:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706712396; cv=none; b=b5CoQxwAXgjY5kRz6CKz0yph2rrnXKQg5pgFFt4s6WNPbiINg7cedsiDvf/BmjQZsG4hF53Ax72lBky6X8AP59RqYe9AsgjDq0KxPagu/2PgABCexGNnGYKJSIzoOXrCGMsW66Hf7vRba0JnLlC4EqID6hZOKRazBp15e92BgUg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706712396; c=relaxed/simple; bh=xw/JEHipL3IdpaBrUlztJJRa6YI56n1PTV2RANvdiQI=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type; b=s7JHDi+cMEqJYfPFIHx+vxSz/rwZs/VBThaoF3TcAlVpqGRQn1xK2VmDleD3xRx84L9Ub/y5cy6z43F7uFaFZoLxfBCBuAmRg2w4BQor7vkqtwHEV6FL60GZSdCSAf+tkx1oZf4FVIeXdLhNv+7Ie9wzgtCp9zv980d1yvwmzyM= 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.196 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 6CEA9E0003; Wed, 31 Jan 2024 14:46:28 +0000 (UTC) Message-ID: <25e2faa5-908c-4391-87db-8540d1f0e904@ghiti.fr> Date: Wed, 31 Jan 2024 15:46:27 +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 , palmer@dabbelt.com, Palmer Dabbelt , 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> 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 Hi Zong, On 31/01/2024 15:24, Zong Li wrote: > On Thu, Jan 18, 2024 at 8:50 AM Zong Li wrote: >> On Wed, Jan 10, 2024 at 11:33 AM Zong Li wrote: >>> On Fri, Dec 29, 2023 at 2:34 PM Zong Li wrote: >>>> On Tue, Dec 5, 2023 at 5:00 PM 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. >>>>> >>>>> 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) >>>>> +{ >>>>> + 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