Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp250736pxt; Wed, 4 Aug 2021 21:46:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQbMMlmB8ZLAPJn0Tzal/fKphMvJxtn2HwfQiS57WBuTc6f3uEM+q/aFKdcrCGRWV1vVIU X-Received: by 2002:a17:906:616:: with SMTP id s22mr2818255ejb.210.1628138785283; Wed, 04 Aug 2021 21:46:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628138785; cv=none; d=google.com; s=arc-20160816; b=eheuruNCyYToi5Nj5keLJO0OxaJc9+O2b/bi/y61WXprSWxVdzfHOYJl5ScZheyKxq psNMXz0tr/RKK+LSof1U08B7tsqeA4NUXqxo8fldM/AhA1nVwmM2+nsUy2qiahoXtJI5 21CU0et/LWdnzqzNPqLQubzKPbpaufGQI+oOHUbL3qG2N8jyuJEiGIZUG73YmngpW9rq Lmbv0Xy7aQTmJcnANMjI5SxzaTOYDQQF1j0f2u0WnqjcDkcV6kBqQ5h3DhFOvROu8Pkr 7an2z+3s4YPp7qOdVHnZFAuoKNvzezhyeOT4v5Xcd+Pa+Nsdw6stLnKalBwAkBZcXV2z CjKQ== 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:subject:cc:to:from:date :dkim-signature; bh=HCTGktV1H3fdO2/AbZVJW+K+J9EjugyQxdNUv6AOmgE=; b=P91RO6NESK+lDJi68jkrs38oGo34/WoPFzN0DvBTnK2swBe7vG8ViAivAzuqq+Tb4h 1Vpva7NbxbjOkHmKCytjyM33JpLBJb4GxnJtXZg+mi2zu8bB3OELt/l+AKkMYJVOcV2d 4clLWWwfmWor0JWTnOvEA4XJEnXlEBFKYnzxz8822lVyL6QPwG0O89oHGO70+khx5xdD 5eJCIJqNNx8izzp6FuMLK/rqfOXr0EQ8m11pvuvGHqyDdqMWardwJP612zU7HUBDLCjH CEEVCUGA5GVnywu9L2f6uyMA62IXUmlXnc4YzrfXzKlumwGR405X/Wb+OMPR7t2oJikO U5zA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tQ7AT61f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d16si4234884edy.450.2021.08.04.21.46.01; Wed, 04 Aug 2021 21:46:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tQ7AT61f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S238013AbhHEByS (ORCPT + 99 others); Wed, 4 Aug 2021 21:54:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:42562 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231771AbhHEByR (ORCPT ); Wed, 4 Aug 2021 21:54:17 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 900DA60E8D; Thu, 5 Aug 2021 01:54:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628128444; bh=EflziLK5J7hb6yFpmywsTa0MRmkbjdSW5CkBDkwVI5I=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=tQ7AT61f977/v5yXPGjbyRrFaTCE7b43r0jooiF1h8JwAUXv1U9g/oGdmxB5tFkDo 1xaK9laAci97J/mjZtdp2Fek2mzJaOri+pVOOOJm03UO/alohnNgxis6cgF6ridMMd 07laeAFoYJYNAa4pd0JL+gSt2lh/iEXbbVb3AuORkVDbWjIJJYtrLgmPQgL1xDOW44 0j81oZMoH96LQEQpimOhVozWQEqzzV8vHVImNEipGiGK9OQfx3US2dQCQBPTc61aV1 b6vXxJSOrCWotWrolog+WG8RbN2WkmrR9xiegTtG1D5U/8A/d4iFPuCn2bTippkmG1 zdRIVjSv6yPig== Date: Thu, 5 Aug 2021 10:54:01 +0900 From: Masami Hiramatsu To: Qi Liu Cc: , , , , , , , , , , , Subject: Re: [PATCH v2] arm64: kprobe: Enable OPTPROBE for arm64 Message-Id: <20210805105401.4acd3217c566b4e3933f355c@kernel.org> In-Reply-To: <20210804060209.95817-1-liuqi115@huawei.com> References: <20210804060209.95817-1-liuqi115@huawei.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 4 Aug 2021 14:02:09 +0800 Qi Liu wrote: > This patch introduce optprobe for ARM64. In optprobe, probed > instruction is replaced by a branch instruction to detour > buffer. Detour buffer contains trampoline code and a call to > optimized_callback(). optimized_callback() calls opt_pre_handler() > to execute kprobe handler. > > Limitations: > - We only support !CONFIG_RANDOMIZE_MODULE_REGION_FULL case to > guarantee the offset between probe point and kprobe pre_handler > is not larger than 128MiB. > > Performance of optprobe on Hip08 platform is test using kprobe > example module[1] to analyze the latency of a kernel function, > and here is the result: > > [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/kprobes/kretprobe_example.c > > kprobe before optimized: > [280709.846380] do_empty returned 0 and took 1530 ns to execute > [280709.852057] do_empty returned 0 and took 550 ns to execute > [280709.857631] do_empty returned 0 and took 440 ns to execute > [280709.863215] do_empty returned 0 and took 380 ns to execute > [280709.868787] do_empty returned 0 and took 360 ns to execute > [280709.874362] do_empty returned 0 and took 340 ns to execute > [280709.879936] do_empty returned 0 and took 320 ns to execute > [280709.885505] do_empty returned 0 and took 300 ns to execute > [280709.891075] do_empty returned 0 and took 280 ns to execute > [280709.896646] do_empty returned 0 and took 290 ns to execute > [280709.902220] do_empty returned 0 and took 290 ns to execute > [280709.907807] do_empty returned 0 and took 290 ns to execute > > optprobe: > [ 2965.964572] do_empty returned 0 and took 90 ns to execute > [ 2965.969952] do_empty returned 0 and took 80 ns to execute > [ 2965.975332] do_empty returned 0 and took 70 ns to execute > [ 2965.980714] do_empty returned 0 and took 60 ns to execute > [ 2965.986128] do_empty returned 0 and took 80 ns to execute > [ 2965.991507] do_empty returned 0 and took 70 ns to execute > [ 2965.996884] do_empty returned 0 and took 70 ns to execute > [ 2966.002262] do_empty returned 0 and took 80 ns to execute > [ 2966.007642] do_empty returned 0 and took 70 ns to execute > [ 2966.013020] do_empty returned 0 and took 70 ns to execute > [ 2966.018400] do_empty returned 0 and took 70 ns to execute > [ 2966.023779] do_empty returned 0 and took 70 ns to execute > [ 2966.029158] do_empty returned 0 and took 70 ns to execute > > Signed-off-by: Qi Liu > > --- > > Changes since V1: > - Address the comments from Masami, checks for all branch instructions, and > use aarch64_insn_patch_text_nosync() instead of aarch64_insn_patch_text() > in each probe. Is it safe for the multicore system? If it is safe because it modifies just one instruction (modifying 32bit in atomic), I understand it. BTW, anyway, you should use _nosync() variant in arch_prepare_optimized_kprobe() too, beacause the optprobe insn buffer is not touched until the probed instruction is optimized by br. [...] > +int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *orig) > +{ > + kprobe_opcode_t *code; > + u32 insn; > + int ret, i; > + void *addrs[TMPL_END_IDX]; > + void *addr; > + > + code = get_optinsn_slot(); > + if (!code) > + return -ENOMEM; > + > + if (!is_offset_in_range((unsigned long)code, > + (unsigned long)orig->addr + 8)) > + goto error; > + > + if (!is_offset_in_range((unsigned long)code + TMPL_CALL_BACK, > + (unsigned long)optimized_callback)) > + goto error; > + > + if (!is_offset_in_range((unsigned long)&code[TMPL_RESTORE_END], > + (unsigned long)op->kp.addr + 4)) > + goto error; > + > + /* Setup template */ > + for (i = 0; i < TMPL_END_IDX; i++) > + addrs[i] = code + i; > + > + ret = aarch64_insn_patch_text(addrs, optprobe_template_entry, > + TMPL_END_IDX); You should use aarch64_insn_patch_text_nosync() here (and all the aarch64_insn_patch_text() in this function too), because the insn buffer must not executed until the probe point is optimized. > + if (ret < 0) > + goto error; > + > + /* Set probe information */ > + addr = code + TMPL_VAL_IDX; > + insn = (unsigned long long)op & 0xffffffff; > + aarch64_insn_patch_text(&addr, &insn, 1); > + > + addr = addr + 4; > + insn = ((unsigned long long)op & GENMASK_ULL(63, 32)) >> 32; > + aarch64_insn_patch_text(&addr, &insn, 1); > + > + addr = code + TMPL_CALL_BACK; > + insn = aarch64_insn_gen_branch_imm((unsigned long)addr, > + (unsigned long)optimized_callback, > + AARCH64_INSN_BRANCH_LINK); > + aarch64_insn_patch_text(&addr, &insn, 1); > + > + /* The original probed instruction */ > + addr = code + TMPL_RESTORE_ORIGN_INSN; > + insn = orig->opcode; > + aarch64_insn_patch_text(&addr, &insn, 1); > + > + /* Jump back to next instruction */ > + addr = code + TMPL_RESTORE_END; > + insn = aarch64_insn_gen_branch_imm( > + (unsigned long)(&code[TMPL_RESTORE_END]), > + (unsigned long)(op->kp.addr) + 4, > + AARCH64_INSN_BRANCH_NOLINK); > + aarch64_insn_patch_text(&addr, &insn, 1); > + > + flush_icache_range((unsigned long)code, > + (unsigned long)(&code[TMPL_END_IDX])); > + /* Set op->optinsn.insn means prepared. */ > + op->optinsn.insn = code; > + > + return 0; Thank you, -- Masami Hiramatsu