Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1109381pxb; Tue, 8 Feb 2022 09:27:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJxd0uJUTR6Irp0shq3N0arIVUQSsDfFZ4dBQAIyftvSbzjPgkJXkG2/4vt1lqdR5ijlPcLX X-Received: by 2002:a17:907:3ea9:: with SMTP id hs41mr4760842ejc.727.1644341222801; Tue, 08 Feb 2022 09:27:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644341222; cv=none; d=google.com; s=arc-20160816; b=iyRATAaPDCQhuH2KrvZ65S9DhIvvHbo42xDef0bOpzt+g7yRhKYZMkL89Y+koI250m nCeFfnDFqBl7+Ewvl59dE8E8aYsyp4CT4G9prWbTsJcxX+DAznMsTi9tUfergC1VBec0 /dmEdAoiE/rqbtOy3M7+vvksEbSyAAWSs+Se8vlDpRHepSyMsAjX/DS9so7wOjUUAeaf EgsA2R30DzgIroc/akLFtaKHcys10NszqplqMBqtUWJyQ4ioPWWZCNML5L87x7Am6zGD 1qgOr4PuZC4I/+FUrEAiw90o71+sdBXZUfiIHwOFIzEYdf1Zh8ZJnL4A0m00cPUYdLA6 GNqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=p5GJUhqoBZKVBO/8R0d/5vUo6i8ZBWVBkYqNLYhElO8=; b=iIisHXXVzdclZy6QGBtXmV/gxDUmVoQJBxa2QFA95NO8dPdjTfv/aqsaKZmsveZV4N xNpW61cC3KNVE06K67Qh2HzYEfP19kBz2IV6a+Tlz47xgyjzA3KFAPVdVh9c9AswvBYh g+Dabk7ml/iRHf8bLsVnlGKdW9kysF292Blqw2DAa0etCf3pl4T8Aful6suAGvrWBpPU iZgmx/apuBkS/bx455GqB+JVYh86YipsNzpjov4iEPAn41Srk3svVx+++9Ly+4vn4Tkb JoYsYsApoGXj0J2XxdWVNbQRxYM69fSQUOcwP0cOCpa1zEFXA6MpC51IBzbO3wW1Ss+e WAog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ChvNyJeb; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e21si7436302edc.657.2022.02.08.09.26.37; Tue, 08 Feb 2022 09:27:02 -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=@gmail.com header.s=20210112 header.b=ChvNyJeb; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236513AbiBGTAq (ORCPT + 99 others); Mon, 7 Feb 2022 14:00:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235237AbiBGS7b (ORCPT ); Mon, 7 Feb 2022 13:59:31 -0500 Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2ABF2C0401E2; Mon, 7 Feb 2022 10:59:30 -0800 (PST) Received: by mail-io1-xd2e.google.com with SMTP id m185so9836199iof.10; Mon, 07 Feb 2022 10:59:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=p5GJUhqoBZKVBO/8R0d/5vUo6i8ZBWVBkYqNLYhElO8=; b=ChvNyJebeZEzCBm65NSTxDwcm0+EoeNZz6fnhElk4Q1fex4qX0HSUngNK8fXpIW7cT nfJYNhBXgUzj1QkXSvQimCYQCP0zGkhhhkXRhRd5idBnx8rF18VZBtQFped0iSMPseXb PqdcH2bxvDbWMzsqY1b+GqogMpHKEyW7H+5hYklzo6nyyEby6z6o5H/9BGtVmmN4Avyt 404hInAjcVVqnpbJR065oYJIML9+F8KoBPSru7wm4p1tMAV6ztfs08dzzCKjjeUdi9ld bnEGsMpJXCk4r2BsZ6r+qcmmGZTxs8XZGeNtWFE7Daux86vbTcly6OjAHzbKWYZ6H6rH Mfbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=p5GJUhqoBZKVBO/8R0d/5vUo6i8ZBWVBkYqNLYhElO8=; b=F0sRPcNtwYSjcRt3wFwocrvdd06wobZbpj+WeBFGFLBQ9Vx4Nrlb+f1yx+coFUD3Wt MEQNkM0Io6PXKxx3YDkQNyuX63vyEiDyl3UkddwPt2XJKPrcQg/tv22g5B36Z/4VXp2M 3YnMRL8aZyQKYNsJCr8eO5yTE1lhLtC74zPMEMfEPjy8ITVuD/OMolckOB5fbQe/Aqdl 7/lhjGkHpWdn81FCHO+c56xZG8jqi09VjaLIdOKMGa9z9KLU/1M7xrH9CnnXPltVpqJq 6D/VyHOX2oT4XGbKWY2ybHtuV5M8yb3yPR257tMdJlxiGx0lICQlcwWeHz7MbI+Rcbz/ uaVA== X-Gm-Message-State: AOAM532uWTUnhSMzGIIQmp1Gh9zvu0TztLC9aCo6bGbAgTJbJ/5GMEtb TpSBhoA/0l+eyHsxNLwHdkOx/4bPOclMxU4ymOeyHjOf X-Received: by 2002:a5e:a806:: with SMTP id c6mr449118ioa.112.1644260369518; Mon, 07 Feb 2022 10:59:29 -0800 (PST) MIME-Version: 1.0 References: <20220202135333.190761-1-jolsa@kernel.org> <20220202135333.190761-3-jolsa@kernel.org> In-Reply-To: <20220202135333.190761-3-jolsa@kernel.org> From: Andrii Nakryiko Date: Mon, 7 Feb 2022 10:59:18 -0800 Message-ID: Subject: Re: [PATCH 2/8] bpf: Add bpf_get_func_ip kprobe helper for fprobe link To: Jiri Olsa Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu , Networking , bpf , lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Wed, Feb 2, 2022 at 5:53 AM Jiri Olsa wrote: > > Adding support to call get_func_ip_fprobe helper from kprobe > programs attached by fprobe link. > > Also adding support to inline it, because it's single load > instruction. > > Signed-off-by: Jiri Olsa > --- > kernel/bpf/verifier.c | 19 ++++++++++++++++++- > kernel/trace/bpf_trace.c | 16 +++++++++++++++- > 2 files changed, 33 insertions(+), 2 deletions(-) > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index 1ae41d0cf96c..a745ded00635 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -13625,7 +13625,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env) > continue; > } > > - /* Implement bpf_get_func_ip inline. */ > + /* Implement tracing bpf_get_func_ip inline. */ > if (prog_type == BPF_PROG_TYPE_TRACING && > insn->imm == BPF_FUNC_get_func_ip) { > /* Load IP address from ctx - 16 */ > @@ -13640,6 +13640,23 @@ static int do_misc_fixups(struct bpf_verifier_env *env) > continue; > } > > + /* Implement kprobe/fprobe bpf_get_func_ip inline. */ > + if (prog_type == BPF_PROG_TYPE_KPROBE && > + eatype == BPF_TRACE_FPROBE && > + insn->imm == BPF_FUNC_get_func_ip) { > + /* Load IP address from ctx (struct pt_regs) ip */ > + insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, > + offsetof(struct pt_regs, ip)); Isn't this architecture-specific? I'm starting to dislike this inlining whole more and more. It's just a complication in verifier without clear real-world benefits. We are clearly prematurely optimizing here. In practice you'll just call bpf_get_func_ip() once and that's it. Function call overhead will be negligible compare to other *userful* work you'll be doing in your BPF program. > + > + new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); > + if (!new_prog) > + return -ENOMEM; > + > + env->prog = prog = new_prog; > + insn = new_prog->insnsi + i + delta; > + continue; > + } > + > patch_call_imm: > fn = env->ops->get_func_proto(insn->imm, env->prog); > /* all functions that have prototype and verifier allowed > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c > index a2024ba32a20..28e59e31e3db 100644 > --- a/kernel/trace/bpf_trace.c > +++ b/kernel/trace/bpf_trace.c > @@ -1036,6 +1036,19 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = { > .arg1_type = ARG_PTR_TO_CTX, > }; > > +BPF_CALL_1(bpf_get_func_ip_fprobe, struct pt_regs *, regs) > +{ > + /* This helper call is inlined by verifier. */ > + return regs->ip; > +} > + > +static const struct bpf_func_proto bpf_get_func_ip_proto_fprobe = { > + .func = bpf_get_func_ip_fprobe, > + .gpl_only = false, > + .ret_type = RET_INTEGER, > + .arg1_type = ARG_PTR_TO_CTX, > +}; > + > BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) > { > struct bpf_trace_run_ctx *run_ctx; > @@ -1279,7 +1292,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) > return &bpf_override_return_proto; > #endif > case BPF_FUNC_get_func_ip: > - return &bpf_get_func_ip_proto_kprobe; > + return prog->expected_attach_type == BPF_TRACE_FPROBE ? > + &bpf_get_func_ip_proto_fprobe : &bpf_get_func_ip_proto_kprobe; > case BPF_FUNC_get_attach_cookie: > return &bpf_get_attach_cookie_proto_trace; > default: > -- > 2.34.1 >