From patchwork Wed Nov 8 14:29:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 163057 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp950666vqo; Wed, 8 Nov 2023 06:29:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IE8XqF3YfzjRoHajyDd4QQuS63dEhsd4ySn5hOnwoYH2533LE6OaDNBrKHpsvJsy/sCNP9o X-Received: by 2002:a05:6a00:1c90:b0:6b3:80f8:7e91 with SMTP id y16-20020a056a001c9000b006b380f87e91mr3497430pfw.9.1699453799076; Wed, 08 Nov 2023 06:29:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699453799; cv=none; d=google.com; s=arc-20160816; b=ZXjHI8SZ78LEpJig2RpNzOjB+5VqsggbocQdZAtMaxU9wFueVtnDyjCPg49oUO0H88 JYytRqg3lRUn/9mOPhbbWLNi7GL+0MxpVeVvq6c6Uld2uGLeFACxTzCDXQ/G47CvC9It 7nAhKSCy4D6G6xsgbYTwXqDAoT319Z150S54FCNe++5qtdOkDNA2FfeP6plUQdWGOCb/ w2AXxR05bblNvOurDbsRQc6uS0OKX263oH3f6/53GW+5yNq46v/8wwVglnjmdwFHxRC3 djhzKp5e7O4V7+3Ep/LdpMhXZ5qoN5bXqjkSXuoOyph8iwkNU93tAY+G1SClIMkB3TwS 8SMQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=t72XAcO2VgpLFC5qLxCjcoXMWzCMywmnN90A6MRl0f0=; fh=SIgps5XdV0XNwjZfT2uAI7g3mrspDldK9Qs8qQAfoa4=; b=1Az6ZC8MFtjwzsyi+cXag9Ss/c4jIaAD31fzA5/Ao1KD4FBhulLjGP5lDImhjMFNpZ he60DecK1P3Pep6eR2EM0Pyq5LiwCrrKullBW/cYIXnTnzU6iZX96kca39rS1P/o7UFX pTr7lAM8KyoaMMgC1cOn+MzT1NC0yzKS8DZ/FUKiEs6GEVuwMpyDqaHPfBrnMNeC0iey konvLT3CGwnfPB+Buo9vM+PMalCQ1PL+KLcb5Xv+H+jhnSFc4pJWYuDzOLRh23IleNET wq68Ug/HcOqsDfYOkvJboAMKGsRyJ+pWiFqYeMNO0CEiJUyDpPPzrB3Zqj+5Jr/REAb7 AeCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MTrRcqZ0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id m5-20020a6562c5000000b005a9c40151b3si4511371pgv.804.2023.11.08.06.29.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 06:29:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MTrRcqZ0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 2BEA48331DFD; Wed, 8 Nov 2023 06:29:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233324AbjKHO3z (ORCPT + 32 others); Wed, 8 Nov 2023 09:29:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343630AbjKHO31 (ORCPT ); Wed, 8 Nov 2023 09:29:27 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD37B259F for ; Wed, 8 Nov 2023 06:29:16 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2537C433C7; Wed, 8 Nov 2023 14:29:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699453756; bh=Hk7SCJAJ6eIOExLRFCUV2MnClkrwEAjdB+TwjIXMW5w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MTrRcqZ0iyVrRGaoPg5sFV+F5wsq9h4p2jfHVC2jwuJQaq6iZPpt1Gr7hY8tOa7Qz 02TFvE2C57GXijqHcSAEC1i/4EMzS2eXqZeB/KnMQDMfzy4Q/sVUNuBdjf2Fxx5yeV 0+3b59Oa72AQVddSUIeZukH8XkQA2dW+wIE1JFsc18MN81OklJUcHL4c8vowi2on6/ IoD4eQCZ/Bbt+Ig3MpSx+ESt7ZEDgN6tCUrE8dT90NpOe8tsXIbP3mWQtvH81XMaZL /junJWSd6auc0pVhrSUa/pI/EGYXda+4kt7T/54PxkTrLjsJZxTh9Mv0hOxgM3I+rK qZrEBRYbNKjLQ== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov , Jiri Olsa , Arnaldo Carvalho de Melo , Daniel Borkmann , Alan Maguire , Mark Rutland , Peter Zijlstra , Thomas Gleixner , Guo Ren Subject: [RFC PATCH v2 25/31] tracing: Add ftrace_fill_perf_regs() for perf event Date: Wed, 8 Nov 2023 23:29:10 +0900 Message-Id: <169945374985.55307.4421748716497412063.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <169945345785.55307.5003201137843449313.stgit@devnote2> References: <169945345785.55307.5003201137843449313.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 08 Nov 2023 06:29:58 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782006467171166575 X-GMAIL-MSGID: 1782006467171166575 From: Masami Hiramatsu (Google) Add ftrace_fill_perf_regs() which should be compatible with the perf_fetch_caller_regs(). In other words, the pt_regs returned from the ftrace_fill_perf_regs() must satisfy 'user_mode(regs) == false' and can be used for stack tracing. Signed-off-by: Masami Hiramatsu (Google) --- Changes from previous series: NOTHING, just forward ported. --- arch/arm64/include/asm/ftrace.h | 7 +++++++ arch/powerpc/include/asm/ftrace.h | 7 +++++++ arch/s390/include/asm/ftrace.h | 5 +++++ arch/x86/include/asm/ftrace.h | 7 +++++++ include/linux/ftrace.h | 31 +++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+) diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrace.h index 5ad24f315d52..5fbe8fe3e8a5 100644 --- a/arch/arm64/include/asm/ftrace.h +++ b/arch/arm64/include/asm/ftrace.h @@ -148,6 +148,13 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) return regs; } +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->pc = (fregs)->pc; \ + (_regs)->regs[29] = (fregs)->fp; \ + (_regs)->sp = (fregs)->sp; \ + (_regs)->pstate = PSR_MODE_EL1h; \ + } while (0) + int ftrace_regs_query_register_offset(const char *name); int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec); diff --git a/arch/powerpc/include/asm/ftrace.h b/arch/powerpc/include/asm/ftrace.h index 9e5a39b6a311..53fa39ef476b 100644 --- a/arch/powerpc/include/asm/ftrace.h +++ b/arch/powerpc/include/asm/ftrace.h @@ -52,6 +52,13 @@ static __always_inline struct pt_regs *arch_ftrace_get_regs(struct ftrace_regs * return fregs->regs.msr ? &fregs->regs : NULL; } +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->result = 0; \ + (_regs)->nip = (fregs)->regs.nip; \ + (_regs)->gpr[1] = (fregs)->regs.gpr[1]; \ + asm volatile("mfmsr %0" : "=r" ((_regs)->msr)); \ + } while (0) + static __always_inline void ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs, unsigned long ip) diff --git a/arch/s390/include/asm/ftrace.h b/arch/s390/include/asm/ftrace.h index 5a82b08f03cd..f36ee3510af8 100644 --- a/arch/s390/include/asm/ftrace.h +++ b/arch/s390/include/asm/ftrace.h @@ -97,6 +97,11 @@ ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs, #define ftrace_regs_query_register_offset(name) \ regs_query_register_offset(name) +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->psw.addr = (fregs)->regs.psw.addr; \ + (_regs)->gprs[15] = (fregs)->regs.gprs[15]; \ + } while (0) + #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS /* * When an ftrace registered caller is tracing a function that is diff --git a/arch/x86/include/asm/ftrace.h b/arch/x86/include/asm/ftrace.h index 4701d009c215..0ac397931bf2 100644 --- a/arch/x86/include/asm/ftrace.h +++ b/arch/x86/include/asm/ftrace.h @@ -54,6 +54,13 @@ arch_ftrace_get_regs(struct ftrace_regs *fregs) return &fregs->regs; } +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->ip = (fregs)->regs.ip; \ + (_regs)->sp = (fregs)->regs.sp; \ + (_regs)->cs = __KERNEL_CS; \ + (_regs)->flags = 0; \ + } while (0) + #define ftrace_regs_set_instruction_pointer(fregs, _ip) \ do { (fregs)->regs.ip = (_ip); } while (0) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index c871b195730c..c221c754885c 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -189,6 +189,37 @@ ftrace_partial_regs(struct ftrace_regs *fregs, struct pt_regs *regs) #endif /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS || CONFIG_HAVE_PT_REGS_TO_FTRACE_REGS_CAST */ +#ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS + +/* + * Please define arch dependent pt_regs which compatible to the + * perf_arch_fetch_caller_regs() but based on ftrace_regs. + * This requires + * - user_mode(_regs) returns false (always kernel mode). + * - able to use the _regs for stack trace. + */ +#ifndef arch_ftrace_fill_perf_regs +/* As same as perf_arch_fetch_caller_regs(), do nothing by default */ +#define arch_ftrace_fill_perf_regs(fregs, _regs) do {} while (0) +#endif + +static __always_inline struct pt_regs * +ftrace_fill_perf_regs(struct ftrace_regs *fregs, struct pt_regs *regs) +{ + arch_ftrace_fill_perf_regs(fregs, regs); + return regs; +} + +#else /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */ + +static __always_inline struct pt_regs * +ftrace_fill_perf_regs(struct ftrace_regs *fregs, struct pt_regs *regs) +{ + return &fregs->regs; +} + +#endif + /* * When true, the ftrace_regs_{get,set}_*() functions may be used on fregs. * Note: this can be true even when ftrace_get_regs() cannot provide a pt_regs.