From patchwork Fri Jun 30 08:33:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13297794 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD1651FB8 for ; Fri, 30 Jun 2023 08:36:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D859C433C0; Fri, 30 Jun 2023 08:36:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1688114208; bh=nSnOgIP1ZTUTvNpA5DrwAPnY2lyaqv1+WHhbzVyevIo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KEi8SBmC/Y1AEbjsdx7tnFpyElwGHJeicLIbUB5iVizlcyxWL/NzR4YmCOmtdnZMe lN9u1zXedATAecQl8ptOkWYIwu8upzhJk3prYC56WFku85qWDa/KL4PEfFQaCP4DFs ZJO642LCJ6qhGoED4VlyI3BmmV+PApGPQioGdSlgxrel3HaF6dNw/xI3fzU1EDwOQm uWf6FUalt/5wtYNn4okLrcJpBueCl3k6kmJ0rRBs5ANz9oPymTH/oB0YaND4W33Z6N JttQvO96rfT8/5aaWOQoTSJ+s0Jj2Sx7+EBMUWlII2OjUeVSgpb8OOqz1BS+taLZIp LAsgvHhDM3v1w== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo Subject: [PATCHv3 bpf-next 15/26] libbpf: Add uprobe multi link detection Date: Fri, 30 Jun 2023 10:33:33 +0200 Message-ID: <20230630083344.984305-16-jolsa@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230630083344.984305-1-jolsa@kernel.org> References: <20230630083344.984305-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Adding uprobe-multi link detection. It will be used later in bpf_program__attach_usdt function to check and use uprobe_multi link over standard uprobe links. Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 35 +++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf_internal.h | 2 ++ 2 files changed, 37 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 06092b9752f1..4f61f9dc1748 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -4817,6 +4817,38 @@ static int probe_perf_link(void) return link_fd < 0 && err == -EBADF; } +static int probe_uprobe_multi_link(void) +{ + LIBBPF_OPTS(bpf_prog_load_opts, load_opts, + .expected_attach_type = BPF_TRACE_UPROBE_MULTI, + ); + LIBBPF_OPTS(bpf_link_create_opts, link_opts); + struct bpf_insn insns[] = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + unsigned long offset = 0; + int prog_fd, link_fd; + + prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL", + insns, ARRAY_SIZE(insns), &load_opts); + if (prog_fd < 0) + return -errno; + + /* create single uprobe on offset 0 in current process */ + link_opts.uprobe_multi.path = "/proc/self/exe"; + link_opts.uprobe_multi.offsets = &offset; + link_opts.uprobe_multi.cnt = 1; + + link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, &link_opts); + + if (link_fd >= 0) + close(link_fd); + close(prog_fd); + + return link_fd >= 0; +} + static int probe_kern_bpf_cookie(void) { struct bpf_insn insns[] = { @@ -4913,6 +4945,9 @@ static struct kern_feature_desc { [FEAT_SYSCALL_WRAPPER] = { "Kernel using syscall wrapper", probe_kern_syscall_wrapper, }, + [FEAT_UPROBE_MULTI_LINK] = { + "BPF uprobe multi link support", probe_uprobe_multi_link, + }, }; bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id) diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 7d75b92e531a..9c04b3fe1207 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -354,6 +354,8 @@ enum kern_feature_id { FEAT_BTF_ENUM64, /* Kernel uses syscall wrapper (CONFIG_ARCH_HAS_SYSCALL_WRAPPER) */ FEAT_SYSCALL_WRAPPER, + /* BPF uprobe_multi link support */ + FEAT_UPROBE_MULTI_LINK, __FEAT_CNT, };