Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp32510rwb; Sun, 6 Nov 2022 02:05:29 -0800 (PST) X-Google-Smtp-Source: AMsMyM76M+xMeAbc8UPubE0qxV080m3yK0LJi2d10GDgHqGDMrjvf4hNBSBAVTOw8XM6rzRJ6oZZ X-Received: by 2002:a05:6a00:8ce:b0:56e:6961:c6b6 with SMTP id s14-20020a056a0008ce00b0056e6961c6b6mr15311193pfu.3.1667729128990; Sun, 06 Nov 2022 02:05:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667729128; cv=none; d=google.com; s=arc-20160816; b=tCstQIxjaeRKSpxtfSqCLDilj9M3d73BQWfRa6L4juNP3EAoye/eEJE6/Z8uKFIcZR pbyd8rtLaHtbWvcfT3PWYjq6QsdhIxMjKZuhHyxWDXDQBB/x/Dg9iX051S2qpi61kr5K Fk7+TyEbT5jI4ETCeIsUuVxiPDsPOz7xwmMWqtcgtu/5MF+gB87fWVjCuaUgIxyNaKm0 qAgabJjU0MpJHJmpDdTyyRbOBJGOOf5xFPjEiLamXkAWjLpjGDnmj93C5Up0ZlbnUAKF VppxEMGKut3tM2mysJgWR9auPzxPLwoidkhTpFbYPJF/1kciMy7U46TpqY91wY/BvJqQ dp8Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=5gu0oU8Zn/wNZgzyJlMsmp+cqVkytPNTEZsqy0iWQMQ=; b=ODIsXZpsvf7dUzRVuIBPoQ7YYCWDDegpHenVRn/LfdqBLvmX7yQxY7CW1dK9XS1T6C Z48GStTnVEnvhVxO4lIX6sdBYewlNp6h4srThXF5Ofq5FiMJCDdJBuWU1uXsRyrqD9g7 13bOecPO079MvRzJJC/NFW3fxE1ZP3Y6oGjqzBlrvVqVN+gIJx6SgEi4s29Uwl4pdrPP dSyFjkVqoLabrmAkDL3CgN8prLQapnxnAFz0htku7dafNGDkHoUjhm1R2WJD0fc62/59 HZzPi+aL0aAS7nqcX+6d8DN5t19OvM1uMibBD/Jl8GwEUgi0v9R4I5B+IYgQzEDR83xD gb4g== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r203-20020a632bd4000000b0046f87a897b5si5705478pgr.776.2022.11.06.02.05.16; Sun, 06 Nov 2022 02:05:28 -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 S229862AbiKFKD5 (ORCPT + 97 others); Sun, 6 Nov 2022 05:03:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229564AbiKFKDx (ORCPT ); Sun, 6 Nov 2022 05:03:53 -0500 Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 27051DFD1 for ; Sun, 6 Nov 2022 02:03:50 -0800 (PST) Received: from cgk-Precision-3650-Tower.. (unknown [219.141.235.82]) by APP-05 (Coremail) with SMTP id zQCowACnrKByhmdj7bRnCA--.7053S6; Sun, 06 Nov 2022 18:03:34 +0800 (CST) From: Chen Guokai To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, rostedt@goodmis.org, mingo@redhat.com, sfr@canb.auug.org.au Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, liaochang1@huawei.com, Chen Guokai Subject: [PATCH v4 2/8] riscv/kprobe: Allocate detour buffer from module area Date: Sun, 6 Nov 2022 18:03:10 +0800 Message-Id: <20221106100316.2803176-3-chenguokai17@mails.ucas.ac.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221106100316.2803176-1-chenguokai17@mails.ucas.ac.cn> References: <20221106100316.2803176-1-chenguokai17@mails.ucas.ac.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: zQCowACnrKByhmdj7bRnCA--.7053S6 X-Coremail-Antispam: 1UD129KBjvJXoW7tr1DGF4kXrWfXr43Ww47XFb_yoW8uF4UpF 4DCr45JrZ8Z3W3G3y3twn5Z34Fva95ta17KrW2vF15ZwsxJr43Aw4vg3yrXrn8tr4a9Fy5 XrW29ryS9ayUA3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmj14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2vY z4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c 02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE 4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4 IIrI8v6xkF7I0E8cxan2IY04v7MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kfnx nUUI43ZEXa7VUjbdbUUUUUU== X-Originating-IP: [219.141.235.82] X-CM-SenderInfo: xfkh0w5xrntxyrx6ztxlovh3xfdvhtffof0/1tbiBwMCE2NnTgRK2AAAsJ 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 From: Liao Chang From: Liao Chang To address the limitation of PC-relative branch instruction on riscv architecture, detour buffer slot used for optprobes is allocated from the region, 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 from kernel, the distance is half of the kernel address space (See Documentation/riscv/vm-layout.rst), hence it needs to override the alloc_optinsn_page() to make sure allocate detour buffer from jump-safe region. Signed-off-by: Liao Chang Co-developed-by: Chen Guokai Signed-off-by: Chen Guokai --- arch/riscv/kernel/probes/kprobes.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c index e6e950b7cf32..034eb7b13b3c 100644 --- a/arch/riscv/kernel/probes/kprobes.c +++ b/arch/riscv/kernel/probes/kprobes.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "decode-insn.h" @@ -84,6 +85,30 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) } #ifdef CONFIG_MMU +#if defined(CONFIG_OPTPROBES) && defined(CONFIG_64BIT) +void *alloc_optinsn_page(void) +{ + void *page; + + page = __vmalloc_node_range(PAGE_SIZE, 1, MODULES_VADDR, + MODULES_END, GFP_KERNEL, + PAGE_KERNEL, 0, NUMA_NO_NODE, + __builtin_return_address(0)); + 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; +} +#endif + void *alloc_insn_page(void) { return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, -- 2.25.1