From patchwork Wed Sep 7 02:33:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Liao, Chang" X-Patchwork-Id: 12968377 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2170AECAAA1 for ; Wed, 7 Sep 2022 02:37:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:CC :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=1GjJGZYmfbZIJfF1f32ahQRP6kQ/rXD09uHUbg6A50E=; b=rO6nHv+5zc/Epq ZtwWote6rwQXTtkKwe8edW6TfGT/buAqsaD7nKf+b3NQMFmiTOV6yv0leVv5mVLjhS+MQ79QBn1tE AOXP8MPK+95NN8N8ioROMP2e4GSJFCdcytGcLwqWNqq/Nvfv5zIsgUe+XPL95BXkKkaW9QJo6jCRo Zxx8bqHxaWmvmF+wI/Cti//gSUBJHUUnQECLxf1IgCimeOhqp1YdFziylhLvlxvX4mRntzilhOpJK uk8U6Gap+Z15GSYKRkPC7hqxknhqODcAcQNTeXDua7dWJIBKvgG9P87kdB0N2Hed+MLvqaZ80pgz1 2IARe21yifFJ81k9/kmw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oVkw9-001fay-Lr; Wed, 07 Sep 2022 02:37:09 +0000 Received: from szxga03-in.huawei.com ([45.249.212.189]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oVkw6-001fU2-Ek for linux-riscv@lists.infradead.org; Wed, 07 Sep 2022 02:37:08 +0000 Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.56]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4MMmYs6df3zHnlQ; Wed, 7 Sep 2022 10:34:57 +0800 (CST) Received: from huawei.com (10.67.174.53) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Wed, 7 Sep 2022 10:36:51 +0800 From: Liao Chang To: , , , , , CC: , Subject: [PATCH] riscv/kprobe: Optimize the performance of patching instruction slot Date: Wed, 7 Sep 2022 10:33:27 +0800 Message-ID: <20220907023327.85630-1-liaochang1@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.67.174.53] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220906_193706_829383_615ED63E X-CRM114-Status: UNSURE ( 7.09 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Since no race condition occurs on each instruction slot, hence it is safe to patch instruction slot without stopping machine. Signed-off-by: Liao Chang --- arch/riscv/kernel/probes/kprobes.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c index e6e950b7cf32..eff7d7fab535 100644 --- a/arch/riscv/kernel/probes/kprobes.c +++ b/arch/riscv/kernel/probes/kprobes.c @@ -24,12 +24,14 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *); static void __kprobes arch_prepare_ss_slot(struct kprobe *p) { unsigned long offset = GET_INSN_LENGTH(p->opcode); + const kprobe_opcode_t brk_insn = __BUG_INSN_32; + kprobe_opcode_t slot[MAX_INSN_SIZE]; p->ainsn.api.restore = (unsigned long)p->addr + offset; - patch_text(p->ainsn.api.insn, p->opcode); - patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset), - __BUG_INSN_32); + memcpy(slot, &p->opcode, offset); + memcpy((void *)((unsigned long)slot + offset), &brk_insn, 4); + patch_text_nosync(p->ainsn.api.insn, slot, offset + 4); } static void __kprobes arch_prepare_simulate(struct kprobe *p)