From patchwork Tue Mar 7 07:10:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiezhu Yang X-Patchwork-Id: 65309 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2298349wrd; Mon, 6 Mar 2023 23:48:33 -0800 (PST) X-Google-Smtp-Source: AK7set8d7eoIEC6L2SUynPt4d5p2vFouOiP3t/pwfoDdAF1k9jMniwHYXjsDZR3Ez1TUuhdiOj4r X-Received: by 2002:a17:90b:1b48:b0:23a:8f25:7fd6 with SMTP id nv8-20020a17090b1b4800b0023a8f257fd6mr11200107pjb.29.1678175312789; Mon, 06 Mar 2023 23:48:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678175312; cv=none; d=google.com; s=arc-20160816; b=OwbYyn7KVA+vTgZ/zfBILZ/2WNiOqre5BA24IDJyjSUyM97HRFUCQkH6vMAkBM2sHu aW803i29823lLVeJfASo+BF0AWo537oKWXui0+HGYb/ipzQnsTF5LXnwpRFyMj00ROF9 heukDbXVnNCmM+6rKr7SIev8BNHNXUUuvcNrFVeYWyOg3Dnn9j7ixAJFu0QyjMUByr4X rG00f1V29dZptHdq/xv7R+85lVC1BGv1Q90VfSgtSKPGg1ZO/Y3/JrU5uwtOKUGNeScT GoGPNnWixZSbhAkhiIhN/CMhF6Lzio28SVFQaP+3g9hRaMbCUNzNnukIMetnXFMuvSF2 ttQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=8D/SGpYsCQ0Ltp8uJLdPJEz6Ex+v/6Ku0QG12bA88ck=; b=lMG2HQ50yCQvrudSm9IMDHeh0RVUyxJR62bFfun9fpMhSfbUuS7YVZXdgjg53ZPPkE GtVhu1L5bABIrVVVFdn2FXYEvYVnMSFH/JvTrEqyD7VZ4vlT77Fe5FAJmbzs9Vk/gpEN g+R6h3BPj2GHaavgGkzJXulR0f0BlrZMhWpFF8tmg1TA4yNC40LpTq04Yh97NnIXWJC6 /EmO/Shmgc8l5TnRbT/fwG/Y5IBChIij+Megsefr17GeAMY+AMK0iQTDGTJD9Mk/dx7V 8dfJY+yRpod5Tvknx8Pj5mM0+7om+/NQXa1dgkii+6HWO9KPnkcSFk44AB3ykscjC8dy ClRQ== 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 l11-20020a17090a4d4b00b00233f3034302si13149781pjh.46.2023.03.06.23.48.17; Mon, 06 Mar 2023 23:48:32 -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 S230225AbjCGHKK (ORCPT + 99 others); Tue, 7 Mar 2023 02:10:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229815AbjCGHKI (ORCPT ); Tue, 7 Mar 2023 02:10:08 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8B835303EB for ; Mon, 6 Mar 2023 23:10:05 -0800 (PST) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8BxedlL4wZkwj8JAA--.11679S3; Tue, 07 Mar 2023 15:10:03 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Cxf+RJ4wZkk7hNAA--.35219S2; Tue, 07 Mar 2023 15:10:01 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , WANG Xuerui Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, loongson-kernel@lists.loongnix.cn Subject: [PATCH] LoongArch: Add support for function error injection Date: Tue, 7 Mar 2023 15:10:01 +0800 Message-Id: <1678173001-16852-1-git-send-email-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.1.0 X-CM-TRANSID: AQAAf8Cxf+RJ4wZkk7hNAA--.35219S2 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBjvJXoWxAF4kKr47Jr13Zr13GFWktFb_yoWrJr1kpF 9IywnrGrWkWr4xJr9IyFyrZFyvvFs7CFW29F1Ika4rCrW7Zr1ktr4kKryqqF48G3yUGrW0 g3WrGr43tF45CaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b7kYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_JrI_Jryl8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVWUJVWUCwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwA2z4 x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6xkF7I0E14v26F4UJVW0owAS 0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zVCFFI0UMc02F40EFcxC0V AKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1l Ox8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxAIw28IcxkI7VAKI48JMxC20s 026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_ JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14 v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xva j40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWUJV W8JbIYCTnIWIevJa73UjIFyTuYvjxUzZ2-UUUUU 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?1759694356498639265?= X-GMAIL-MSGID: =?utf-8?q?1759694356498639265?= Inspired by the commit 42d038c4fb00 ("arm64: Add support for function error injection") and commit ee55ff803b38 ("riscv: Add support for function error injection"), support function error injection for LoongArch. Mainly implement two functions: (1) regs_set_return_value() which is used to overwrite the return value, (2) override_function_with_return() which is used to override the probed function returning and jump to its caller. Here is a simple test under CONFIG_FUNCTION_ERROR_INJECTION and CONFIG_FAIL_FUNCTION: # echo sys_clone > /sys/kernel/debug/fail_function/inject # echo 100 > /sys/kernel/debug/fail_function/probability # dmesg bash: fork: Invalid argument # dmesg ... FAULT_INJECTION: forcing a failure. name fail_function, interval 1, probability 100, space 0, times 1 ... Call Trace: [<90000000002238f4>] show_stack+0x5c/0x180 [<90000000012e384c>] dump_stack_lvl+0x60/0x88 [<9000000000b1879c>] should_fail_ex+0x1b0/0x1f4 [<900000000032ead4>] fei_kprobe_handler+0x28/0x6c [<9000000000230970>] kprobe_breakpoint_handler+0xf0/0x118 [<90000000012e3e60>] do_bp+0x2c4/0x358 [<9000000002241924>] exception_handlers+0x1924/0x10000 [<900000000023b7d0>] sys_clone+0x0/0x4 [<90000000012e4744>] do_syscall+0x7c/0x94 [<9000000000221e44>] handle_syscall+0xc4/0x160 Signed-off-by: Tiezhu Yang Acked-by: Masami Hiramatsu (Google) Tested-by: Hengqi Chen --- arch/loongarch/Kconfig | 1 + arch/loongarch/include/asm/ptrace.h | 6 ++++++ arch/loongarch/lib/Makefile | 2 ++ arch/loongarch/lib/error-inject.c | 10 ++++++++++ 4 files changed, 19 insertions(+) create mode 100644 arch/loongarch/lib/error-inject.c diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index 7fd5125..b16ff7e 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -99,6 +99,7 @@ config LOONGARCH select HAVE_FAST_GUP select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_ARG_ACCESS_API + select HAVE_FUNCTION_ERROR_INJECTION 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 d761db9..db7332a 100644 --- a/arch/loongarch/include/asm/ptrace.h +++ b/arch/loongarch/include/asm/ptrace.h @@ -154,6 +154,12 @@ static inline long regs_return_value(struct pt_regs *regs) return regs->regs[4]; } +static inline void regs_set_return_value(struct pt_regs *regs, + unsigned long val) +{ + regs->regs[4] = val; +} + #define instruction_pointer(regs) ((regs)->csr_era) #define profile_pc(regs) instruction_pointer(regs) diff --git a/arch/loongarch/lib/Makefile b/arch/loongarch/lib/Makefile index 40bde63..30b1595 100644 --- a/arch/loongarch/lib/Makefile +++ b/arch/loongarch/lib/Makefile @@ -5,3 +5,5 @@ lib-y += delay.o memset.o memcpy.o memmove.o \ clear_user.o copy_user.o dump_tlb.o unaligned.o + +obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o diff --git a/arch/loongarch/lib/error-inject.c b/arch/loongarch/lib/error-inject.c new file mode 100644 index 0000000..afc9e1c --- /dev/null +++ b/arch/loongarch/lib/error-inject.c @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +void override_function_with_return(struct pt_regs *regs) +{ + instruction_pointer_set(regs, regs->regs[1]); +} +NOKPROBE_SYMBOL(override_function_with_return);