Message ID | 20221106100316.2803176-3-chenguokai17@mails.ucas.ac.cn |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1420236wru; Sun, 6 Nov 2022 02:04:53 -0800 (PST) X-Google-Smtp-Source: AMsMyM72Gghd0M7FqD8/abIGzovbCyVFe+zP6qPYp26KGGr2ertJUj88bd1spI1Ny/ciz6JgiSfd X-Received: by 2002:a63:5a44:0:b0:431:fa3a:f92c with SMTP id k4-20020a635a44000000b00431fa3af92cmr39512311pgm.471.1667729092761; Sun, 06 Nov 2022 02:04:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667729092; cv=none; d=google.com; s=arc-20160816; b=TT8078HHD5ejtcxtBB5HGsbb8X/TgWiS+Rnrg689LIJz5IodFr0npCzEVMs5WqzPiA +PLf60Xm+B7Kwcq/VcfXE0qtoYIpOdlzGH2lxXEs7bxDyE7nOsc7pzc7UMQyxfW7TxST 6Gmai/q7EhZ+zH/funGcRhCYjjsoqxN3Oq+9/u+AWFShtBFhEofMEGKTQeM0aHO62a0u YiWrxJeWZhAbtPm9ij64tMRIzIhWdkYYdnw/5z2TVXJyDNPFjfe5tF+a+O3lXi1IiADf vemWFGRdxQc8L7Y7FY3np0UXboIKMdZ3+YX/fAER6pElKhowb1UVYy5Lbrd26nyR43Tc ZH5A== 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 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.04.40; Sun, 06 Nov 2022 02:04:52 -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 <rfc822;hjfbswb@gmail.com> + 99 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 <rfc822;linux-kernel@vger.kernel.org>); 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 <linux-kernel@vger.kernel.org>; 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 <chenguokai17@mails.ucas.ac.cn> 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 <chenguokai17@mails.ucas.ac.cn> 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748740701352011643?= X-GMAIL-MSGID: =?utf-8?q?1748740701352011643?= |
Series |
Add OPTPROBES feature on RISCV
|
|
Commit Message
Xim
Nov. 6, 2022, 10:03 a.m. UTC
From: Liao Chang <liaochang1@huawei.com> From: Liao Chang <liaochang1@huawei.com> 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 <liaochang1@huawei.com> Co-developed-by: Chen Guokai <chenguokai17@mails.ucas.ac.cn> Signed-off-by: Chen Guokai <chenguokai17@mails.ucas.ac.cn> --- arch/riscv/kernel/probes/kprobes.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
Comments
On Sun, 6 Nov 2022 18:03:10 +0800 Chen Guokai <chenguokai17@mails.ucas.ac.cn> wrote: > @@ -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); FYI, the above combination is going to be going away: https://lore.kernel.org/all/Y10OyLCLAAS6rsZv@hirez.programming.kicks-ass.net/ -- Steve > + > + return page; > +} > +#endif > +
在 2022/11/17 9:25, Steven Rostedt 写道: > On Sun, 6 Nov 2022 18:03:10 +0800 > Chen Guokai <chenguokai17@mails.ucas.ac.cn> wrote: > >> @@ -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); > > FYI, the above combination is going to be going away: > > https://lore.kernel.org/all/Y10OyLCLAAS6rsZv@hirez.programming.kicks-ass.net/ Thanks for reminding, i will use this API in next revision. > -- Steve > > >> + >> + return page; >> +} >> +#endif >> + >
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 <asm/cacheflush.h> #include <asm/bug.h> #include <asm/patch.h> +#include <asm/set_memory.h> #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,