From patchwork Fri Jun 2 08:52:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13264886 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 89ED4C8EC for ; Fri, 2 Jun 2023 08:52:51 +0000 (UTC) Received: from mail-yw1-x112b.google.com (mail-yw1-x112b.google.com [IPv6:2607:f8b0:4864:20::112b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8599170A for ; Fri, 2 Jun 2023 01:52:49 -0700 (PDT) Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-568ba7abc11so18509957b3.3 for ; Fri, 02 Jun 2023 01:52:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685695969; x=1688287969; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=i/SQ2Fo2T0bYItWrPhzxcl37HvHH4TS9C3sENSUKJow=; b=ZkbIglzF1auDWMgWho4gjayZGvTckYf2LX/8uV8AnUnEcMNywS8Jv4KAaX4JnVUpCg AucbyovAN4vbdCx6PABmc9dOkShwB/bMyasPROUoYY656rXt4Q8LLNfK4It/jmskanco 4kR5tu28teS6wW3W6jRFlVbADdEDxw0mNOv1wkx1yZDgAX/7jHMRWHTzNPZ2T79KqYto NqsCSl88bikC8SH552YSs6/Xn1jWJ1nS6IjY7Q2RJlpnWVNkd7B5c5X9UMFOJWHA5/wf Z98uIgrkufVcbKzgWlOKT8pDiCrNCGi52o4v7iQsADMcK9rssTX9CmKsku8xw8We9+m8 wZbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685695969; x=1688287969; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i/SQ2Fo2T0bYItWrPhzxcl37HvHH4TS9C3sENSUKJow=; b=L5P8PwStVLo3f+X2UBkcg79pNS9AswBytsvsYYEQ+crKaRBr6duQ43hTUAIFxN76k9 UYRV1WzrGU+RMn6/KtpVX1O0mLlnCv4Y4rULN7s3kU6DzSCS5TYhlO838PdiB6llJnrc I1mamPsjPVLJqP4GHg5/aUebZFkMXfInFaXtlsVO3ACcT7yDm568NyxUU+P/b/EdLZEC 4Zwye4LaH2ivPcagI4D/nLK+JN9r4OwcdEtflODo2bxFQ0ch4WgQ8lZCu8IBSWHBC5HY J23awgzxPBUNy96v3C/ONywkByAB0eI9o5ZvVzgDx8a784oNZyZ9fiaRupnIDfHePgHF MPgQ== X-Gm-Message-State: AC+VfDx6XWhjQ7s5GeOWuKYH1J6b4z6qV8Tl6K3DBmT5V1NRV/a9/qJW mWadcHT//3ufgwavO2IB1zo= X-Google-Smtp-Source: ACHHUZ4fFXS6vx6f+h4QcnloXwMig6qlTw9CkZPjQetIj1TaCQLhRGtp8POsc5o8Gt6qYjodnrZcKQ== X-Received: by 2002:a0d:d913:0:b0:561:b7bb:9cc with SMTP id b19-20020a0dd913000000b00561b7bb09ccmr11658640ywe.16.1685695968937; Fri, 02 Jun 2023 01:52:48 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5401:1e90:5400:4ff:fe75:fb5d]) by smtp.gmail.com with ESMTPSA id b123-20020a0dd981000000b00565c29cf592sm289828ywe.10.2023.06.02.01.52.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 01:52:48 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 1/6] bpf: Support ->fill_link_info for kprobe_multi Date: Fri, 2 Jun 2023 08:52:34 +0000 Message-Id: <20230602085239.91138-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230602085239.91138-1-laoar.shao@gmail.com> References: <20230602085239.91138-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net By adding support for ->fill_link_info to the kprobe_multi link, users will be able to inspect it using `bpftool link show`. This enhancement will expose both the count of probed functions and their respective addresses to the user. Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 4 ++++ kernel/trace/bpf_trace.c | 26 ++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 4 ++++ 3 files changed, 34 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a7b5e91..22c8168 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6438,6 +6438,10 @@ struct bpf_link_info { __s32 priority; __u32 flags; } netfilter; + struct { + __u64 addrs; + __u32 count; + } kprobe_multi; }; } __attribute__((aligned(8))); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 2bc41e6..ec53bc9 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2548,9 +2548,35 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) kfree(kmulti_link); } +static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link, + struct bpf_link_info *info) +{ + u64 *uaddrs = (u64 *)u64_to_user_ptr(info->kprobe_multi.addrs); + struct bpf_kprobe_multi_link *kmulti_link; + u32 ucount = info->kprobe_multi.count; + + if (!uaddrs ^ !ucount) + return -EINVAL; + + kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); + if (!uaddrs) { + info->kprobe_multi.count = kmulti_link->cnt; + return 0; + } + + if (!ucount) + return 0; + if (ucount != kmulti_link->cnt) + return -EINVAL; + if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64))) + return -EFAULT; + return 0; +} + static const struct bpf_link_ops bpf_kprobe_multi_link_lops = { .release = bpf_kprobe_multi_link_release, .dealloc = bpf_kprobe_multi_link_dealloc, + .fill_link_info = bpf_kprobe_multi_link_fill_link_info, }; static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index a7b5e91..22c8168 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6438,6 +6438,10 @@ struct bpf_link_info { __s32 priority; __u32 flags; } netfilter; + struct { + __u64 addrs; + __u32 count; + } kprobe_multi; }; } __attribute__((aligned(8))); From patchwork Fri Jun 2 08:52:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13264887 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 3AA7014266 for ; Fri, 2 Jun 2023 08:52:52 +0000 (UTC) Received: from mail-yw1-x112a.google.com (mail-yw1-x112a.google.com [IPv6:2607:f8b0:4864:20::112a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FA9E170C for ; Fri, 2 Jun 2023 01:52:50 -0700 (PDT) Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-568af2f6454so18413737b3.1 for ; Fri, 02 Jun 2023 01:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685695969; x=1688287969; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xoDDmMH61IBCnDqgz7C9NGBec5DAwaQUjkpv1nwzXHc=; b=Ou/cIdETxr1MRABUdYjGgSp9mkYeXnuvMfKBXqQcJRxtZsxV8gEUipxOtJ5z7Lhpqt mLaD5AXzNbGRQfg4TbPspYPFAUNRrCJaFxq7lR/CMPl8lS2znYRtNaEuQkV3SlAX+m7e kcNupg+BetpYCDw9v3RyR51d358WU1cULxOmtO54o0iRDjUzjW67Fc9CXzj56TkGnPh4 fCOVKzh1JXh+ZAuNylAhr4ILjUZEQd0CrZhUscAK0USuFgNfv75DrCDxCsrA1HjkX/ps isqYclpViIn5fKWNsvKN65/Qo1newzIbvdGp3dP0vYAiQTapmDsOD6zwbM85bLhMY/YH AfVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685695969; x=1688287969; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xoDDmMH61IBCnDqgz7C9NGBec5DAwaQUjkpv1nwzXHc=; b=JY+dJ2JXp2XHQda7m9GKUTjIzzmmzx4kZRqlUXy8XcL5vKlpLJ1hqdIzoEzASZ1GTM DTAHU8Ar6Zr1KK1/qI9ZBvgZ3UQc2ocQ6lxGCTLjXUHedssf2ZqlabLT7Q2ne7NHc5H3 8DpJgkbCM+bMUwCVD5MsFFrcjVgx4K63wTCYGejLPz1xtnQP54XKF/0LP3iZ8vu3cR4I 8KTpnjevTQawII++iW6EjNCiax60y3g83M2bDeTYn4oDj2hDjrcZ/zn0QeHM71RjaGS7 9S2+FagMEbbK4b8Yvx0pVecrJkVMtLTXDf/HCXlUun+PzTv51lWX6zX6+B2zUmAyEvgF BpEw== X-Gm-Message-State: AC+VfDz7N5CkIFAzMd+K9UZBXcsWSIaXMUhKMLcrEQaDnEkh+zYoJ2Cd Hg42fPrAzPSCFN19uWYmBrc= X-Google-Smtp-Source: ACHHUZ4sa9Ir5h8cVvqYyJBKPObI3uGhobpREMU/y8DJDT/jdUTihGtiZcag1XALJ2/WSY5rsLCS4Q== X-Received: by 2002:a0d:d1c7:0:b0:55a:574f:327c with SMTP id t190-20020a0dd1c7000000b0055a574f327cmr11363480ywd.13.1685695969627; Fri, 02 Jun 2023 01:52:49 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5401:1e90:5400:4ff:fe75:fb5d]) by smtp.gmail.com with ESMTPSA id b123-20020a0dd981000000b00565c29cf592sm289828ywe.10.2023.06.02.01.52.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 01:52:49 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 2/6] bpftool: Show probed function in kprobe_multi link info Date: Fri, 2 Jun 2023 08:52:35 +0000 Message-Id: <20230602085239.91138-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230602085239.91138-1-laoar.shao@gmail.com> References: <20230602085239.91138-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net Show the already expose kprobe_multi link info in bpftool. The result as follows, $ tools/bpf/bpftool/bpftool link show 1: kprobe_multi prog 5 func_cnt 4 addrs symbols ffffffffb4d465b0 schedule_timeout_interruptible ffffffffb4d465f0 schedule_timeout_killable ffffffffb4d46630 schedule_timeout_uninterruptible ffffffffb4d46670 schedule_timeout_idle pids trace(8729) $ tools/bpf/bpftool/bpftool link show -j [{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}] Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 2d78607..3b00c07 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -166,6 +166,57 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info) return err; } +static int cmp_u64(const void *A, const void *B) +{ + const __u64 *a = A, *b = B; + + return *a - *b; +} + +static void kprobe_multi_print_plain(__u64 addr, char *sym, __u32 indent) +{ + printf("\n\t%*s %0*llx %s", indent, "", 16, addr, sym); +} + +static void kprobe_multi_print_json(__u64 addr, char *sym) +{ + jsonw_start_object(json_wtr); + jsonw_uint_field(json_wtr, "addr", addr); + jsonw_string_field(json_wtr, "symbol", sym); + jsonw_end_object(json_wtr); +} + +static void kernel_syms_show(const __u64 *addrs, __u32 cnt, __u32 indent) +{ + char buff[256], sym[256]; + __u64 addr; + int i = 0; + FILE *fp; + + fp = fopen("/proc/kallsyms", "r"); + if (!fp) + return; + + /* Each address is guaranteed to be unique. */ + qsort((void *)addrs, cnt, sizeof(__u64), cmp_u64); + /* The addresses in /proc/kallsyms are already sorted. */ + while (fgets(buff, sizeof(buff), fp)) { + if (sscanf(buff, "%llx %*c %s", &addr, sym) != 2) + continue; + /* The addr probed by kprobe_multi is always in + * /proc/kallsyms, so we can ignore some edge cases. + */ + if (addr != addrs[i]) + continue; + if (indent) + kprobe_multi_print_plain(addr, sym, indent); + else + kprobe_multi_print_json(addr, sym); + i++; + } + fclose(fp); +} + static int show_link_close_json(int fd, struct bpf_link_info *info) { struct bpf_prog_info prog_info; @@ -218,6 +269,17 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) jsonw_uint_field(json_wtr, "map_id", info->struct_ops.map_id); break; + case BPF_LINK_TYPE_KPROBE_MULTI: + const __u64 *addrs; + + jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count); + jsonw_name(json_wtr, "addrs"); + jsonw_start_array(json_wtr); + addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs); + if (info->kprobe_multi.count) + kernel_syms_show(addrs, info->kprobe_multi.count, 0); + jsonw_end_array(json_wtr); + break; default: break; } @@ -396,6 +458,20 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) case BPF_LINK_TYPE_NETFILTER: netfilter_dump_plain(info); break; + case BPF_LINK_TYPE_KPROBE_MULTI: + __u32 indent, cnt, i; + const __u64 *addrs; + + cnt = info->kprobe_multi.count; + if (!cnt) + break; + printf("\n\tfunc_cnt %d %-16s %s", cnt, "addrs", "symbols"); + for (i = 0; cnt; i++) + cnt /= 10; + indent = strlen("func_cnt ") + i; + addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs); + kernel_syms_show(addrs, cnt, indent); + break; default: break; } @@ -417,7 +493,9 @@ static int do_show_link(int fd) { struct bpf_link_info info; __u32 len = sizeof(info); + __u64 *addrs = NULL; char buf[256]; + int count; int err; memset(&info, 0, sizeof(info)); @@ -441,12 +519,28 @@ static int do_show_link(int fd) info.iter.target_name_len = sizeof(buf); goto again; } + if (info.type == BPF_LINK_TYPE_KPROBE_MULTI && + !info.kprobe_multi.addrs) { + count = info.kprobe_multi.count; + if (count) { + addrs = calloc(count, sizeof(__u64)); + if (!addrs) { + p_err("mem alloc failed"); + close(fd); + return -1; + } + info.kprobe_multi.addrs = (unsigned long)addrs; + goto again; + } + } if (json_output) show_link_close_json(fd, &info); else show_link_close_plain(fd, &info); + if (addrs) + free(addrs); close(fd); return 0; } From patchwork Fri Jun 2 08:52:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13264888 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 C60D214266 for ; Fri, 2 Jun 2023 08:52:52 +0000 (UTC) Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A04041712 for ; Fri, 2 Jun 2023 01:52:51 -0700 (PDT) Received: by mail-qk1-x72b.google.com with SMTP id af79cd13be357-75b04e897a5so203405285a.2 for ; Fri, 02 Jun 2023 01:52:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685695970; x=1688287970; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QuzOX4XFKmJOiLjt9d1z+UYL8/sI/TO2hkUeE9dOaVY=; b=LVD7CSdNnL7X02JLfuOVQ4+gw4VcarH4cp/1Z3rRjhG+rnPnAb8C8VSXOljDgPFrtP BW5YeGOUshi9VPlm49dnU5lWcHCQAJVflylJCaJnHXMAwswgLLc2/Is1/Nqky3QLQWQH dHPuKFieY7JIMEEQjKKNLjzEktj+ZQRseSk2NcPWVOr3MdSA5WVmeMQSxzQ9JO8WqWfr +W1qYkXjv1sH0UeLnI9MxG/ZkuknIdBwwTxa4SJ0tdf/ToBhQedMzW2056ch/ByyAP40 +O/dvovzG5sLVjlz6kLIU7CaOhqPkDWwp7tpzYbfzBtXz6OPHBcaLB0+6TOQ/bJfUyNN hTUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685695970; x=1688287970; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QuzOX4XFKmJOiLjt9d1z+UYL8/sI/TO2hkUeE9dOaVY=; b=XMU/0PLdoQxebKqWNxtQjWR7YY0QUT23m/JWtVP9yIoXbA7Oh5BJGohg80u0MP96Si mT2y0Ze01galrP/CwgYpive5gmb5HQB50a/swjendQFUtlNNnna0rg+vt0S9ABalCn+U Ku1lI7c83WscdNEJ0Ld4m+flg++x7uKtruoRTFd10K55vkvq4j1Hw5e18B2pEoJM+xcQ ehLW3LWcusx5VwC1SiytURsjWGI4A07Oun0hhyxnoGBs8r99859i1Ko+sbE5oJPIbIP6 NLPZop6CNzFdMcJ64Yo3a+V5WuWiT/outfOD0vz01yL785rq1xnqpISsq4i5odby8wCG kdyQ== X-Gm-Message-State: AC+VfDz1QVQQL8LaLAO4obHbC85DZ7VEDvm5iy+apzKWOnREBoFIiDYf BPjPKzb/WBk1zRUi0h5HBZk= X-Google-Smtp-Source: ACHHUZ6n4s0wClGcQ8hYIIRNWI8mGYD5Ncf6q/MfzqH0H7hvLVsQHwn+QL8KSJYwDkWnb7NzW7TP+g== X-Received: by 2002:a05:620a:8a02:b0:75b:23a1:35fc with SMTP id qt2-20020a05620a8a0200b0075b23a135fcmr11178123qkn.13.1685695970383; Fri, 02 Jun 2023 01:52:50 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5401:1e90:5400:4ff:fe75:fb5d]) by smtp.gmail.com with ESMTPSA id b123-20020a0dd981000000b00565c29cf592sm289828ywe.10.2023.06.02.01.52.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 01:52:49 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 3/6] bpf: Always expose the probed address Date: Fri, 2 Jun 2023 08:52:36 +0000 Message-Id: <20230602085239.91138-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230602085239.91138-1-laoar.shao@gmail.com> References: <20230602085239.91138-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net Since different symbols can share the same name, it is insufficient to only expose the symbol name. It is essential to also expose the symbol address so that users can accurately identify which one is being probed. Signed-off-by: Yafang Shao --- kernel/trace/trace_kprobe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 59cda19..a7a905a 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -1547,7 +1547,7 @@ int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type, if (tk->symbol) { *symbol = tk->symbol; *probe_offset = tk->rp.kp.offset; - *probe_addr = 0; + *probe_addr = (unsigned long)tk->rp.kp.addr; } else { *symbol = NULL; *probe_offset = 0; From patchwork Fri Jun 2 08:52:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13264889 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 69C9214266 for ; Fri, 2 Jun 2023 08:52:53 +0000 (UTC) Received: from mail-yw1-x1130.google.com (mail-yw1-x1130.google.com [IPv6:2607:f8b0:4864:20::1130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F299EE51 for ; Fri, 2 Jun 2023 01:52:51 -0700 (PDT) Received: by mail-yw1-x1130.google.com with SMTP id 00721157ae682-565e6beb7aaso18569827b3.2 for ; Fri, 02 Jun 2023 01:52:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685695971; x=1688287971; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yl+AK0T9hur52OaYcgWIsqNGxbmTBsbkE2leUjsComM=; b=fis+lOWYVR+Qp8IvYCxWvE1IVqwMqxkgtsANnwb2gJnrQaXOjpW9Ah/WnC50ADMv0R xd5421CFpLkTpBI9aHvShM9ykeDZEwpSr5tBhcQ7DiIrWGzMDo1rBk5hAe3LK3eCPjqS IZsDKpGgrC0YUNlHTTEHWVetROLVOZzyxfnXwWDy7dGKKgjX5xaEU80tBF3ZlnPwYVcL iqOZLaTM85/bqFg/NmJY3AMERY5RH5wTvrsIxsy1csIyjtw2HpEOSQxXi+JzN8qcQmuV cmj2KI3RuXjgrI1FJVFXMydHpralWsQQGatJtBiC/hUQNR/Rd+ivkIag16sV5TNVr4xt IuqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685695971; x=1688287971; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yl+AK0T9hur52OaYcgWIsqNGxbmTBsbkE2leUjsComM=; b=WhHmxUCHGfuCiZEuX7QejkkwVi0WTG6Sf3QaOeTKQcyoMHKeGM0ylwwGDjlpy0lZOa JRWbvoxgkkilVKp/62M76QxL71s1/KqO8zsiXhxmrZMAmnmZVSSsPfUa9wAVznfbMeKF o9CiFWPguloNASKvjL9Lcon9Ok+S64lppq2OuqhudGraFCpT1CnNeVKqJB/Lyk4ICrlC yUMbmg8vDtrenirVEn8p8jurDAfBgawRJrnAjfKUJv7HF98Q1Ks8QvA4c38KSmBQ/tUB nCmABQXo4Xuiv+5i12VjAYD1Kqzo4BNpyTc2vWWcfFYzCjzhFhb0YHRYGucrzPGCwYhc 6cNg== X-Gm-Message-State: AC+VfDyiUiNAlCo7iyZNYtbS+Yu0IhI5N5iAZk0lnNIN72wkJSj1DW6U PZr0C+ZbHdFwtOhSkZGiJiw= X-Google-Smtp-Source: ACHHUZ5av1D8Jv3An+z/Zik4XlBO5Z8K+qyV+my4hxX9ldfvZNHWdqPpW1P+a3kjJH+SM4G7V1M2bg== X-Received: by 2002:a0d:d64e:0:b0:565:c966:60dd with SMTP id y75-20020a0dd64e000000b00565c96660ddmr12751515ywd.48.1685695971193; Fri, 02 Jun 2023 01:52:51 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5401:1e90:5400:4ff:fe75:fb5d]) by smtp.gmail.com with ESMTPSA id b123-20020a0dd981000000b00565c29cf592sm289828ywe.10.2023.06.02.01.52.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 01:52:50 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 4/6] bpf: Add a common helper bpf_copy_to_user() Date: Fri, 2 Jun 2023 08:52:37 +0000 Message-Id: <20230602085239.91138-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230602085239.91138-1-laoar.shao@gmail.com> References: <20230602085239.91138-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net Add a common helper bpf_copy_to_user(), which will be used at multiple places. Signed-off-by: Yafang Shao --- kernel/bpf/syscall.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 92a57ef..80c9ec0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3234,6 +3234,25 @@ static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, raw_tp_link->btp->tp->name); } +static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen, + u32 len) +{ + if (ulen >= len + 1) { + if (copy_to_user(ubuf, buf, len + 1)) + return -EFAULT; + } else { + char zero = '\0'; + + if (copy_to_user(ubuf, buf, ulen - 1)) + return -EFAULT; + if (put_user(zero, ubuf + ulen - 1)) + return -EFAULT; + return -ENOSPC; + } + + return 0; +} + static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, struct bpf_link_info *info) { @@ -3252,20 +3271,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, if (!ubuf) return 0; - if (ulen >= tp_len + 1) { - if (copy_to_user(ubuf, tp_name, tp_len + 1)) - return -EFAULT; - } else { - char zero = '\0'; - - if (copy_to_user(ubuf, tp_name, ulen - 1)) - return -EFAULT; - if (put_user(zero, ubuf + ulen - 1)) - return -EFAULT; - return -ENOSPC; - } - - return 0; + return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len); } static const struct bpf_link_ops bpf_raw_tp_link_lops = { From patchwork Fri Jun 2 08:52:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13264890 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 A5CA346AA for ; Fri, 2 Jun 2023 08:52:54 +0000 (UTC) Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7DE5170C for ; Fri, 2 Jun 2023 01:52:52 -0700 (PDT) Received: by mail-yb1-xb33.google.com with SMTP id 3f1490d57ef6-ba8a0500f4aso1905515276.3 for ; Fri, 02 Jun 2023 01:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685695972; x=1688287972; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CoWktiecpuhKACCKJUKAizBJc2ltmc/bGhYBs8kLMUM=; b=ZYEqJ8OuXlCu6gf9K6H/5BgfWS6vKYTBKyG8875ALuLG5eSTaQaMqPa7/vhYIou9bl y5G8V/jlDJJNsmKs0O1uRT/AwJpBqnIGC08Kmdd6VmNa/5Pc2ltiCS/VsPyUG83SkUOi uoH4+iQnYUXWxrXytBdsucJgNVjhuiJVcIcdC516D9oXJ2kAclyZey2W7g0y1Mtjjyig t/bYg4XnyY6WkkDLcE9BkmueBLMxq7zvPblSTMwQY3repmiZecnnQXo4a9olVdINMLCx HwIXR3TFUbjql4OT3zUzhjSMitjiak3yzcBr5f8u/4jk4730jmyVRAH+ETqa9u4MtLz7 +TBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685695972; x=1688287972; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CoWktiecpuhKACCKJUKAizBJc2ltmc/bGhYBs8kLMUM=; b=aMlHzkmngnyz2wxDOrZE7jpD3SHg1C/O+c08ohrwcddDm7lvf21LOTHgFq4M5ZbfAw tdcj8cIakwt4geF1Lh1P+XrsdtDQOm11he0Xkv6FtqzMLqt/uvD0GKjPWoA0g0GqNTaQ 8rz3argyUmmAk2WBJ4Ur/wSE+ag42Ehrt+k6cLS0J/Tm+XBecGArvDr0TZBEdlGHKi/J IZRECH/u+J2BRg9HVzI8reUbvyRohlFmUh++OXAS6iK+wOGnHXgAyHXeD55yFSgdfy8y O/hHD3VxYm35mC0uuq6PGAJZcOMhznQ62wxIyHysv+RSs30xErZQAURiGbDY09q6VPXq Igdg== X-Gm-Message-State: AC+VfDx6ISd4Q/KUxFzSgXV6sHkMcBv4MIE8CivWUakXdbNK04VIQh9L 8U36nVXvy8HKAhEIo4KQPRyths4IYNpu6U+mtkk= X-Google-Smtp-Source: ACHHUZ7fi91OeEcVZlNzOkv8THwi1vuf3/Vjrb7au7prtsBMMHAHGgIszktmyIkAboA6lhALKIgjrw== X-Received: by 2002:a0d:c802:0:b0:565:c4af:1a90 with SMTP id k2-20020a0dc802000000b00565c4af1a90mr10892241ywd.40.1685695971961; Fri, 02 Jun 2023 01:52:51 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5401:1e90:5400:4ff:fe75:fb5d]) by smtp.gmail.com with ESMTPSA id b123-20020a0dd981000000b00565c29cf592sm289828ywe.10.2023.06.02.01.52.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 01:52:51 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 5/6] bpf: Support ->fill_link_info for perf_event Date: Fri, 2 Jun 2023 08:52:38 +0000 Message-Id: <20230602085239.91138-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230602085239.91138-1-laoar.shao@gmail.com> References: <20230602085239.91138-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net By adding support for ->fill_link_info to the perf_event link, users will be able to inspect it using `bpftool link show`. While users can currently access this information via `bpftool perf show`, consolidating the link information for all link types in one place would be more convenient. Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 6 ++++++ kernel/bpf/syscall.c | 45 ++++++++++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 6 ++++++ 3 files changed, 57 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 22c8168..87ecf8b 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6442,6 +6442,12 @@ struct bpf_link_info { __u64 addrs; __u32 count; } kprobe_multi; + struct { + __aligned_u64 name; /* in/out: symbol name buffer ptr */ + __u64 addr; + __u32 name_len; + __u32 offset; + } perf_event; }; } __attribute__((aligned(8))); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 80c9ec0..da2de8e 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3303,9 +3303,54 @@ static void bpf_perf_link_dealloc(struct bpf_link *link) kfree(perf_link); } +static int bpf_perf_link_fill_link_info(const struct bpf_link *link, + struct bpf_link_info *info) +{ + struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); + char __user *ubuf = u64_to_user_ptr(info->perf_event.name); + u32 ulen = info->perf_event.name_len; + const struct perf_event *event; + u64 probe_offset, probe_addr; + u32 prog_id, fd_type; + const char *buf; + size_t len; + int err; + + if (!ulen ^ !ubuf) + return -EINVAL; + if (!ubuf) + return 0; + + event = perf_get_event(perf_link->perf_file); + if (IS_ERR(event)) + return PTR_ERR(event); + + err = bpf_get_perf_event_info(event, &prog_id, &fd_type, + &buf, &probe_offset, + &probe_addr); + if (err) + return err; + + len = strlen(buf); + if (buf) { + err = bpf_copy_to_user(ubuf, buf, ulen, len); + if (err) + return err; + } else { + char zero = '\0'; + + if (put_user(zero, ubuf)) + return -EFAULT; + } + info->perf_event.addr = probe_addr; + info->perf_event.offset = probe_offset; + return 0; +} + static const struct bpf_link_ops bpf_perf_link_lops = { .release = bpf_perf_link_release, .dealloc = bpf_perf_link_dealloc, + .fill_link_info = bpf_perf_link_fill_link_info, }; static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 22c8168..87ecf8b 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6442,6 +6442,12 @@ struct bpf_link_info { __u64 addrs; __u32 count; } kprobe_multi; + struct { + __aligned_u64 name; /* in/out: symbol name buffer ptr */ + __u64 addr; + __u32 name_len; + __u32 offset; + } perf_event; }; } __attribute__((aligned(8))); From patchwork Fri Jun 2 08:52:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13264891 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 7C0B246AA for ; Fri, 2 Jun 2023 08:52:56 +0000 (UTC) Received: from mail-yw1-x112f.google.com (mail-yw1-x112f.google.com [IPv6:2607:f8b0:4864:20::112f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65B331713 for ; Fri, 2 Jun 2023 01:52:53 -0700 (PDT) Received: by mail-yw1-x112f.google.com with SMTP id 00721157ae682-565a3cdba71so20018137b3.0 for ; Fri, 02 Jun 2023 01:52:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685695973; x=1688287973; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2vC5VC4yFEIJf6awDHKSY7ygYDdMqF7ISKjmMUJqgZ8=; b=p6ALRpcCoWAYSCUO0S9HG3Dx73tNS7HGqhVFtcdDozhYP6EeuGlp9Szt94Vk6l2LEl OCsYJD/Y6/Ro+lNSZOrQT6M7tOhp2MU3jr5QSCBhm0OiQlFOALzJpio9ctWWJw7HWVBE um1zsbxRFylGSl9WapiC2tOqv/EiavE3u2ok+QRQ69d6zNjldAawQvL0B6fwb8dO41t7 bn1jqrqQvVPgonGU7rUYjKl1MK3fSp8DCKDEAk8pYdXu+RO8pr65ns+O4HchJVvFqKgK zVazgNWxBhRqBPH1TpRaS3o9xboRg5bHdy+wr6p89OryaD/0mhDL0M9ATYV5Xjs9Uhyl 3gXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685695973; x=1688287973; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2vC5VC4yFEIJf6awDHKSY7ygYDdMqF7ISKjmMUJqgZ8=; b=Ybj4eYtNSCvBxwkcPaImVaDt/4C0BGlAFYSmsxlkI/tiwGW9BtMy9cARnc9TVUKR/M H7JvSPyZevL0WbvBS8fOTJRXKTv37mLDjDoJ4RTo2q5lN3ROLh/I5nQMZTm7yxSfORZa QGpZrsrQnrSFOq3z5sq2xTgDjaHJ2PxMUV1AM010HUFSuwZSzXoG13UmtHqMpaBixDuI Vk+qfdjvJwqZ18K6CggR7UrVOdKGfA7PXt6u1UdndzBdLc4+j3fwW/9J8I6dSRwIm6/z y/0joA3/kjWcaFxvmoz8j29SBbkWNSJkcb7w7UmQT8aAa22upK8vNTtKP/xFS6MUUor9 laoQ== X-Gm-Message-State: AC+VfDyYAQb8y+4cNB+mdyHYUqCiTWG/Jfstv1vuakiJbIOLSZ+ABGB+ hdJEsvK5VZ2EvVKhnUNHAkU= X-Google-Smtp-Source: ACHHUZ7SHKrekYblIQYpIcUizpGJ0YWOZqEGgflxdJKOHrZaNQSb5xeFys5KdDw3DcnNYbnh/BSAVA== X-Received: by 2002:a81:778b:0:b0:568:bc0b:bf92 with SMTP id s133-20020a81778b000000b00568bc0bbf92mr13348346ywc.34.1685695972769; Fri, 02 Jun 2023 01:52:52 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5401:1e90:5400:4ff:fe75:fb5d]) by smtp.gmail.com with ESMTPSA id b123-20020a0dd981000000b00565c29cf592sm289828ywe.10.2023.06.02.01.52.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 01:52:52 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 6/6] bpftool: Show probed function in perf_event link info Date: Fri, 2 Jun 2023 08:52:39 +0000 Message-Id: <20230602085239.91138-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230602085239.91138-1-laoar.shao@gmail.com> References: <20230602085239.91138-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net Show the exposed perf_event link info in bpftool. The result as follows, $ bpftool link show 1: perf_event prog 5 func kernel_clone addr ffffffffb40bc310 offset 0 bpf_cookie 0 pids trace(9726) $ bpftool link show -j [{"id":1,"type":"perf_event","prog_id":5,"func":"kernel_clone","addr":18446744072435254032,"offset":0,"bpf_cookie":0,"pids":[{"pid":9726,"comm":"trace"}]}] Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 3b00c07..045f59f 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -280,6 +280,12 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) kernel_syms_show(addrs, info->kprobe_multi.count, 0); jsonw_end_array(json_wtr); break; + case BPF_LINK_TYPE_PERF_EVENT: + jsonw_string_field(json_wtr, "func", + u64_to_ptr(info->perf_event.name)); + jsonw_uint_field(json_wtr, "addr", info->perf_event.addr); + jsonw_uint_field(json_wtr, "offset", info->perf_event.offset); + break; default: break; } @@ -416,7 +422,7 @@ void netfilter_dump_plain(const struct bpf_link_info *info) static int show_link_close_plain(int fd, struct bpf_link_info *info) { struct bpf_prog_info prog_info; - const char *prog_type_str; + const char *prog_type_str, *buf; int err; show_link_header_plain(info); @@ -472,6 +478,12 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs); kernel_syms_show(addrs, cnt, indent); break; + case BPF_LINK_TYPE_PERF_EVENT: + buf = (const char *)u64_to_ptr(info->perf_event.name); + if (buf[0] != '\0' || info->perf_event.addr) + printf("\n\tfunc %s addr %llx offset %d ", buf, + info->perf_event.addr, info->perf_event.offset); + break; default: break; } @@ -498,6 +510,7 @@ static int do_show_link(int fd) int count; int err; + buf[0] = '\0'; memset(&info, 0, sizeof(info)); again: err = bpf_link_get_info_by_fd(fd, &info, &len); @@ -533,6 +546,12 @@ static int do_show_link(int fd) goto again; } } + if (info.type == BPF_LINK_TYPE_PERF_EVENT && + !info.perf_event.name) { + info.perf_event.name = (unsigned long)&buf; + info.perf_event.name_len = sizeof(buf); + goto again; + } if (json_output) show_link_close_json(fd, &info);