Message ID | 20221030090141.2550837-1-chenguokai17@mails.ucas.ac.cn |
---|---|
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 l7csp1719971wru; Sun, 30 Oct 2022 02:43:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6y/cOH+5/AD0Lrj7evCegYpAl/u8cjblytN3z52puQmnYYdeDBKIuOceaQgPLDQ1r39Aqy X-Received: by 2002:aa7:c58a:0:b0:461:fc07:a821 with SMTP id g10-20020aa7c58a000000b00461fc07a821mr8261128edq.19.1667123022597; Sun, 30 Oct 2022 02:43:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667123022; cv=none; d=google.com; s=arc-20160816; b=Qd9TZEW+kVR90sx06rHIfZuDj13vjBIK0MyZ06q5QZZkezZC0cLqOVN5muM8PdgA5e cp9PaUlgkhQCxR9sgInlGM2MjLq+Df3IlNBnOwgdUi1TH2x2Rgjrl9sWDOBCeZIJBAwS I87knX6NLMD4bBIH4bLxYuQQiNq6Jl/4p2JfL6tOQdVJGfnGlQEDgLi78tKgeoYBqm5K n0fDwphgHPSllT5FOxcyCDMsbqjhzd19S/sGtYChFHq51MYtnG+lWdmGaRGDEzDDoUay Nwq8NB+ZrUwnGdkLRPFO6NMU8h+LGMs6nys8/cR+/7UsM4YnFeIsEPkVh4T6J78YUjPC mcRg== 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 :message-id:date:subject:cc:to:from; bh=ZaavVXE2szzA2qMlJ+yvtE/Jc6pXGjNvUHk7e5yGHkU=; b=Bluz1m/9gqQtarArwwthJf41Camzm1xCBrIa5hLaduCidTZmtdBmkhw2fbVT4jlbGc VSBqFaYEUejRHeK9x/EWG7aux2/EdplunabI0w9q0xTb7W7x2EGyA1EBSZiV3y8OKNUB m17nFY3aXMpsthD3G4HN6Ho1j44FE3VeEwnw9guE7aamcnka064ru1dh4bwZQoD5hvYG ESpd+L6OG4DrFJYdkTB+kdD8ANd4oWhCM/qT0zsJJtygd8fiTVvlXm69qrbi8eFX7s41 GWQ+zIDRKYZnAeIY0VodTfw+Ly1FS/zpk6lTA705yQ2dKhQcNxkC28+tqNPduq+vpxaS G9rA== 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 dr22-20020a170907721600b0078315844537si4599038ejc.378.2022.10.30.02.43.19; Sun, 30 Oct 2022 02:43:42 -0700 (PDT) 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 S229835AbiJ3JXG (ORCPT <rfc822;paulgraves1991@gmail.com> + 99 others); Sun, 30 Oct 2022 05:23:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229781AbiJ3JWx (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 30 Oct 2022 05:22:53 -0400 Received: from cstnet.cn (smtp23.cstnet.cn [159.226.251.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 48591D137 for <linux-kernel@vger.kernel.org>; Sun, 30 Oct 2022 02:22:35 -0700 (PDT) Received: from cgk-Precision-3650-Tower.. (unknown [219.141.235.82]) by APP-03 (Coremail) with SMTP id rQCowABXCVmKPV5jkxYmBw--.33365S4; Sun, 30 Oct 2022 17:02:02 +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 Subject: [PATCH v3 0/8] Add OPTPROBES feature on RISCV Date: Sun, 30 Oct 2022 17:01:33 +0800 Message-Id: <20221030090141.2550837-1-chenguokai17@mails.ucas.ac.cn> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: rQCowABXCVmKPV5jkxYmBw--.33365S4 X-Coremail-Antispam: 1UD129KBjvJXoWxXw18Zry7Zw1DWFyDZr43KFg_yoWrJry7pF WkKw45CrWUJrs3CrW3Jw4kuFySyan3Gw43ur1UJ34rX3y3JrySy3Zagay5ZF13GF4a9w1j qrn09ryvkFy5A3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvFb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I 8E87Iv6xkF7I0E14v26rxl6s0DM2vYz4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x2 0xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18Mc Ij6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l c2xSY4AK67AK6r4fMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I 0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWU tVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcV CY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAF wI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvj xUyPl8UUUUU X-Originating-IP: [219.141.235.82] X-CM-SenderInfo: xfkh0w5xrntxyrx6ztxlovh3xfdvhtffof0/1tbiCQMPE2NeHVkoSAAAsP 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: <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?1748105190587124558?= X-GMAIL-MSGID: =?utf-8?q?1748105190587124558?= |
Series |
Add OPTPROBES feature on RISCV
|
|
Message
Xim
Oct. 30, 2022, 9:01 a.m. UTC
From: Liao Chang <liaochang1@huawei.com>
Add jump optimization support for RISC-V.
Replaces ebreak instructions used by normal kprobes with an
auipc+jalr instruction pair, at the aim of suppressing the probe-hit
overhead.
All known optprobe-capable RISC architectures have been using a single
jump or branch instructions while this patch chooses not. RISC-V has a
quite limited jump range (4KB or 2MB) for both its branch and jump
instructions, which prevent optimizations from supporting probes that
spread all over the kernel.
Auipc-jalr instruction pair is introduced with a much wider jump range
(4GB), where auipc loads the upper 12 bits to a free register and jalr
Deaconappends the lower 20 bits to form a 32 bit immediate. Note that
returns from probe handler requires another free register. As kprobes
can appear almost anywhere inside the kernel, the free register should
be found in a generic way, not depending on calling convention or any
other regulations.
The algorithm for finding the free register is inspired by the register
renaming in modern processors. From the perspective of register renaming,
a register could be represented as two different registers if two neighbour
instructions both write to it but no one ever reads. Extending this fact,
a register is considered to be free if there is no read before its next
write in the execution flow. We are free to change its value without
interfering normal execution.
Static analysis shows that 51% instructions of the kernel (default config)
is capable of being replaced i.e. one free register can be found at both
the start and end of replaced instruction pairs while the replaced
instructions can be directly executed.
Contribution:
Chen Guokai invents the algorithm of searching free register, evaluated
the ratio of optimizaion, the basic function for RVI kernel binary.
Liao Chang adds the support for hybrid RVI and RVC kernel binary, fix
some bugs with different kernel configurations, refactor out entire
feature into some individual patches.
v3:
1. Support of hybrid RVI and RVC kernel binary.
2. Refactor out entire feature into some individual patches.
v2:
1. Adjust comments
2. Remove improper copyright
3. Clean up format issues that is no common practice
4. Extract common definition of instruction decoder
5. Fix race issue in SMP platform.
v1:
Chen Guokai contribute the basic functionality code.
Liao Chang (8):
riscv/kprobe: Prepare the skeleton to implement RISCV OPTPROBES
feature
riscv/kprobe: Allocate detour buffer from module area
riscv/kprobe: Prepare the skeleton to prepare optimized kprobe
riscv/kprobe: Add common RVI and RVC instruction decoder code
riscv/kprobe: Search free register(s) to clobber for 'AUIPC/JALR'
riscv/kprobe: Add code to check if kprobe can be optimized
riscv/kprobe: Prepare detour buffer for optimized kprobe
riscv/kprobe: Patch AUIPC/JALR pair to optimize kprobe
arch/riscv/Kconfig | 1 +
arch/riscv/include/asm/bug.h | 5 +-
arch/riscv/include/asm/kprobes.h | 48 ++
arch/riscv/include/asm/patch.h | 1 +
arch/riscv/kernel/patch.c | 22 +-
arch/riscv/kernel/probes/Makefile | 1 +
arch/riscv/kernel/probes/decode-insn.h | 145 ++++++
arch/riscv/kernel/probes/kprobes.c | 25 +
arch/riscv/kernel/probes/opt.c | 559 ++++++++++++++++++++++
arch/riscv/kernel/probes/opt_trampoline.S | 137 ++++++
arch/riscv/kernel/probes/simulate-insn.h | 41 ++
11 files changed, 980 insertions(+), 5 deletions(-)
create mode 100644 arch/riscv/kernel/probes/opt.c
create mode 100644 arch/riscv/kernel/probes/opt_trampoline.S