From patchwork Sat Feb 11 08:44:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhang X-Patchwork-Id: 55758 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1401496wrn; Sat, 11 Feb 2023 00:50:34 -0800 (PST) X-Google-Smtp-Source: AK7set9svE7kencEnCHMkYVShILlLJwx+5Tm1uLb7ZyhLMdMoXDXZTr/GM7jMPIRqF0GFoQVKqHL X-Received: by 2002:a17:90a:ae0e:b0:232:cdb6:c4ad with SMTP id t14-20020a17090aae0e00b00232cdb6c4admr9241256pjq.0.1676105434584; Sat, 11 Feb 2023 00:50:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676105434; cv=none; d=google.com; s=arc-20160816; b=rv1geFcRHFdquKYEmXCE3Aj2lt7FNigbRObStKrA6LAxHjeqCnXIMiHCt9WVjyoYZY b9235T46Cj+e9jxljzQ5/VttpPHcn1PKWuSqLirLQEWSZHhJGUQVs+q6cZBHEvpIamyu 1b70uwYPw/HHagu9qlqeadQCjAKnzRDbpfPpmNCwawXFUtN9o4HG1nMI5grDXx3fb02S Ix/jVSudsCbMVyHFhQHYmt/gOfAivi64eZk88puDO894fZnCyfC1H9op4/8qeXJdhZcw 8iMOPM/fiJxNbzJvRs42DvJK6STJSwcggH2TwaaO4hsJv4owfUvcpc0QO1bRRNqzi45l QnQw== 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 :message-id:date:subject:cc:to:from; bh=3A58Fnmgh+5U94PilL2nrzlZtTqvt7paTIGTg1d4baw=; b=mJYrGAtLdnVphbsPjamF0MwqoCQCNFwEdkNyDNr/ZJny8rjLM388BukLhh60pUvHXb tA5y6DBH8LPJr1EjihzUm/BfSpePxh5xBbt1aKnwoU9Fbc6h5OhvgkOTsOD47YtuX0hW wCRcSBvmbNTXTuCHa8rD5KGYTEiR6OCNYZZufxMjD9HNrRNoxrZDXlIVGw+kWEZluQDZ TCapWr5ySCuiT89kWMioTpxeVqs1eN7E1YvSdFihLFNP0ToIzyOsMH0wmiONNLzoCPX9 tFTq/59HPkkPCoMp8Dgt29rkiEVqQlCrk5lSwQ/ndUILjVyQW2QH/qgYNP7hRlYmm1mX XiFg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a18-20020a17090a481200b0022bfa01f0d7si6137477pjh.155.2023.02.11.00.50.22; Sat, 11 Feb 2023 00:50:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229735AbjBKIot (ORCPT + 99 others); Sat, 11 Feb 2023 03:44:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229801AbjBKIos (ORCPT ); Sat, 11 Feb 2023 03:44:48 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 916ED1ABCE for ; Sat, 11 Feb 2023 00:44:20 -0800 (PST) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8Ax3epjVedjuzERAA--.34355S3; Sat, 11 Feb 2023 16:44:19 +0800 (CST) Received: from localhost.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Cxf+RjVedjXv8wAA--.57308S2; Sat, 11 Feb 2023 16:44:19 +0800 (CST) From: Qing Zhang To: Huacai Chen , Oleg Nesterov , WANG Xuerui Cc: Jiaxun Yang , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] LoongArch: ptrace: Add function argument access API Date: Sat, 11 Feb 2023 16:44:14 +0800 Message-Id: <20230211084414.25998-1-zhangqing@loongson.cn> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Cxf+RjVedjXv8wAA--.57308S2 X-CM-SenderInfo: x2kd0wptlqwqxorr0wxvrqhubq/ X-Coremail-Antispam: 1Uk129KBjvJXoWxWrWftryfGFW5ZF47KFyxuFg_yoW5JFWDpa 4kA3ZxGr48urs3uFW3J3WrZryrZrs7CrWakr1Ik34Syr9FqryrJFWkXr1qy3WktrWUJFWI gF15t3yYgFs8Z3JanT9S1TB71UUUUjUqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b3xYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_Jrv_JF1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVW8JVW5JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4 x0Y4vEx4A2jsIE14v26F4j6r4UJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UM2kK e7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zVCFFI 0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUtVWrXwAv7VC2z280 aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxAIw28Icx kI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v26r1Y6r17MI8I3I0E 5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAV WUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1I6r4UMIIF0xvE2Ix0cI8IcVCY 1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI 0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7I U8EeHDUUUUU== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757523931690162647?= X-GMAIL-MSGID: =?utf-8?q?1757523931690162647?= Add regs_get_argument() which returns N th argument of the function call, t show This enables ftrace kprobe events to access kernel function arguments via $argN syntax. eg: echo 'p bio_add_page arg1=$arg1' > kprobe_events bash: echo: write error: Invalid argument Signed-off-by: Qing Zhang --- arch/loongarch/Kconfig | 1 + arch/loongarch/include/asm/ptrace.h | 33 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index e3eba2eb4b44..103046966893 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -97,6 +97,7 @@ config LOONGARCH select HAVE_EXIT_THREAD select HAVE_FAST_GUP select HAVE_FTRACE_MCOUNT_RECORD + select HAVE_FUNCTION_ARG_ACCESS_API select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FUNCTION_TRACER select HAVE_GENERIC_VDSO diff --git a/arch/loongarch/include/asm/ptrace.h b/arch/loongarch/include/asm/ptrace.h index 66a0e6c480a3..f6ffcc00753c 100644 --- a/arch/loongarch/include/asm/ptrace.h +++ b/arch/loongarch/include/asm/ptrace.h @@ -120,6 +120,39 @@ static inline long regs_return_value(struct pt_regs *regs) return regs->regs[4]; } +/** + * regs_get_kernel_argument() - get Nth function argument in kernel + * @regs: pt_regs of that context + * @n: function argument number (start from 0) + * + * regs_get_argument() returns @n th argument of the function call. + * Note that this chooses most probably assignment, in some case + * it can be incorrect. + * This is expected to be called from kprobes or ftrace with regs + * where the top of stack is the return address. + */ +static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs, + unsigned int n) +{ + static const unsigned int argument_offs[] = { + offsetof(struct pt_regs, regs[4]), + offsetof(struct pt_regs, regs[5]), + offsetof(struct pt_regs, regs[6]), + offsetof(struct pt_regs, regs[7]), + offsetof(struct pt_regs, regs[8]), + offsetof(struct pt_regs, regs[9]), + offsetof(struct pt_regs, regs[10]), + offsetof(struct pt_regs, regs[11]), +#define NR_REG_ARGUMENTS 8 + }; + + if (n >= NR_REG_ARGUMENTS) { + n -= NR_REG_ARGUMENTS; + return regs_get_kernel_stack_nth(regs, n); + } else + return regs_get_register(regs, argument_offs[n]); +} + #define instruction_pointer(regs) ((regs)->csr_era) #define profile_pc(regs) instruction_pointer(regs)