Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp1824163rwj; Sat, 24 Dec 2022 04:11:42 -0800 (PST) X-Google-Smtp-Source: AMrXdXuJkAIk35WFaWXZQ8ww0zmi++dvXnSQSCLHzBTojZrqTTbWdoEepS71JgthulY/zgYZ/4VC X-Received: by 2002:a17:902:f211:b0:190:c219:ae22 with SMTP id m17-20020a170902f21100b00190c219ae22mr11934961plc.53.1671883902313; Sat, 24 Dec 2022 04:11:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671883902; cv=none; d=google.com; s=arc-20160816; b=MqCiJEqAQrxM8a/6uwMjScOuQDAJHmD7/R7iUUggnoLjHD79n19yZ4y6TgSLVZoosG jgnBmxIfpJYDb6Y4nCfGYPsetK27RWm/ixrp0evw4wNkCutoKNS4ruMWvVH1UBx0ol+Q kng+zINVBqISIOaD2r0eBy5hgnoz2nIFRYsLfEIH5rEa6haa3rCU9Pw3dVj8LWYmvqMY ncKCFvc8UByF8GebDhqGzm6y9jVqgUru1e8ko0RzRDmJzlam3kpnosPZMJyDlSndZZDH hf3AXDddQm0flWqVh0iOjF4ZhPpPKl8pHjrSoQcehcdgWkMcvAAR7hPyqcqWmmU9l7A6 cTeg== 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=3IsEzlhkrMdHskuImv7kPB8VMQAdUdFZhOgEBvuR6iI=; b=IEr1t3SdlA+Py60BEQk4OWMCAm2aFlvvjR780Gmiv9nz5pLAMF7rcYPq/hJXmtfaWx QcMpAZTE6Tl9f2GEI1wVsEaiE8czup7S9bwxUbqrHwiC7I8cGPWmIGFoolzgE61OzM89 vlzLTfybWmIOllX+yP+oBueWPhaSeTZoOSI+6THfOocfyJQdRCmHRyCERbjIAfUu4dts M7ITJte0yOKspw3MFhAZkdeoQ9q1zynoqUszj12E2exftgHM9i1zIWGhfQ9aXVyta1ls MSBtFnVGoJ4dMElp58fLnpb1Abm1m6Ooa4E9THK0OyV+hPtQHA5gUpvHSlySP57jfy5E nhAA== 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 y20-20020a634954000000b0047780dc6a58si6161062pgk.500.2022.12.24.04.11.33; Sat, 24 Dec 2022 04:11:42 -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 S231175AbiLXLnr (ORCPT + 64 others); Sat, 24 Dec 2022 06:43:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229570AbiLXLnq (ORCPT ); Sat, 24 Dec 2022 06:43:46 -0500 Received: from cstnet.cn (smtp23.cstnet.cn [159.226.251.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 16B9A9FD3 for ; Sat, 24 Dec 2022 03:43:42 -0800 (PST) Received: from cgk-Precision-3650-Tower.. (unknown [219.141.235.82]) by APP-03 (Coremail) with SMTP id rQCowABXXpbf5aZj9dVkCA--.18955S6; Sat, 24 Dec 2022 19:43:27 +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 v5 2/9] riscv/kprobe: Allocate detour buffer from module area Date: Sat, 24 Dec 2022 19:43:08 +0800 Message-Id: <20221224114315.850130-3-chenguokai17@mails.ucas.ac.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224114315.850130-1-chenguokai17@mails.ucas.ac.cn> References: <20221224114315.850130-1-chenguokai17@mails.ucas.ac.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: rQCowABXXpbf5aZj9dVkCA--.18955S6 X-Coremail-Antispam: 1UD129KBjvJXoW7ur1kJFWrKrWfCrW7Wr15urg_yoW8urW3pF 4DCr45JrZ8Aw13G3yayw1kZr9YvaykXw47KrW2vF1UZw47tr43Zw4v934Fqrn5trW29FyY qr4q9ryS9ayUZaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmK14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6xkF7I0E14v26F4UJVW0owAa c4AC62xK8xCEY4vEwIxC4wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzV Aqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S 6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxw ACI402YVCY1x02628vn2kIc2xKxwCY02Avz4vE14v_Gw4l42xK82IYc2Ij64vIr41l4I8I 3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxV WUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAF wI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcI k0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j 6r4UJbIYCTnIWIevJa73UjIFyTuYvjfUjSdgDUUUU X-Originating-IP: [219.141.235.82] X-CM-SenderInfo: xfkh0w5xrntxyrx6ztxlovh3xfdvhtffof0/1tbiAwEKE2OmnOhnLgAAsj X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 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 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c index f21592d20306..e1856b04db04 100644 --- a/arch/riscv/kernel/probes/kprobes.c +++ b/arch/riscv/kernel/probes/kprobes.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -84,6 +85,29 @@ 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_rox((unsigned long)page, 1); + + return page; +} +#endif + void *alloc_insn_page(void) { return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, -- 2.34.1