Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2099627rwb; Mon, 7 Nov 2022 09:08:38 -0800 (PST) X-Google-Smtp-Source: AMsMyM4bKZMFoV2eumaDlp+TH1RNUsVxljp7vXSmgEbnGOjeXfqIJJ4DZoCZk+fdXnRm40JvwXsm X-Received: by 2002:a17:906:dac8:b0:741:545b:796a with SMTP id xi8-20020a170906dac800b00741545b796amr48701991ejb.240.1667840918309; Mon, 07 Nov 2022 09:08:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667840918; cv=none; d=google.com; s=arc-20160816; b=TsJUj8Xmr6VK0eYyt/9l2/zyqPo5eZLGjVVyb6hfHqzjdc76jM21685KsHY0WMi9VH y8uANOtpAn0u1mvDXgsQltZ6fFryb1a12dT8/Vc3ELBHylk7GfI+RC4GQ7naWVsKrqrp dej6m2aBbCkaYTwAJaCyykXoh/GDZYDYlzGrz91r5j33O43ox69g99BrkywxD2N3wXPr QAf5tcMfpZVeQTkB1xAiFf8g91DiJIFuUuIdR4mOL7jydEh6QFmF8/Rng8fxXSSRpllT M9da8ymeZVc8g4rh1d00nONklIAKHRutyTKPU/R0z/I9TaDf13OhcvNshKxmOYJJZg10 mPcg== 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:references:in-reply-to:subject:cc:to:from :dkim-signature; bh=9wENBGFZ1gu/VxhGgamlpajxJdLZL0OifnAzhGbGnyc=; b=HycZdZb4twIMnqkB06/JBwGIK7H0yI+jQLOanpSWpLUwefWXHZ5Z4eC7jBENlIZhva /oWknYW6KDPCXmfF2xT/X1u41d1/ZCrUF2ThcXgy20ZVF/xcRBEVB4zRLQCRXOAOYn1j CRk8zaT+KjCvULMX6mK9GW8grxvCBlnziQibXXweZL6X4lbTw8e5K495Pialm5aIgUMT 2Dx74aWwJlE3IJkkTTdlrHnNLYTBauN8NesSu7pQWmlUdbgmn/Srr5usOdU3VYmsBH5S CbKnwHQFB57gM49+skG5BeiMzAkkKCi0XlvPfe8afupwYiby+1AK25OGt3wozAYtX7Zd A8Kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uDayzqo4; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oy10-20020a170907104a00b00781df71dbc1si7998240ejb.174.2022.11.07.09.08.14; Mon, 07 Nov 2022 09:08:38 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uDayzqo4; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232824AbiKGQ4O (ORCPT + 93 others); Mon, 7 Nov 2022 11:56:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232804AbiKGQ4L (ORCPT ); Mon, 7 Nov 2022 11:56:11 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6798220C9 for ; Mon, 7 Nov 2022 08:56:10 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 16594CE1718 for ; Mon, 7 Nov 2022 16:56:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25903C433D6; Mon, 7 Nov 2022 16:56:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667840167; bh=pnyl9ZpInkkIrifXkuZxEpTl9OIUwhuPakxK4echwoU=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=uDayzqo4JFxbQbxSN4/X/creKMzVS+AWMg/Jj9ta5qzISdtF8uG4v1Ms+sAi9C3fG 5DwENLzEQyD9aAnjkbcxwJfWuhtp8RICFHy/jCO76xPu3HAPx2cvGV589EQ8A7Ikdc 1MffGd/cMLS1G5a+nZ0OR264xr+S3K5QCQnJxV97wGtSoSMJZpD6SYbYl5ldby2T0e zxENeHrkrnLtMDghJ2HDMkT2fpG2UW6YtA7rzTh9WNGarnWr7bpZuwNRR3o2rAS4R1 gJ5JrwIFy6IMbFGZTR744jEBmwmSeqomCbYqop5EOS4BKXIKavFgR1w5doHSCCibsX 0+KTS5QZuVZMQ== From: =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= To: Chen Guokai , 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: Re: [PATCH v4 6/8] riscv/kprobe: Add code to check if kprobe can be optimized In-Reply-To: <20221106100316.2803176-7-chenguokai17@mails.ucas.ac.cn> References: <20221106100316.2803176-1-chenguokai17@mails.ucas.ac.cn> <20221106100316.2803176-7-chenguokai17@mails.ucas.ac.cn> Date: Mon, 07 Nov 2022 17:56:04 +0100 Message-ID: <87v8nq1z6j.fsf@all.your.base.are.belong.to.us> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 Chen Guokai writes: [...] > diff --git a/arch/riscv/kernel/probes/opt.c b/arch/riscv/kernel/probes/op= t.c > index 6d23c843832e..876bec539554 100644 > --- a/arch/riscv/kernel/probes/opt.c > +++ b/arch/riscv/kernel/probes/opt.c [...] > /* > - * If two free registers can be found at the beginning of both > - * the start and the end of replaced code, it can be optimized > - * Also, in-function jumps need to be checked to make sure that > - * there is no jump to the second instruction to be replaced > + * The kprobe can be optimized when no in-function jump reaches to the > + * instructions replaced by optimized jump instructions(AUIPC/JALR). > */ > static bool can_optimize(unsigned long paddr, struct optimized_kprobe *o= p) > { > - return false; > + int ret; > + unsigned long addr, size =3D 0, offset =3D 0; > + struct kprobe *kp =3D get_kprobe((kprobe_opcode_t *)paddr); > + > + /* > + * Skip optimization if kprobe has been disarmed or instrumented > + * instruction support XOI. > + */ > + if (!kp || (riscv_probe_decode_insn(&kp->opcode, NULL) !=3D INSN_GOOD)) > + return false; > + > + /* > + * Find a instruction window large enough to contain a pair > + * of AUIPC/JALR, and ensure each instruction in this window > + * supports XOI. > + */ > + ret =3D search_copied_insn(paddr, op); > + if (ret) > + return false; > + > + if (!kallsyms_lookup_size_offset(paddr, &size, &offset)) > + return false; > + > + /* Check there is enough space for relative jump(AUIPC/JALR) */ > + if (size - offset <=3D op->optinsn.length) > + return false; > + > + /* > + * Decode instructions until function end, check any instruction > + * don't jump into the window used to emit optprobe(AUIPC/JALR). > + */ Don't the fixup tables need to be checked, similar to the x86 code? Bj=C3=B6rn