From nobody Sun Apr 19 17:04:31 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED189C433EF for ; Wed, 29 Jun 2022 01:15:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229867AbiF2BPk (ORCPT ); Tue, 28 Jun 2022 21:15:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229717AbiF2BPe (ORCPT ); Tue, 28 Jun 2022 21:15:34 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8104A28E1A for ; Tue, 28 Jun 2022 18:15:33 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LXk500rRXzkWvG; Wed, 29 Jun 2022 09:14:12 +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, 29 Jun 2022 09:15:31 +0800 From: Liao Chang To: , , , , , CC: , Subject: [PATCH] riscv/kprobes: allocate detour buffer from module area Date: Wed, 29 Jun 2022 09:13:17 +0800 Message-ID: <20220629011317.259986-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: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To address the limitation of PC-relative branch instruction on riscv architecture, detour buffer slot is allocated from a area, the distance of which from kernel should be less than 4GB. For the time being, Modules region always live before the kernel. But Vmalloc region reside far away from kernel, the distance is half of the kernel address space. Signed-off-by: Liao Chang --- arch/riscv/kernel/probes/kprobes.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/= kprobes.c index e6e950b7cf32..bc027a663b17 100644 --- a/arch/riscv/kernel/probes/kprobes.c +++ b/arch/riscv/kernel/probes/kprobes.c @@ -6,12 +6,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include =20 #include "decode-insn.h" =20 @@ -86,10 +88,28 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) #ifdef CONFIG_MMU void *alloc_insn_page(void) { +#if defined(CONFIG_MODULES) && defined(CONFIG_64BIT) + void *page; + + page =3D module_alloc(PAGE_SIZE); + if (!page) + return NULL; + + set_vm_flush_reset_perms(page); + /* + * First make the page read-only, and only then make it executable to + * prevent it from being W+X in between. + */ + set_memory_ro((unsigned long)page, 1); + set_memory_x((unsigned long)page, 1); + + return page; +#else return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_READ_EXEC, VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, __builtin_return_address(0)); +#endif } #endif =20 --=20 2.17.1