From patchwork Wed Feb 15 09:24:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhang X-Patchwork-Id: 57450 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp91084wrn; Wed, 15 Feb 2023 01:28:46 -0800 (PST) X-Google-Smtp-Source: AK7set9Tc6t4HtVFJ47T4d6tO5YfVHKcXMuNtRQt25+XnsQGJ8wwH0KcCkea3JBHY7NFQBxATq0b X-Received: by 2002:a17:903:1c7:b0:19a:b7c0:f093 with SMTP id e7-20020a17090301c700b0019ab7c0f093mr1744832plh.54.1676453325783; Wed, 15 Feb 2023 01:28:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676453325; cv=none; d=google.com; s=arc-20160816; b=ziMLTzsgMt0Nf+7fHM31mlEqNh+0xoaiqrGBxklRsKomLx2198kyam0fkJOWx10dyN FC1GetJNpEk8YZYgTNVmhnfmWU2POY16ivFwppMKTYvNs5Bkl9BPVQoKTSc7w2fldH1t YUXk0G8gguV7gNDHombOvPrkX1a59Bl2ZY2JE6j4ndKhv041s0qr7nFWCwh//qZ3uJ+p 7/3oZZt1rNhktHP4lSCudvUHtMlZOe/Y4tZ27UtrqQyjX2sF1z388V1+jbo9eHGIMcVn aTOMOqYovHmIzbN/jEUse2ITSGCOHx/hSZJgUxz+zTApiteKlbG4Dx60VPfZMv8MJKmE jnZg== 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=dKgJBBnEjNVZGeYByJCnJz6MWpoeG2+nc0E0uPeAFzk=; b=X0r9gMYcdAGHYMooguL+eNU9t58jZmz+AVfjgL80ZLaaRPRRi5ONFEi2KEu6emU4Im A2nmux5urSmuZS3N5IQYJ3NjdrcikntuBbe6JrK0ZgE8AmDVBw1E+8CqSbMM08lpf+y4 5p8MXbWYmQKP8djrFn7AA3jKFIzw4iib63r11TY7JnHxue9eLxQIXD5LlFENm0BME1Cc oE4EEI9l0XSUAGKaPoIfV81ORLPmmBt3FQhKJeEUFb2QWZvaF3R5kRKCg5vAQQLkBObn o2lNtmp5hpE2iwDIOPNDrn76Ewu9gqsmtjcJRo2aMM4x0/vkf0eZD7KCtJ3/HNtD0B9w uuhw== 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 ik28-20020a170902ab1c00b0019aa43ead55si7306443plb.597.2023.02.15.01.28.32; Wed, 15 Feb 2023 01:28:45 -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 S233873AbjBOJZo (ORCPT + 99 others); Wed, 15 Feb 2023 04:25:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233880AbjBOJZm (ORCPT ); Wed, 15 Feb 2023 04:25:42 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 032153770B for ; Wed, 15 Feb 2023 01:25:02 -0800 (PST) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8DxldjspOxjRuAAAA--.1656S3; Wed, 15 Feb 2023 17:25:00 +0800 (CST) Received: from localhost.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8AxHuTopOxj3MczAA--.59761S2; Wed, 15 Feb 2023 17:24:56 +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 RESEND] LoongArch: ptrace: Add function argument access API Date: Wed, 15 Feb 2023 17:24:56 +0800 Message-Id: <20230215092456.30446-1-zhangqing@loongson.cn> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-CM-TRANSID: AQAAf8AxHuTopOxj3MczAA--.59761S2 X-CM-SenderInfo: x2kd0wptlqwqxorr0wxvrqhubq/ X-Coremail-Antispam: 1Uk129KBjvJXoW7Kr1kZFyfCF4fWF1DurWrAFb_yoW5JFWrpa 4kA3ZxGr48urs3uFW3J3WrZryrZrs7CrWSkr1Ik34Syr9FqryrJFWkXr1qy3WktrWUJFWI gF15t3yYgFs8Z3JanT9S1TB71UUUUjUqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU ba8YFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_JrI_Jryl8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr1l84 ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_Cr1U M2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zV CFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWrXVW3AwAv7VC2 z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxAIw2 8IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v26r1Y6r17MI8I 3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxV WUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26ryj6F1UMIIF0xvE2Ix0cI8I cVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aV AFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZE Xa7IU0NJ57UUUUU== 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?1757888722324626886?= X-GMAIL-MSGID: =?utf-8?q?1757888722324626886?= Add regs_get_argument() which returns N th argument of the function call, 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)