Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp12961669rwl; Wed, 4 Jan 2023 01:12:21 -0800 (PST) X-Google-Smtp-Source: AMrXdXsMmXPhlvi6i7Eainp+S/WuHpJQ+AzvIzXPxqeLCVHupXUjMk8ut8ul+Y0kpCZ+niFKuDGU X-Received: by 2002:a17:902:f08a:b0:189:efe8:1e with SMTP id p10-20020a170902f08a00b00189efe8001emr45841271pla.68.1672823541083; Wed, 04 Jan 2023 01:12:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672823541; cv=none; d=google.com; s=arc-20160816; b=j9J9nZ6STorOpnMd1S35eR6hK1wKmArIRQy2WHlyLR5swReZ6pBsbu9R9WDkueL22D TlpXhbHLIpX0dJ/mIkhUHYJK45i3uyT0EE5z39IZ1N8TKOC0QBeL82GvBygTRjS7T03w DRALV4BVp8IPuVneyJqAHRMQbrvhpEdBwsfxD4FwZygf8jZh+v/QIKdun/qjAF4G3IYK RL9lmifG+5c9t9dn/3ikgm/wHzanAaLgG/68ABxhr0ZF/2qfyCF9TzL7NfBtxVZOHJMG 933ddAEf+ViY8JZH/GgsPBE2yCi18aFRSkHjuRQO5qywx5vAyyJcPeZtuA3d4Opjjcu3 daRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=xiOR4MVeXtFFOHmkfcxo7+/cUnQPxfor4ndqygomBKM=; b=KXqEaTVI4Y88NcJ/kRPHpk9BQoONSXLx9DheHKG5FxPKXbnCnI6YhLQtqU8mz1mBrB rVpM5rkzbEGOnSJ4OCOxh73s9GVaINtKRE5L7NG98+BcEzW0tnwj7DnHN5Ma2CT3kwQn E89EFie3cAV0llZ/nKSvD4umxsPY0eWgQp2vjoiN51X175WPLvaRGu2S5WduSTAXlSzh Esla/vpitkBSWuA8Db3+2hto+miehtIP8fZk4YM/yEYuCwmn2I4jHOFakrvCL4S6NLA9 etXxSiPGW0xWF76I7ICkdEC20CiQ4pSk98EVmDE6rOTdFq+FlpX/1p+QeDZQS3UIWTOs HVHA== 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t141-20020a635f93000000b0049e18b1b23bsi17910477pgb.138.2023.01.04.01.12.13; Wed, 04 Jan 2023 01:12:21 -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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234097AbjADIfg (ORCPT + 57 others); Wed, 4 Jan 2023 03:35:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234266AbjADIfT (ORCPT ); Wed, 4 Jan 2023 03:35:19 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B917D1A042 for ; Wed, 4 Jan 2023 00:35:18 -0800 (PST) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Nn2tx0qM5zRqny; Wed, 4 Jan 2023 16:33:45 +0800 (CST) Received: from [10.67.110.108] (10.67.110.108) 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.34; Wed, 4 Jan 2023 16:35:16 +0800 Message-ID: Date: Wed, 4 Jan 2023 16:35:15 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: [PATCH v5 6/9] riscv/kprobe: Add code to check if kprobe can be optimized To: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Chen Guokai , , , , , , CC: , References: <20221224114315.850130-1-chenguokai17@mails.ucas.ac.cn> <20221224114315.850130-7-chenguokai17@mails.ucas.ac.cn> <87sfgsvmnj.fsf@all.your.base.are.belong.to.us> From: "liaochang (A)" In-Reply-To: <87sfgsvmnj.fsf@all.your.base.are.belong.to.us> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.110.108] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,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 在 2023/1/3 2:04, Björn Töpel 写道: > Chen Guokai writes: > >> From: Liao Chang > >> diff --git a/arch/riscv/kernel/probes/opt.c b/arch/riscv/kernel/probes/opt.c >> index a0d2ab39e3fa..258a283c906d 100644 >> --- a/arch/riscv/kernel/probes/opt.c >> +++ b/arch/riscv/kernel/probes/opt.c >> @@ -271,15 +271,103 @@ static void find_free_registers(struct kprobe *kp, struct optimized_kprobe *op, >> *ra = (kw == 1UL) ? 0 : __builtin_ctzl(kw & ~1UL); >> } >> >> +static bool insn_jump_into_range(unsigned long addr, unsigned long start, >> + unsigned long end) >> +{ >> + kprobe_opcode_t insn = *(kprobe_opcode_t *)addr; >> + unsigned long target, offset = GET_INSN_LENGTH(insn); >> + >> +#ifdef CONFIG_RISCV_ISA_C >> + if (offset == RVC_INSN_LEN) { >> + if (riscv_insn_is_c_beqz(insn) || riscv_insn_is_c_bnez(insn)) >> + target = addr + rvc_branch_imme(insn); >> + else if (riscv_insn_is_c_jal(insn) || riscv_insn_is_c_j(insn)) >> + target = addr + rvc_jal_imme(insn); >> + else >> + target = 0; >> + return (target >= start) && (target < end); >> + } >> +#endif >> + >> + if (riscv_insn_is_branch(insn)) >> + target = addr + rvi_branch_imme(insn); >> + else if (riscv_insn_is_jal(insn)) >> + target = addr + rvi_jal_imme(insn); >> + else >> + target = 0; >> + return (target >= start) && (target < end); >> +} >> + >> +static int search_copied_insn(unsigned long paddr, struct optimized_kprobe *op) >> +{ >> + int i = 1; >> + unsigned long offset = GET_INSN_LENGTH(*(kprobe_opcode_t *)paddr); >> + >> + while ((i++ < MAX_COPIED_INSN) && (offset < 2 * RVI_INSN_LEN)) { >> + if (riscv_probe_decode_insn((probe_opcode_t *)paddr + offset, >> + NULL) != INSN_GOOD) > > If the second argument is NULL, and the insn is auipc, we'll splat with > NULL-ptr exception. Good catch, it is my fault to ignore the access to second argument in macro RISCV_INSN_SET_SIMULATE. > > Hmm, probe_opcode_t is u32, right? And GET_INSN_LENGTH() returns 4 or 2 > ...then the pointer arithmetic will be a mess? Hmm, This pointer arithemtic does make no sense here, i had debugged this function on QEMU step by step, and it work well. Anyway, i will go through this function again, thanks. > > > Björn -- BR, Liao, Chang