From patchwork Thu Jun 8 10:35:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271907 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 425038495 for ; Thu, 8 Jun 2023 10:35:41 +0000 (UTC) Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76C7A2D42 for ; Thu, 8 Jun 2023 03:35:37 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id af79cd13be357-75d4df773b4so33314885a.0 for ; Thu, 08 Jun 2023 03:35:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220536; x=1688812536; 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=vi9s8LCdN3bDa4TRCbJie9LePONriO/RiRjf4ffMgD0=; b=imGqHwW2RHxWLqgUMEahhYa23jhf++qtboqZ5wdrj+vWqVQD+0f2MgVB+y9eRFsd/l Q6w82b1TvqU1VsEnnwJEbfbRHUAxYVpLhAnlj8jaV2TTAutAPh86/ELk3Dmke/Y7RntU VrAAO+RChWIu5aOPks5OdJRJ6qjBzyHx7jXZ7+CCffh0hfvWXcUvL3IznLh09VqKyIu4 Y/wZfwiBlABCmrIxKaU++TlnNUuR0HPM8846LIN21egA5pvqiKE/m9z7eEtFgviH0u/N 5pCyW8b3/j4uFTsxlf4yGCoG6VU5bHCCZ7CgaA+/qdtwmIJ3r3GliyATs8214xoLlvSK Xy9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220536; x=1688812536; 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=vi9s8LCdN3bDa4TRCbJie9LePONriO/RiRjf4ffMgD0=; b=Tn2o/oR1XY0mDxSk0HfcRwxESrRhShf5lHXpt0OXkzOHC/ULoyM93bv4x2QnFNDaMj 5JrOmz3JW9OMj8wKEQglGcESdOveeiiezM3u2asNRL4xE4KWIMtaT0ud8bor6nQMNXHG L+OeWmVUZFTMjX0oJt+5smh7Q6XdQ1bQbK99asgjMVKTbe/ZgANQgkifM2TJ6cOqUqPz p3fJ4O4oaSwp33hwVeQyGz6z0wJqP66uRbVkClGx5KhEvJvZfg2A64ma8LHt9iE7o9yw IjIhv6ZIKatphag024ICXmwuW1mWAwgyj0qN4Xop+vnBYI3emmU0/YtREDgvF5KhGE7N +VTg== X-Gm-Message-State: AC+VfDydzLoBPmC+7G5fz/lIu/HoCpoAYepKLOqI9bxczuqFYlC2KLFt 1oMqIyinabKuzjhdy5TRQCw= X-Google-Smtp-Source: ACHHUZ4R2Sk0iyzhEUsjGOlQNR/qVGuY1sHi3Bd53vnEvkWD9mlfoGvJ5gPF2ZWHRuQhE/2h5lboVQ== X-Received: by 2002:a05:622a:181e:b0:3f5:ef49:722c with SMTP id t30-20020a05622a181e00b003f5ef49722cmr7370397qtc.2.1686220536568; Thu, 08 Jun 2023 03:35:36 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:36 -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 v2 bpf-next 01/11] bpf: Support ->fill_link_info for kprobe_multi Date: Thu, 8 Jun 2023 10:35:13 +0000 Message-Id: <20230608103523.102267-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 With the addition of support for fill_link_info to the kprobe_multi link, users will gain the ability to inspect it conveniently using the `bpftool link show` command. This enhancement provides valuable information to the user, including the count of probed functions and their respective addresses. It's important to note that if the kptr_restrict setting is set to 2, the probed addresses will not be exposed, ensuring security. Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 5 +++++ kernel/trace/bpf_trace.c | 30 ++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 40 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a7b5e91..d99cc16 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6438,6 +6438,11 @@ struct bpf_link_info { __s32 priority; __u32 flags; } netfilter; + struct { + __aligned_u64 addrs; /* in/out: addresses buffer ptr */ + __u32 count; + __u8 retprobe; + } kprobe_multi; }; } __attribute__((aligned(8))); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 2bc41e6..738efcf 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2548,9 +2548,39 @@ 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 __user *uaddrs = 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; + info->kprobe_multi.retprobe = kmulti_link->fp.exit_handler ? + true : false; + if (kptr_restrict == 2) + return 0; + 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..d99cc16 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6438,6 +6438,11 @@ struct bpf_link_info { __s32 priority; __u32 flags; } netfilter; + struct { + __aligned_u64 addrs; /* in/out: addresses buffer ptr */ + __u32 count; + __u8 retprobe; + } kprobe_multi; }; } __attribute__((aligned(8))); From patchwork Thu Jun 8 10:35:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271909 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 09A41A94E for ; Thu, 8 Jun 2023 10:35:43 +0000 (UTC) Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DFAE2D46 for ; Thu, 8 Jun 2023 03:35:38 -0700 (PDT) Received: by mail-qv1-xf32.google.com with SMTP id 6a1803df08f44-6260e771419so1985776d6.1 for ; Thu, 08 Jun 2023 03:35:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220537; x=1688812537; 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=BFKcJn3VhrX3jN6M8CYGvDQgnPeR9FxZhN0RrOT/Q78=; b=gHFHZ3DDDzXXt9AxTlK7wR+lZr1/QqyX9uLafDFZ5vm2kxQpbs+0EsgazYI1monRaQ X6dS4/PfG+j0rGn1SQVewtbW0kCoJ8ggVcrJ+41UQ+WIdZJM7vAh/M2nj8AyICXxGUtj nB3emouR5FZm8DVob5lPjPZ3ZmUYkMNF+uz70RSVRZ3+sU+FpE2nFdnBWSRlVED41CUd 2NunuXaKeBexyU9I6zE3enXBhI50c3RunB/oeSyt7BJfbXTXQXDDyiYzi2e/Z7tGFzVC /9gKWz4fFd1Ec1zJelAODBLlijGhYQ7UkNE2T8sDp+JYwMC+6mKwfTGsOE4NtInJE6qr +GgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220537; x=1688812537; 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=BFKcJn3VhrX3jN6M8CYGvDQgnPeR9FxZhN0RrOT/Q78=; b=XNd1NXfWtGA4Q1rPOckS5wIkrIlRQesqV0EYjtoouYOIH+BY88NqfPnluH6FNmTjvx z6ir0Npb0sYifOOlL1q7hqT0EaikWCt+rFAXTfRPauUyImK6fETqEYQnwxAa+kT+1XgB Ahupb2mofcwokrQs3aWTOSpRyE2qK/ic+BkgH/6EVOuIH9g1lBeS9czBqnHn6Ml8hWZh 1BQ1zAougnUAT1e0pBt6lHlvp9HdHuyTckXHyk5WC4oHSjdrJmol6BmWnxtQOXzr5PAO UtEVORviHjJmVm8tWAvefmDPFGXdPx93Eq2LSAk1aZlD+uxa4Voc4gOBEAk3y9gNGeNH 30PA== X-Gm-Message-State: AC+VfDxAUtO7U8+E6jPZKTG19z668gn6DxKM2roeRZP+346QcxFyiqAJ zQ4TYjAygJrFLzuUVqoeT8o= X-Google-Smtp-Source: ACHHUZ46JqkV+yaYbw5DJSZbTe2H3cbVCys7N0MVjaJBnRJ4EKvoH/oy7iE1qm/Hhz2acM4xJXk6yw== X-Received: by 2002:a05:6214:5017:b0:621:65de:f600 with SMTP id jo23-20020a056214501700b0062165def600mr832777qvb.1.1686220537483; Thu, 08 Jun 2023 03:35:37 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:37 -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 v2 bpf-next 02/11] bpftool: Add address filtering in kernel_syms_load() Date: Thu, 8 Jun 2023 10:35:14 +0000 Message-Id: <20230608103523.102267-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 To enhance the functionality of kernel_syms_load(), an extension is proposed to allow filtering of specific addresses of interest. The extension will ensure that the addresses being filtered are present in the /proc/kallsyms file, which serves as a reference for valid kernel symbols. If an address is found to be invalid or not present in /proc/kallsyms, the function will return a value of -1, indicating an error condition. Signed-off-by: Yafang Shao --- tools/bpf/bpftool/prog.c | 6 ++-- tools/bpf/bpftool/xlated_dumper.c | 72 +++++++++++++++++++++++++++++++-------- tools/bpf/bpftool/xlated_dumper.h | 3 +- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 8443a14..116b5b5 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -777,7 +777,7 @@ static int do_show(int argc, char **argv) __u32 *lens; __u32 i; if (info->nr_jited_ksyms) { - kernel_syms_load(&dd); + kernel_syms_load(&dd, NULL, 0); ksyms = u64_to_ptr(info->jited_ksyms); } @@ -841,7 +841,7 @@ static int do_show(int argc, char **argv) goto exit_free; } } else { - kernel_syms_load(&dd); + kernel_syms_load(&dd, NULL, 0); dd.nr_jited_ksyms = info->nr_jited_ksyms; dd.jited_ksyms = u64_to_ptr(info->jited_ksyms); dd.btf = btf; @@ -1927,7 +1927,7 @@ static int do_loader(int argc, char **argv) if (verifier_logs) { struct dump_data dd = {}; - kernel_syms_load(&dd); + kernel_syms_load(&dd, NULL, 0); dump_xlated_plain(&dd, (void *)gen.insns, gen.insns_sz, false, false); kernel_syms_destroy(&dd); } diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c index da608e1..327c3d6 100644 --- a/tools/bpf/bpftool/xlated_dumper.c +++ b/tools/bpf/bpftool/xlated_dumper.c @@ -23,46 +23,88 @@ static int kernel_syms_cmp(const void *sym_a, const void *sym_b) ((struct kernel_sym *)sym_b)->address; } -void kernel_syms_load(struct dump_data *dd) +static int cmp_u64(const void *A, const void *B) +{ + const __u64 *a = A, *b = B; + + return *a - *b; +} + +/* Set @filter_addrs to filter out the interested addresses. + * The addresses in @filter_addrs must be in /proc/kallsyms. + * The number of addresses in @filter_addrs must be @filter_cnt. + * Each address in @filter_addrs must be unique. + * + * Return 0 on success, -1 on invalid filter, 1 on no symbols. + */ +int kernel_syms_load(struct dump_data *dd, const __u64 *filter_addrs, + __u32 filter_cnt) { struct kernel_sym *sym; char buff[256]; - void *tmp, *address; + void *tmp = NULL, *address; + bool realloc = true; + __u32 i = 0; FILE *fp; fp = fopen("/proc/kallsyms", "r"); if (!fp) - return; + return 1; + if (filter_addrs && filter_cnt) + qsort((void *)filter_addrs, filter_cnt, sizeof(__u64), + cmp_u64); while (fgets(buff, sizeof(buff), fp)) { - tmp = libbpf_reallocarray(dd->sym_mapping, dd->sym_count + 1, - sizeof(*dd->sym_mapping)); - if (!tmp) { + if (realloc) { + tmp = libbpf_reallocarray(dd->sym_mapping, + dd->sym_count + 1, + sizeof(*dd->sym_mapping)); + if (!tmp) { out: - free(dd->sym_mapping); - dd->sym_mapping = NULL; - fclose(fp); - return; + free(dd->sym_mapping); + dd->sym_mapping = NULL; + fclose(fp); + return 1; + } + dd->sym_mapping = tmp; + sym = &dd->sym_mapping[dd->sym_count]; } - dd->sym_mapping = tmp; - sym = &dd->sym_mapping[dd->sym_count]; if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2) continue; - sym->address = (unsigned long)address; if (!strcmp(sym->name, "__bpf_call_base")) { - dd->address_call_base = sym->address; + dd->address_call_base = (unsigned long)address; /* sysctl kernel.kptr_restrict was set */ - if (!sym->address) + if (!address) goto out; } + if (filter_addrs && filter_cnt) { + if ((__u64)address != filter_addrs[i]) { + if (realloc) + realloc = false; + continue; + } + if (i++ == filter_cnt) + break; + if (!realloc) + realloc = true; + } + sym->address = (unsigned long)address; if (sym->address) dd->sym_count++; } fclose(fp); + /* invalid filter address found */ + if (filter_addrs && filter_cnt && i != filter_cnt) { + free(dd->sym_mapping); + dd->sym_mapping = NULL; + return -1; + } + qsort(dd->sym_mapping, dd->sym_count, sizeof(*dd->sym_mapping), kernel_syms_cmp); + return 0; } void kernel_syms_destroy(struct dump_data *dd) diff --git a/tools/bpf/bpftool/xlated_dumper.h b/tools/bpf/bpftool/xlated_dumper.h index 9a94637..14ecbd7 100644 --- a/tools/bpf/bpftool/xlated_dumper.h +++ b/tools/bpf/bpftool/xlated_dumper.h @@ -26,7 +26,8 @@ struct dump_data { char scratch_buff[SYM_MAX_NAME + 8]; }; -void kernel_syms_load(struct dump_data *dd); +int kernel_syms_load(struct dump_data *dd, const __u64 *filter_addrs, + __u32 filter_cnt); void kernel_syms_destroy(struct dump_data *dd); struct kernel_sym *kernel_syms_search(struct dump_data *dd, unsigned long key); void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len, From patchwork Thu Jun 8 10:35:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271908 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 06B008495 for ; Thu, 8 Jun 2023 10:35:43 +0000 (UTC) Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5186C2D47 for ; Thu, 8 Jun 2023 03:35:39 -0700 (PDT) Received: by mail-qv1-xf30.google.com with SMTP id 6a1803df08f44-5ed99ebe076so4232436d6.2 for ; Thu, 08 Jun 2023 03:35:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220538; x=1688812538; 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=tZK2D3AupHIPEtRDmceJilfcB3T0jwDK/RuBEpK2/zo=; b=QoCfGIZ9IobBvr4RO4QA7eVVNQGA/IpZgfz4mbzVzxDN17gzezrxnFnMsSHSMM2AH8 vovSyoLtXVd5pADyGoHcul7OG86b35IH1uNfcyjE78uCpgQ0xM0ppTXWC4AEdVOITalY 5kJcqjQ7P/gTwxO26QxltauTxrHMXHCGw8pCriZUCJ4YiWcTW7UNyQSLq025EhlZmDHD b0ukqddUaeGzEtsG3Xz39sifUh3HcZandpIVaSFD1yrosy7WPpjuUHy6mPQxVOxzXA/S fjDqhSAZ1//kWaAWGp8V8RxorVJ53im25gM5OAat37dsrEgbMKKaqoQURcaktEWMSlva Q0AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220538; x=1688812538; 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=tZK2D3AupHIPEtRDmceJilfcB3T0jwDK/RuBEpK2/zo=; b=NjdTvY+GhPqdeBjQ33NtE+mRVsb9J4jdBfBr7oHW1nA+lZW7nG/Q2stCcnNQqu+lpL TX6iYwks9yDP7nI+Sng8dCD8teaRmN/TOAKkDIGnPIQeALdZsfc3R26HsSBswjgLoGB6 oKOXFcLb/c7IF21wZXiWUyXucgr75+sgt3GjOT3gTGQu7IEK6uSlq7j+pjJzrQ5X3u0J VPLNzcFrQjRRgtTtmpGOgdg0mEdXtW1JL+d0Uz3wo8HKhhysKYxl7l4YGORsGs3f/cjH SCGTEF9W5DYnEno1Oe6FlWkWrVr3Xn4YRADjQfUWfKcbf6BPlAZa444vEek1qBLQO3ID Y2Jw== X-Gm-Message-State: AC+VfDyMiDlUP++De4X024YzKLGxHDkggUQ4Fog8iz6uyA3nc9saYbUW a+ujgo40BXm1GNQmG0KcS+E= X-Google-Smtp-Source: ACHHUZ6vAAzHwkxAiVjmyEQq9t/GdrU9CPQ5mN8EpmQg7crFXpBVYWvXoXYRxt5CMezvu86gP1V38A== X-Received: by 2002:a05:6214:f0a:b0:622:199c:c4d0 with SMTP id gw10-20020a0562140f0a00b00622199cc4d0mr1362667qvb.15.1686220538452; Thu, 08 Jun 2023 03:35:38 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:38 -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 v2 bpf-next 03/11] bpftool: Show probed function in kprobe_multi link info Date: Thu, 8 Jun 2023 10:35:15 +0000 Message-Id: <20230608103523.102267-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 4: kprobe_multi prog 29 retprobe 0 func_cnt 4 addrs ffffffffb5d475b0 funcs schedule_timeout_interruptible ffffffffb5d475f0 schedule_timeout_killable ffffffffb5d47630 schedule_timeout_uninterruptible ffffffffb5d47670 schedule_timeout_idle pids trace(276245) $ tools/bpf/bpftool/bpftool link show -j [{"id":4,"type":"kprobe_multi","prog_id":29,"retprobe":0,"func_cnt":4,"funcs":[{"addr":18446744072465184176,"func":"schedule_timeout_interruptible"},{"addr":18446744072465184240,"func":"schedule_timeout_killable"},{"addr":18446744072465184304,"func":"schedule_timeout_uninterruptible"},{"addr":18446744072465184368,"func":"schedule_timeout_idle"}],"pids":[{"pid":276245,"comm":"trace"}]}] Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 2d78607..c8033c3 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -14,6 +14,7 @@ #include "json_writer.h" #include "main.h" +#include "xlated_dumper.h" static struct hashmap *link_table; @@ -166,6 +167,34 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info) return err; } +static void +show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + struct dump_data dd = {}; + const __u64 *addrs; + __u32 i; + int err; + + jsonw_uint_field(json_wtr, "retprobe", info->kprobe_multi.retprobe); + jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count); + jsonw_name(json_wtr, "funcs"); + jsonw_start_array(json_wtr); + addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs); + err = kernel_syms_load(&dd, addrs, info->kprobe_multi.count); + if (err) { + jsonw_end_array(json_wtr); + return; + } + for (i = 0; i < dd.sym_count; i++) { + jsonw_start_object(json_wtr); + jsonw_uint_field(json_wtr, "addr", dd.sym_mapping[i].address); + jsonw_string_field(json_wtr, "func", dd.sym_mapping[i].name); + jsonw_end_object(json_wtr); + } + jsonw_end_array(json_wtr); + kernel_syms_destroy(&dd); +} + static int show_link_close_json(int fd, struct bpf_link_info *info) { struct bpf_prog_info prog_info; @@ -218,6 +247,9 @@ 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: + show_kprobe_multi_json(info, json_wtr); + break; default: break; } @@ -351,6 +383,35 @@ void netfilter_dump_plain(const struct bpf_link_info *info) printf(" flags 0x%x", info->netfilter.flags); } +static void show_kprobe_multi_plain(struct bpf_link_info *info) +{ + struct dump_data dd = {}; + const __u64 *addrs; + __u32 i; + int err; + + if (!info->kprobe_multi.count) + return; + + printf("\n\tretprobe %d func_cnt %u ", + info->kprobe_multi.retprobe, info->kprobe_multi.count); + addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs); + err = kernel_syms_load(&dd, addrs, info->kprobe_multi.count); + if (err) + return; + for (i = 0; i < dd.sym_count; i++) { + if (!i) + printf("\n\taddrs %016lx funcs %s ", + dd.sym_mapping[i].address, + dd.sym_mapping[i].name); + else + printf("\n\t %016lx %s ", + dd.sym_mapping[i].address, + dd.sym_mapping[i].name); + } + kernel_syms_destroy(&dd); +} + static int show_link_close_plain(int fd, struct bpf_link_info *info) { struct bpf_prog_info prog_info; @@ -396,6 +457,9 @@ 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: + show_kprobe_multi_plain(info); + break; default: break; } @@ -417,7 +481,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 +507,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 Thu Jun 8 10:35:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271910 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 A3159C12D for ; Thu, 8 Jun 2023 10:35:43 +0000 (UTC) Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34D802738 for ; Thu, 8 Jun 2023 03:35:40 -0700 (PDT) Received: by mail-qv1-xf30.google.com with SMTP id 6a1803df08f44-62b6af3822fso3889876d6.2 for ; Thu, 08 Jun 2023 03:35:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220539; x=1688812539; 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=4BDOzLCRV7y5M7RTGg7dFlMbt6JiPdaCVfbp9qQHRpE=; b=hYuLC4boh66CRtg8RpKRBxCKx+k/HXJtDV8nD4x77gizHnbd9VLxpv38jOPabQ2kb9 MIgOpOeipqaVOKPKy7pLHZdM5Rf71f6GjKOENzJ5fGKmVEoXNT0u7IUAbGUfhvc6AYEo gVAPqMk8tX0Bl3twQHi3Jxycm+/3yJuMyH/GlnkLmHkVKDerf+gTHBD9K8AnXKzKInpo j0+WWizsc/8kBPgAwEC0jPuIIIVD6TMuX68xCBA0ue0Vq+j7Z0q9MCd/yFietCf2JUe8 vddtWaZWOHL3CZlIWV2oaSmgR79Yty5aSQFN2tSbhhwKgaW+AG0c2SC19J1AUmI6Crk0 G9BA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220539; x=1688812539; 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=4BDOzLCRV7y5M7RTGg7dFlMbt6JiPdaCVfbp9qQHRpE=; b=LWPJ2vPkvRcFwg+XPKwSGmJaZBWOGA8Cx0506qUSuKSRV+p4upJHLZE2CrkdXsgUkx m6NeYBKhhvDatOYN1sSLM8R4HcdLW4u2n7m3LbWoLxh8Bk9HnMq0ZkQ9wLI39pKyD7V4 N44fjozyxD5BnGMbkBnFnt2aDhAYG3UUbKeX7wQdHn7B9pjwQ4FERYNpsA8Nqwej9efd mAM5Ffy7BTsHL5/lpS3dAA7zMMoR7EJDljPibgKoVC/GQWUCy7+aQ7V/4kcsX/Cnzpav 4N6VMXaXSo7bWc5UAVhGf2nMNiBu2q7CC4BPzCINTDu0UjASGzrsTZowvQZuDBkbbmhw stUQ== X-Gm-Message-State: AC+VfDwIUZZyhHpsuo0EqIFY1qMUAKb27OWTYErbv6pN3xaQIM3eVHRx zSBV0JpgUwcmn4MMNbzNFBQ= X-Google-Smtp-Source: ACHHUZ4Dc0J1LAw6/DYIkVNmuAinarj9GSnx/HsMbNpYEkqMW7bxX3i2XftTnyUJQvPrtUXe60c77Q== X-Received: by 2002:a05:6214:e8a:b0:626:2f1b:b41a with SMTP id hf10-20020a0562140e8a00b006262f1bb41amr1255312qvb.49.1686220539264; Thu, 08 Jun 2023 03:35:39 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:38 -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 v2 bpf-next 04/11] bpf: Protect probed address based on kptr_restrict setting Date: Thu, 8 Jun 2023 10:35:16 +0000 Message-Id: <20230608103523.102267-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 The probed address can be accessed by userspace through querying the task file descriptor (fd). However, it is crucial to adhere to the kptr_restrict setting and refrain from exposing the address if it is not permitted. Signed-off-by: Yafang Shao --- kernel/trace/trace_kprobe.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 59cda19..6564541 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -1551,7 +1551,10 @@ int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type, } else { *symbol = NULL; *probe_offset = 0; - *probe_addr = (unsigned long)tk->rp.kp.addr; + if (kptr_restrict != 2) + *probe_addr = (unsigned long)tk->rp.kp.addr; + else + *probe_addr = 0; } return 0; } From patchwork Thu Jun 8 10:35:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271911 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 BB5BFC12D for ; Thu, 8 Jun 2023 10:35:44 +0000 (UTC) Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10C8A2113 for ; Thu, 8 Jun 2023 03:35:41 -0700 (PDT) Received: by mail-qk1-x72a.google.com with SMTP id af79cd13be357-75b3645fb1fso108665785a.1 for ; Thu, 08 Jun 2023 03:35:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220540; x=1688812540; 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=Rj2TwWknXJwXQfH/U46aiBj+aCjwmGpqTDR3ryEZBYM=; b=AAkeq/D4CRCW+BArBUADS1u1VE2yj35/BFDnk4TrhYIS0vswf6Nq8YUeLlu1BQF9QI k68ZhJFEIxRD6vq1ASIoihlv3p6KsjvIXW+eJP2iVXRtK4lZrh2DNmkzBW25NWt+N5Xr SCk2Ic+N7UwHrbbhnZNf9fjMJOvgkgs5Yq2FzJ3UaGoq3YCucTFa4ICIyUJeWNvKGeaw npydQ6rIwBD1cWDKKgEGJggpOOzijcsay9m453RAJsXLN/cWp2jQ9lxEJuo/JA/i94kZ o2w2hsfTM2t7nEK+XQ6Gq3DhsCB32eXD4i4hHAHni0gz6V+zee2WyeXW0Ie6OmTt1rwr Y1oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220540; x=1688812540; 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=Rj2TwWknXJwXQfH/U46aiBj+aCjwmGpqTDR3ryEZBYM=; b=eyb1hAJITJjR9HC66PaWFaYcrKbasU8S/1IyToSJ1ZB0pCcVsIgAKGTxCIxo0h2VNE hC12ANggfQgnG5YezyE2vS9IdoEklW+kkfDwsE42QqluqZNmuGFlKB1H+hwuiZaE1mjf GZJR62ELfZPJXRzz3c9Rf20cKZB2d+GPaqhyDnwSUbg9vNrwhuyoCUz+Dd+gGXVDhgDp woiZVVr24VqF4aXz8g5UbqifnhV8NFwH8j0jn32uxX0tLlf/3frQRBZPPg4WgCkAjL12 YZ7CFjdr9lCzhbI6ou+ouh7J02oOlRJIUzJrGoGI2dzUKEGH/nXsBMcQ6WThEqP88SH1 Atwg== X-Gm-Message-State: AC+VfDwUucRmtZBP+E1Yr8dG0VcCJrbo5duigOVKiR9iJm8TXDDtYO5g Wwubq7WeMEtIAgOrsngloAQ= X-Google-Smtp-Source: ACHHUZ4FgrxALPq4Mu4ZgnbEltCMKt0s971K5B22AtLRViOupJE3FzMEPh62XXhWVTxXBYQDJQHFlQ== X-Received: by 2002:a05:6214:2a8f:b0:62b:6bc5:7a1b with SMTP id jr15-20020a0562142a8f00b0062b6bc57a1bmr866047qvb.15.1686220540107; Thu, 08 Jun 2023 03:35:40 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:39 -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 v2 bpf-next 05/11] bpf: Clear the probe_addr for uprobe Date: Thu, 8 Jun 2023 10:35:17 +0000 Message-Id: <20230608103523.102267-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 To avoid returning uninitialized or random values when querying the file descriptor (fd) and accessing probe_addr, it is necessary to clear the variable prior to its use. Fixes: 41bdc4b40ed6 ("bpf: introduce bpf subcommand BPF_TASK_FD_QUERY") Signed-off-by: Yafang Shao Cc: Yonghong Song --- kernel/trace/bpf_trace.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 738efcf..0cabd2e 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2372,10 +2372,12 @@ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, event->attr.type == PERF_TYPE_TRACEPOINT); #endif #ifdef CONFIG_UPROBE_EVENTS - if (flags & TRACE_EVENT_FL_UPROBE) + if (flags & TRACE_EVENT_FL_UPROBE) { err = bpf_get_uprobe_info(event, fd_type, buf, probe_offset, event->attr.type == PERF_TYPE_TRACEPOINT); + *probe_addr = 0x0; + } #endif } From patchwork Thu Jun 8 10:35:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271912 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 E2ECCC2D4 for ; Thu, 8 Jun 2023 10:35:44 +0000 (UTC) Received: from mail-qv1-xf34.google.com (mail-qv1-xf34.google.com [IPv6:2607:f8b0:4864:20::f34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF9EA2720 for ; Thu, 8 Jun 2023 03:35:41 -0700 (PDT) Received: by mail-qv1-xf34.google.com with SMTP id 6a1803df08f44-6260e771419so1985956d6.1 for ; Thu, 08 Jun 2023 03:35:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220541; x=1688812541; 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=KZ7jUkbT4JEijzfbm7zfDaqWfOi1VdugfizMyGWzInU=; b=VJnEV2oT6MCSXgGi9yAX5ZDKMe/AiQgFhz0GSy3BO/GxpabcJJ0rFUMqbCUG5y55hH JWkJEsS7fesNSYO1dHqSoG+cRsd2muIrbYqd4j1nFuhFWnseEO1HTldPVLUCEOdcacLb gugfKncLXynFsoaKPrrC4OkUfbeRT1/xjpQrI4W/b5FiAV8cX5ronhE23jDVcUGocEyB dQjYqNJxSB4/CUgWw2lGokKKlstOBm3O2q6z8en4Yu7zWTTW9shpChKO127HmHX9bhRT Sx7ajmCC6HksetNfWhNxDunFtnn7dd/T9oGDKISKmnBA6Gdwt3nN9D4P228H2asEAEug +CXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220541; x=1688812541; 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=KZ7jUkbT4JEijzfbm7zfDaqWfOi1VdugfizMyGWzInU=; b=YiBjpyuysrPtNbzFrq3AKLtk/OLB6sF5sMrz5sTBBxqpNA8A4VhJpY2KlEb3zsAYrC xjWQUv8uOsV4btG+xfQhbf7tWzwC0Tp4ySAih7U41+kU/mcreXR1osCkUlbqcTEtFYkw RFsrvowFurUl3Uc/gnDrvgotttic0Vm6Z5HAWcHpBkAQbjoa6CPfhh6npW1XSw2uJ61R 4wXmrFJ22Qhns3VbbyE9W7gnz/luUqUe1WkoBo0yYgRutq8SqIg8A5zV4Y6ViV2nahQf FGVnOEUsfytp6R2I3xu5QaTyEtfqvELrsaiW/X1Ple/LIUP3B16nD0jUkhDStbxEhqio TYXA== X-Gm-Message-State: AC+VfDzXUCw8hGugQhSddGIUEEaAdnVLDFBxFDjxNv51tF0Qips/eRGe FryIyeNa7zRTmOzjP4zY/aw= X-Google-Smtp-Source: ACHHUZ4b8cVmQFzEDWD86f0Voftfk2Ky2ZMD4FDEfj1gtcKDcuf1ybyQL/F9Pg3be20B78uoFLDSqw== X-Received: by 2002:a05:6214:2488:b0:56e:c066:3cd2 with SMTP id gi8-20020a056214248800b0056ec0663cd2mr981156qvb.2.1686220541047; Thu, 08 Jun 2023 03:35:41 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:40 -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 v2 bpf-next 06/11] bpf: Expose symbol addresses for precise identification Date: Thu, 8 Jun 2023 10:35:18 +0000 Message-Id: <20230608103523.102267-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 6564541..61911e0 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -1547,15 +1547,14 @@ 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; } else { *symbol = NULL; *probe_offset = 0; - if (kptr_restrict != 2) - *probe_addr = (unsigned long)tk->rp.kp.addr; - else - *probe_addr = 0; } + if (kptr_restrict != 2) + *probe_addr = (unsigned long)tk->rp.kp.addr; + else + *probe_addr = 0; return 0; } #endif /* CONFIG_PERF_EVENTS */ From patchwork Thu Jun 8 10:35:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271913 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 E58B7C2D8 for ; Thu, 8 Jun 2023 10:35:44 +0000 (UTC) Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [IPv6:2607:f8b0:4864:20::f2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCDBB2D40 for ; Thu, 8 Jun 2023 03:35:42 -0700 (PDT) Received: by mail-qv1-xf2f.google.com with SMTP id 6a1803df08f44-6261a25e9b6so3410036d6.0 for ; Thu, 08 Jun 2023 03:35:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220542; x=1688812542; 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=YIqt3XjrxTuvqftnfbbwKPC43OuqsK9HXwQWl45kfGw=; b=VRiEIaCnTG+l/hJr2yVr70HRushuOzGKtPK5YS1kvvbOocQSvPg2i74jF8SqDf+Qvx 7hoRvGo2y91N/myHpJEdwg+kU9vy0uaZ5P68JRN91Urmom2nbglZxYTAHdAms6rRRw0V R5WifonjLDUOgZUEswjCtX8l0J81Opn5Xb63Nm48qdHRTFTacHTVBxInAoRS66HVJyYB VbgO/uBVwU68AF5lNFTTyEQIInSvccKVsTg69CMP10fy/Ufym66yF5b8uiHznDfY7p1O 2LCQx4PxBiSaYLwRDWi1rNGMGkH9zszLTStGH8ZoGVWY3to3Ou7Yn8E1TE6pMULGdfMt hODQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220542; x=1688812542; 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=YIqt3XjrxTuvqftnfbbwKPC43OuqsK9HXwQWl45kfGw=; b=jvpQJtir5t1qFvjuXkTJd7wHYp2gQvcPx9CDFaQUfGMsZomywVwaTKFxF/gWzPvq1A CZ2PsBH9QXHoILlpqlulclVz3DoFtnH12ugi+W0USZDO2TfK7OzCKtdEGzhKNm9H4rsf 2VRcN9o47xDtcV/t4mF8IC0dScYfOx3HTvEsnLxzEvfKimRHbcHxtDXKQWZBGSLiVqD7 BBq/JPMtUXCBOaqjI67JGd2LOPNRs8fciY8kpgvjVHp74ZBmdhhHKTlhRTpW8vVQRKq3 Zw3ymB9yeWdgh5auWCccgMEe7jhPP2OdzD7wznDqOaNmXQ/SIyhLnzvyB953ik39cc7X tEWg== X-Gm-Message-State: AC+VfDxaUkYVbPtPNLlEFF+CMg43QZyMqvK8MUhsfldSMi3aa2wp+QDf wkPCTWJU41+VnwAvfnR03CU= X-Google-Smtp-Source: ACHHUZ4Z1piJ2Cl8qfLVcTWUnFRMW9Y8J6USDZ/m7r1qjiJd24ZvFkCar3+NHpBez97KtH+bBNyfdQ== X-Received: by 2002:ad4:5c4b:0:b0:625:bf3e:4d7d with SMTP id a11-20020ad45c4b000000b00625bf3e4d7dmr971587qva.40.1686220541952; Thu, 08 Jun 2023 03:35:41 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:41 -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 v2 bpf-next 07/11] bpf: Add a common helper bpf_copy_to_user() Date: Thu, 8 Jun 2023 10:35:19 +0000 Message-Id: <20230608103523.102267-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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. No functional change. 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 Thu Jun 8 10:35:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271914 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 5FB22C8FD for ; Thu, 8 Jun 2023 10:35:46 +0000 (UTC) Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB0882103 for ; Thu, 8 Jun 2023 03:35:43 -0700 (PDT) Received: by mail-qv1-xf2b.google.com with SMTP id 6a1803df08f44-62b671e0a0dso3978866d6.1 for ; Thu, 08 Jun 2023 03:35:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220543; x=1688812543; 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=N6mSN8B6rsVi3+V/BwBxx2tAFIVmO4m7KApI1B2488Y=; b=Uv8iwfGoBk1QqMFIGFV4fZtuWB+MbOtXzsvW3Rr3VmEaDqNjgTW9uJt0uu4jLL2GJO x23Wl7RMSFZgAjziwgLiJa/WkBdSnipDuZucT8a9Aiov+46u50/+Pilx7popSbt3ZLbm QyZZb9/GBF6JODHxnU+lUyO5rM3+IiTHJNdyU+aue59IU9ftiV9qG2klu8GJAUZgieID cSMMDZpn8w3l7NGqV+RuGiqUUtTuNI+wN//1AAgnOYC/YMxU+MgOjpqeeVLWvm7NItiP t46maIGX8Q9BrE9MATNyE5FSHIg483rKNpSXxK1GYvDFy2qF7BEayXltzd8fosVpTBjs exHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220543; x=1688812543; 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=N6mSN8B6rsVi3+V/BwBxx2tAFIVmO4m7KApI1B2488Y=; b=fknGktU05SqYYsAuDg0XF6RQLwVT7JXQARbuatUcAR45nWy6jEMKJYxWyr8aUnBJ8b 1NXdJcxWfAp4CKlIcMxIkkqsXqopLoQgDk0xi6aijT8x/Kq6J7zA/hVNZmRTQPh+H+Y7 GS2SxG+caldASJhqhTDFgu5+3IyJCd3SDBYwL6ZiB3eC8iMqe6MOGyhTTRAx1IDR4cyy C+EgenEslPif1A3HvQYxGL4Qvzd4k5b2ihieWQA1ydRsuz2TgHJD9/MC0C2b1FIhyFA2 KUiuxW/zl0WvRYUZ+7vK3WpbC7r2c7tBmEzW5iFb89QVAg3MEJj3GcHZbnUPZ5zIgKPv LjZg== X-Gm-Message-State: AC+VfDwKgmzArMpczhuHEQ14diE8o8lSOabPL8M+CW+CYvqo4aAzXuc2 Bim6xR4WspuN1ChHyvhKjig= X-Google-Smtp-Source: ACHHUZ7gn7CBkRWcMSIJsIOvGE8yzT1u0xjBmK7w8q8wbGAoye5PUAosZv3TeNwiGUx98YrUKtq/+w== X-Received: by 2002:ad4:5964:0:b0:616:5460:aafd with SMTP id eq4-20020ad45964000000b006165460aafdmr1163428qvb.3.1686220542910; Thu, 08 Jun 2023 03:35:42 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:42 -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 v2 bpf-next 08/11] bpf: Support ->fill_link_info for perf_event Date: Thu, 8 Jun 2023 10:35:20 +0000 Message-Id: <20230608103523.102267-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 introducing support for ->fill_link_info to the perf_event link, users gain the ability to inspect it using `bpftool link show`. While the current approach involves accessing this information via `bpftool perf show`, consolidating link information for all link types in one place offers greater convenience. Additionally, this patch extends support to the generic perf event, which is not currently accommodated by `bpftool perf show`. While only the perf type and config are exposed to userspace, other attributes such as sample_period and sample_freq are ignored. It's important to note that if kptr_restrict is set to 2, the probed address will not be exposed, maintaining security measures. Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 22 ++++++++++ kernel/bpf/syscall.c | 98 ++++++++++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 22 ++++++++++ 3 files changed, 142 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index d99cc16..c3b821d 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6443,6 +6443,28 @@ struct bpf_link_info { __u32 count; __u8 retprobe; } kprobe_multi; + union { + struct { + /* The name is: + * a) uprobe: file name + * b) kprobe: kernel function + */ + __aligned_u64 name; /* in/out: name buffer ptr */ + __u32 name_len; + __u32 offset; /* offset from the name */ + __u64 addr; + __u8 retprobe; + } probe; /* uprobe, kprobe */ + struct { + /* in/out: tracepoint name buffer ptr */ + __aligned_u64 tp_name; + __u32 name_len; + } tp; /* tracepoint */ + struct { + __u64 config; + __u32 type; + } event; /* generic perf event */ + } perf_event; }; } __attribute__((aligned(8))); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 80c9ec0..e349bdb 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3303,9 +3303,107 @@ static void bpf_perf_link_dealloc(struct bpf_link *link) kfree(perf_link); } +static int bpf_perf_link_fill_name(const struct perf_event *event, + char __user *uname, u32 ulen, + u64 *probe_offset, u64 *probe_addr, + u32 *fd_type) +{ + const char *buf; + u32 prog_id; + size_t len; + int err; + + if (!ulen ^ !uname) + return -EINVAL; + if (!uname) + return 0; + + 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(uname, buf, ulen, len); + if (err) + return err; + } else { + char zero = '\0'; + + if (put_user(zero, uname)) + return -EFAULT; + } + return 0; +} + +static int bpf_perf_link_fill_probe(const struct perf_event *event, + struct bpf_link_info *info) +{ + char __user *uname = u64_to_user_ptr(info->perf_event.probe.name); + u32 ulen = info->perf_event.probe.name_len; + u64 addr, off; + u32 type; + int err; + + err = bpf_perf_link_fill_name(event, uname, ulen, &off, &addr, &type); + if (err) + return err; + info->perf_event.probe.offset = off; + if (type == BPF_FD_TYPE_KRETPROBE || type == BPF_FD_TYPE_URETPROBE) + info->perf_event.probe.retprobe = 1; + else + info->perf_event.probe.retprobe = 0; + + if (kptr_restrict == 2) + return 0; + info->perf_event.probe.addr = addr; + return 0; +} + +static int bpf_perf_link_fill_tp(const struct perf_event *event, + struct bpf_link_info *info) +{ + char __user *uname = u64_to_user_ptr(info->perf_event.tp.tp_name); + u32 ulen = info->perf_event.tp.name_len; + u64 addr, off; + u32 type; + + return bpf_perf_link_fill_name(event, uname, ulen, &off, &addr, &type); +} + +static int bpf_perf_link_fill_event(const struct perf_event *event, + struct bpf_link_info *info) +{ + info->perf_event.event.type = event->attr.type; + info->perf_event.event.config = event->attr.config; + return 0; +} + +static int bpf_perf_link_fill_link_info(const struct bpf_link *link, + struct bpf_link_info *info) +{ + struct bpf_perf_link *perf_link; + const struct perf_event *event; + + perf_link = container_of(link, struct bpf_perf_link, link); + event = perf_get_event(perf_link->perf_file); + if (IS_ERR(event)) + return PTR_ERR(event); + + if (!event->prog) + return -EINVAL; + if (event->prog->type == BPF_PROG_TYPE_PERF_EVENT) + return bpf_perf_link_fill_event(event, info); + if (event->prog->type == BPF_PROG_TYPE_TRACEPOINT) + return bpf_perf_link_fill_tp(event, info); + return bpf_perf_link_fill_probe(event, info); +} + 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 d99cc16..c3b821d 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6443,6 +6443,28 @@ struct bpf_link_info { __u32 count; __u8 retprobe; } kprobe_multi; + union { + struct { + /* The name is: + * a) uprobe: file name + * b) kprobe: kernel function + */ + __aligned_u64 name; /* in/out: name buffer ptr */ + __u32 name_len; + __u32 offset; /* offset from the name */ + __u64 addr; + __u8 retprobe; + } probe; /* uprobe, kprobe */ + struct { + /* in/out: tracepoint name buffer ptr */ + __aligned_u64 tp_name; + __u32 name_len; + } tp; /* tracepoint */ + struct { + __u64 config; + __u32 type; + } event; /* generic perf event */ + } perf_event; }; } __attribute__((aligned(8))); From patchwork Thu Jun 8 10:35:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271915 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 14FB1C8FD for ; Thu, 8 Jun 2023 10:35:47 +0000 (UTC) Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C31C0272D for ; Thu, 8 Jun 2023 03:35:44 -0700 (PDT) Received: by mail-qv1-xf2d.google.com with SMTP id 6a1803df08f44-6260e8a1424so3507916d6.2 for ; Thu, 08 Jun 2023 03:35:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220544; x=1688812544; 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=DM9iAlLzD6FAPOXhyqRgOn4jy8JggVrXZ+3w6h7Ykuw=; b=aewEulSOkyI57tFlqPY/eLFLU7F+cj8kZe51lL0ORZp5zZmXK3dVLl2HpmUrwNI9ud sDdeIGRYQhYj6Xca/7N/Ude4H5bdmShRXaUtJPaHdneDHb0GPN1FVUwW1E6KIkI1rYAt hqmqlLc++aXKiCVb+TT13owfMkacMquWYtYolnqM7YxNqKS7/TcvBsD8MrlwoUe+P7/8 BZ6pRp5MCTr+oJifgjHd+hDhRfPWGX858BUgB4VA9pAPlHzkFwCLNH2swv3+OKZtboKE MtEaAW3GWRP/2X7Mx8vNQzbgW9AZovooqAnxWkUWKfWv1M20lxJpjzLP+1Giin4hdFCX +X1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220544; x=1688812544; 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=DM9iAlLzD6FAPOXhyqRgOn4jy8JggVrXZ+3w6h7Ykuw=; b=REAxRoQUCXAoCl1UcIsYt02ZVyHnfgqF4LjgUounWKGDv2dWg6vvC7HYmoJFtfPwr0 Z3vwqmhwuKLfRg99wXnQK2AcljJy3hEuDmP+jTfvTApm+Lah/dRmeb9qvhXrzOIGJ7EA op4Q0LAGMXQzQ4qLfsVjHVzvD/ph0zs5MPWBP9rcDbooH3LdAVWjYwmRspRB18PWCwZK 1+Bv7lROMd3LS7idw65VVu4Vf4ETcKz8oK30LcJjFx4e6GIv1a5HY1qtOG1Ica75JXn1 z8CDzH6tJ+sRqh/De692GNrlPZFK8idBRTt3XVeNfqGaZTSyPpGs8MN8T9uJbE+ITwXb Heyg== X-Gm-Message-State: AC+VfDyoKHvQQir0zun+eGg38Bdrjnrj8fAMIhR7R8UiibIWn42lQ1x8 YvoXFM6R6WiWwNHG4RUgM4+2/DmOsJvH2uyv9lY= X-Google-Smtp-Source: ACHHUZ7/3MLtfL+S/ZSgA9WLZcEaTu3mtSl+t4TZfT+CrDv9x9HOs5R4Jcol7p838GWs+yYxjtVJBA== X-Received: by 2002:a05:6214:19e1:b0:626:e55:dfad with SMTP id q1-20020a05621419e100b006260e55dfadmr1186473qvc.41.1686220543852; Thu, 08 Jun 2023 03:35:43 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:43 -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 v2 bpf-next 09/11] libbpf: Add perf event names Date: Thu, 8 Jun 2023 10:35:21 +0000 Message-Id: <20230608103523.102267-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 libbpf API to get generic perf event name. Signed-off-by: Yafang Shao --- tools/lib/bpf/libbpf.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 56 +++++++++++++++++++++++++ tools/lib/bpf/libbpf.map | 6 +++ 3 files changed, 169 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 47632606..27d396f 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -119,6 +119,64 @@ [BPF_STRUCT_OPS] = "struct_ops", }; +static const char * const perf_type_name[] = { + [PERF_TYPE_HARDWARE] = "hardware", + [PERF_TYPE_SOFTWARE] = "software", + [PERF_TYPE_TRACEPOINT] = "tracepoint", + [PERF_TYPE_HW_CACHE] = "hw_cache", + [PERF_TYPE_RAW] = "raw", + [PERF_TYPE_BREAKPOINT] = "breakpoint", +}; + +static const char * const perf_hw_name[] = { + [PERF_COUNT_HW_CPU_CYCLES] = "cpu_cycles", + [PERF_COUNT_HW_INSTRUCTIONS] = "instructions", + [PERF_COUNT_HW_CACHE_REFERENCES] = "cache_references", + [PERF_COUNT_HW_CACHE_MISSES] = "cache_misses", + [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = "branch_instructions", + [PERF_COUNT_HW_BRANCH_MISSES] = "branch_misses", + [PERF_COUNT_HW_BUS_CYCLES] = "bus_cycles", + [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = "stalled_cycles_frontend", + [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = "stalled_cycles_backend", + [PERF_COUNT_HW_REF_CPU_CYCLES] = "ref_cpu_cycles", +}; + +static const char * const perf_hw_cache_name[] = { + [PERF_COUNT_HW_CACHE_L1D] = "l1d", + [PERF_COUNT_HW_CACHE_L1I] = "l1i", + [PERF_COUNT_HW_CACHE_LL] = "ll", + [PERF_COUNT_HW_CACHE_DTLB] = "dtlb", + [PERF_COUNT_HW_CACHE_ITLB] = "itlb", + [PERF_COUNT_HW_CACHE_BPU] = "bpu", + [PERF_COUNT_HW_CACHE_NODE] = "node", +}; + +static const char * const perf_hw_cache_op_name[] = { + [PERF_COUNT_HW_CACHE_OP_READ] = "read", + [PERF_COUNT_HW_CACHE_OP_WRITE] = "write", + [PERF_COUNT_HW_CACHE_OP_PREFETCH] = "prefetch", +}; + +static const char * const perf_hw_cache_op_result_name[] = { + [PERF_COUNT_HW_CACHE_RESULT_ACCESS] = "access", + [PERF_COUNT_HW_CACHE_RESULT_MISS] = "miss", +}; + +static const char * const perf_sw_name[] = { + [PERF_COUNT_SW_CPU_CLOCK] = "cpu_clock", + [PERF_COUNT_SW_TASK_CLOCK] = "task_clock", + [PERF_COUNT_SW_PAGE_FAULTS] = "page_faults", + [PERF_COUNT_SW_CONTEXT_SWITCHES] = "context_switches", + [PERF_COUNT_SW_CPU_MIGRATIONS] = "cpu_migrations", + [PERF_COUNT_SW_PAGE_FAULTS_MIN] = "page_faults_min", + [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = "page_faults_maj", + [PERF_COUNT_SW_ALIGNMENT_FAULTS] = "alignment_faults", + [PERF_COUNT_SW_EMULATION_FAULTS] = "emulation_faults", + [PERF_COUNT_SW_DUMMY] = "dummy", + [PERF_COUNT_SW_BPF_OUTPUT] = "bpf_output", + [PERF_COUNT_SW_CGROUP_SWITCHES] = "cgroup_switches", +}; + static const char * const link_type_name[] = { [BPF_LINK_TYPE_UNSPEC] = "unspec", [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", @@ -8953,6 +9011,55 @@ const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t) return attach_type_name[t]; } +const char *libbpf_perf_type_str(enum perf_type_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_type_name)) + return NULL; + + return perf_type_name[t]; +} + +const char *libbpf_perf_hw_str(enum perf_hw_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_hw_name)) + return NULL; + + return perf_hw_name[t]; +} + +const char *libbpf_perf_hw_cache_str(enum perf_hw_cache_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_name)) + return NULL; + + return perf_hw_cache_name[t]; +} + +const char *libbpf_perf_hw_cache_op_str(enum perf_hw_cache_op_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_name)) + return NULL; + + return perf_hw_cache_op_name[t]; +} + +const char * +libbpf_perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_result_name)) + return NULL; + + return perf_hw_cache_op_result_name[t]; +} + +const char *libbpf_perf_sw_str(enum perf_sw_ids t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_sw_name)) + return NULL; + + return perf_sw_name[t]; +} + const char *libbpf_bpf_link_type_str(enum bpf_link_type t) { if (t < 0 || t >= ARRAY_SIZE(link_type_name)) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 754da73..4123e4c 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -16,6 +16,7 @@ #include #include // for size_t #include +#include #include "libbpf_common.h" #include "libbpf_legacy.h" @@ -61,6 +62,61 @@ enum libbpf_errno { LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t); /** + * @brief **libbpf_perf_type_str()** converts the provided perf type value + * into a textual representation. + * @param t The perf type. + * @return Pointer to a static string identifying the perf type. NULL is + * returned for unknown **perf_type_id** values. + */ +LIBBPF_API const char *libbpf_perf_type_str(enum perf_type_id t); + +/** + * @brief **libbpf_perf_hw_str()** converts the provided perf hw id + * into a textual representation. + * @param t The perf hw id. + * @return Pointer to a static string identifying the perf hw id. NULL is + * returned for unknown **perf_hw_id** values. + */ +LIBBPF_API const char *libbpf_perf_hw_str(enum perf_hw_id t); + +/** + * @brief **libbpf_perf_hw_cache_str()** converts the provided perf hw cache + * id into a textual representation. + * @param t The perf hw cache id. + * @return Pointer to a static string identifying the perf hw cache id. + * NULL is returned for unknown **perf_hw_cache_id** values. + */ +LIBBPF_API const char *libbpf_perf_hw_cache_str(enum perf_hw_cache_id t); + +/** + * @brief **libbpf_perf_hw_cache_op_str()** converts the provided perf hw + * cache op id into a textual representation. + * @param t The perf hw cache op id. + * @return Pointer to a static string identifying the perf hw cache op id. + * NULL is returned for unknown **perf_hw_cache_op_id** values. + */ +LIBBPF_API const char *libbpf_perf_hw_cache_op_str(enum perf_hw_cache_op_id t); + +/** + * @brief **libbpf_perf_hw_cache_op_result_str()** converts the provided + * perf hw cache op result id into a textual representation. + * @param t The perf hw cache op result id. + * @return Pointer to a static string identifying the perf hw cache op result + * id. NULL is returned for unknown **perf_hw_cache_op_result_id** values. + */ +LIBBPF_API const char * +libbpf_perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t); + +/** + * @brief **libbpf_perf_sw_str()** converts the provided perf sw id + * into a textual representation. + * @param t The perf sw id. + * @return Pointer to a static string identifying the perf sw id. NULL is + * returned for unknown **perf_sw_ids** values. + */ +LIBBPF_API const char *libbpf_perf_sw_str(enum perf_sw_ids t); + +/** * @brief **libbpf_bpf_link_type_str()** converts the provided link type value * into a textual representation. * @param t The link type. diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 7521a2f..6ae0a36 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -395,4 +395,10 @@ LIBBPF_1.2.0 { LIBBPF_1.3.0 { global: bpf_obj_pin_opts; + libbpf_perf_hw_cache_op_result_str; + libbpf_perf_hw_cache_op_str; + libbpf_perf_hw_cache_str; + libbpf_perf_hw_str; + libbpf_perf_sw_str; + libbpf_perf_type_str; } LIBBPF_1.2.0; From patchwork Thu Jun 8 10:35:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271916 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 8B480C8FD for ; Thu, 8 Jun 2023 10:35:47 +0000 (UTC) Received: from mail-qv1-xf2e.google.com (mail-qv1-xf2e.google.com [IPv6:2607:f8b0:4864:20::f2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A76912738 for ; Thu, 8 Jun 2023 03:35:45 -0700 (PDT) Received: by mail-qv1-xf2e.google.com with SMTP id 6a1803df08f44-6260e8a1424so3508036d6.2 for ; Thu, 08 Jun 2023 03:35:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220545; x=1688812545; 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=qPqi6o92g3CsXbO/7xcmBvCyD1k9IeMV6B5FbznM9F4=; b=cStFsMUA3zKxG+fm2RYeNAzy0zRDGNydPdtJvCN40w/zyx3fpNYgHvn0YV570nHGAO g5o0aJoKizDK1dG0nsY/bUgN/gSboGcX2mNgc/ALu48SGh2qtAG5ZMigvxaWF5RSMg3F 0FYtvG+MUNlaKg3pt9koiiGx7IWjCNwaqNU8DvEQqeFctezlSKzg/8Ur0w2otoomKjIp Kdfe/b6MBYnOv5KZYitDMNJczUYlypEQOzPYC5XiGULCBz/ATY60O41tNgXIAkUCyZUi EOU1SstIIosj3l9/Gs65xYCbJNFQFDTxZfcCn7J4exZC37daxqN4YSlkDeCVdpbq/0iv kP+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220545; x=1688812545; 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=qPqi6o92g3CsXbO/7xcmBvCyD1k9IeMV6B5FbznM9F4=; b=QITExv5dIkCz6OHUeOB9KOohsSFp9tYlCnVPDOVfihFP/8D5U5Wp3uznfZEYl6ywsc IxAXzQQL7QkJjcl8A3l4iUog2zFKFkC6+n7UCJRtSvSF60DhgQAkk6hqexAR3NMx7DXA H0TD2zf18g7uVSQkpXM0+bTMzBzCXzE5QHYoyyPkzSfGLyRfrzKez75y57FeyG+PA2g1 giAIZOKXutknY8NPNcBKAQswkn1ZzhVkMVYLUAzuBI24e14QOeuqwXkyVSoi4gO4BaKU mHiQ+im/RTWMf0rOMAzz1GQm/AcYqWtfaDUytgQTQxi9NTJoaJlrCMljVyXQXwvXIaba V1IQ== X-Gm-Message-State: AC+VfDwKlCJYorlvE26qVON33Olee8X2fY4xfAazoFgdfwr79FjHrTKa 3WGkpbKcR6Drm0fTxgqzn94= X-Google-Smtp-Source: ACHHUZ5DE1Ya43rpmkskVqAu66jO1hTU9QEP6nHZpbdHVm4/kDLNGxfRJ/kp5i9PIfp0jc7n5Yr+7g== X-Received: by 2002:a05:6214:2241:b0:629:78ae:80f0 with SMTP id c1-20020a056214224100b0062978ae80f0mr1185588qvc.8.1686220544697; Thu, 08 Jun 2023 03:35:44 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:44 -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 v2 bpf-next 10/11] bpftool: Move get_prog_info() into do_show_link() Date: Thu, 8 Jun 2023 10:35:22 +0000 Message-Id: <20230608103523.102267-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 As a preparation for a subsequent change, the function get_prog_info() is moved into do_show_link() with no functional alteration. This adjustment paves the way for the upcoming modification. Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index c8033c3..a2b75f4 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -195,11 +195,10 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info) kernel_syms_destroy(&dd); } -static int show_link_close_json(int fd, struct bpf_link_info *info) +static int show_link_close_json(int fd, struct bpf_link_info *info, + const struct bpf_prog_info *prog_info) { - struct bpf_prog_info prog_info; const char *prog_type_str; - int err; jsonw_start_object(json_wtr); @@ -211,16 +210,12 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) u64_to_ptr(info->raw_tracepoint.tp_name)); break; case BPF_LINK_TYPE_TRACING: - err = get_prog_info(info->prog_id, &prog_info); - if (err) - return err; - - prog_type_str = libbpf_bpf_prog_type_str(prog_info.type); + prog_type_str = libbpf_bpf_prog_type_str(prog_info->type); /* libbpf will return NULL for variants unknown to it. */ if (prog_type_str) jsonw_string_field(json_wtr, "prog_type", prog_type_str); else - jsonw_uint_field(json_wtr, "prog_type", prog_info.type); + jsonw_uint_field(json_wtr, "prog_type", prog_info->type); show_link_attach_type_json(info->tracing.attach_type, json_wtr); @@ -412,11 +407,10 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info) kernel_syms_destroy(&dd); } -static int show_link_close_plain(int fd, struct bpf_link_info *info) +static int show_link_close_plain(int fd, struct bpf_link_info *info, + const struct bpf_prog_info *prog_info) { - struct bpf_prog_info prog_info; const char *prog_type_str; - int err; show_link_header_plain(info); @@ -426,16 +420,12 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) (const char *)u64_to_ptr(info->raw_tracepoint.tp_name)); break; case BPF_LINK_TYPE_TRACING: - err = get_prog_info(info->prog_id, &prog_info); - if (err) - return err; - - prog_type_str = libbpf_bpf_prog_type_str(prog_info.type); + prog_type_str = libbpf_bpf_prog_type_str(prog_info->type); /* libbpf will return NULL for variants unknown to it. */ if (prog_type_str) printf("\n\tprog_type %s ", prog_type_str); else - printf("\n\tprog_type %u ", prog_info.type); + printf("\n\tprog_type %u ", prog_info->type); show_link_attach_type_plain(info->tracing.attach_type); if (info->tracing.target_obj_id || info->tracing.target_btf_id) @@ -479,6 +469,7 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) static int do_show_link(int fd) { + struct bpf_prog_info prog_info; struct bpf_link_info info; __u32 len = sizeof(info); __u64 *addrs = NULL; @@ -486,6 +477,7 @@ static int do_show_link(int fd) int count; int err; + memset(&prog_info, 0, sizeof(info)); memset(&info, 0, sizeof(info)); again: err = bpf_link_get_info_by_fd(fd, &info, &len); @@ -495,6 +487,13 @@ static int do_show_link(int fd) close(fd); return err; } + + if (!prog_info.type) { + err = get_prog_info(info.prog_id, &prog_info); + if (err) + return err; + } + if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT && !info.raw_tracepoint.tp_name) { info.raw_tracepoint.tp_name = (unsigned long)&buf; @@ -523,9 +522,9 @@ static int do_show_link(int fd) } if (json_output) - show_link_close_json(fd, &info); + show_link_close_json(fd, &info, &prog_info); else - show_link_close_plain(fd, &info); + show_link_close_plain(fd, &info, &prog_info); if (addrs) free(addrs); From patchwork Thu Jun 8 10:35:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13271917 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 A73DFC8FD for ; Thu, 8 Jun 2023 10:35:48 +0000 (UTC) Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A82432103 for ; Thu, 8 Jun 2023 03:35:46 -0700 (PDT) Received: by mail-qk1-x733.google.com with SMTP id af79cd13be357-75d46c7cd6cso32593685a.3 for ; Thu, 08 Jun 2023 03:35:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686220546; x=1688812546; 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=xNt3akaJPIthWai2WuzrVMbK68Et2Ndbp/y1XO2NjqU=; b=ndjYNjM9wpPXop6hCh2JTLL8KroL7lHFqK5UwiGUbPvSUYsqlnVTuxoHU9+XSh7YZr DmdFVNukq67bps1BHuniveLUqto3K+6nanejOs6D1xD9gTCfNULJ1gbQVWFwKV4X6++/ +9g3Bz7DDoJqb8s9DMsKLYVykPRJGfS01nWYU/3MC5s1V5numdvsa5LOtR4A4STsmwPG +RlK3IEsx2Bla6nQd99aeq1J9kaeGvjr+4gzSBU/5Sj3Hy/RPVZpJjyvRRsJDoXjNJR2 yQDUOe9UghgNnFbLvm3SqElcXtK+oaZLJdkq+yPBC+PcQonppuKOqgmJEjGKjeOii9UV 8pYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686220546; x=1688812546; 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=xNt3akaJPIthWai2WuzrVMbK68Et2Ndbp/y1XO2NjqU=; b=az/oR6p6Q21eWGLpZKJfwJKouMpmOfb+b3G6uBK/7XU8HOYxO/6omRduf0e1qh1KxT NIg288iVo7OtlMu4H3j3SgLUjNqIeXwWNMTESC0HROvn3V6FZTDn8y73Y2XL2OCn/CJ9 OEBlhnddPZUIR7m9k44tPqFyTKyqqLqEk0tDUEgOV+e8A0rbDn6S+FoGevXZsDy7VTfJ 0VnNaFSLlAmNwY+tkXQZgOycdoFGGBAjDxT49lfVrzVu61tLI4k3gOpt/8XcvVyIT3EV OvF8CJPH863KQbAZrd80HpudhyZiF6NYcm6A0HREuOe+AYeiNfV64+0RfW0BYfBpLcbd 3pMA== X-Gm-Message-State: AC+VfDw23t1UbRmuX6cf2JX1Ew+DfiUcpqI+KZy/I/2GQdKVlkOS2VTz CyHkm8rnHZXQn3A3bMbJReY= X-Google-Smtp-Source: ACHHUZ5tYM5H1ThLinatloRJU9vuukTVS6serqx49OOw0zl/z0/M1CQFaes9vXO9IKYTdTlnPb2yoA== X-Received: by 2002:a05:6214:d0a:b0:626:2e07:c080 with SMTP id 10-20020a0562140d0a00b006262e07c080mr1006917qvh.15.1686220545741; Thu, 08 Jun 2023 03:35:45 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:2418:5400:4ff:fe77:b548]) by smtp.gmail.com with ESMTPSA id p16-20020a0cf550000000b0062839fc6e36sm302714qvm.70.2023.06.08.03.35.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 03:35:45 -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 v2 bpf-next 11/11] bpftool: Show probed function in perf_event link info Date: Thu, 8 Jun 2023 10:35:23 +0000 Message-Id: <20230608103523.102267-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com> References: <20230608103523.102267-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 Enhance bpftool to display comprehensive information about exposed perf_event links, covering uprobe, kprobe, tracepoint, and generic perf event. The resulting output will include the following details: $ tools/bpf/bpftool/bpftool link show 4: perf_event prog 22 retprobe 0 name kernel_clone addr ffffffff940a9660 bpf_cookie 0 pids kprobe(133008) 5: perf_event prog 28 tp_name sched_switch bpf_cookie 0 pids tracepoint(133199) 7: perf_event prog 42 type software config cpu_clock bpf_cookie 0 pids perf_event(134187) 8: perf_event prog 42 type hw_cache config ll-read-miss bpf_cookie 0 pids perf_event(134187) 9: perf_event prog 42 type hardware config cpu_cycles bpf_cookie 0 pids perf_event(134187) 13: perf_event prog 72 retprobe 0 name /home/waken/bpf/uprobe/a.out offset 0x1338 bpf_cookie 0 pids uprobe(164506) 14: perf_event prog 73 retprobe 1 name /home/waken/bpf/uprobe/a.out offset 0x1338 bpf_cookie 0 pids uprobe(164506) $ tools/bpf/bpftool/bpftool link show -j [{"id":4,"type":"perf_event","prog_id":22,"retprobe":0,"name":"kernel_clone","offset":0,"addr":18446744071898306144,"bpf_cookie":0,"pids":[{"pid":133008,"comm":"kprobe"}]},{"id":5,"type":"perf_event","prog_id":28,"tp_name":"sched_switch","bpf_cookie":0,"pids":[{"pid":133199,"comm":"tracepoint"}]},{"id":7,"type":"perf_event","prog_id":42,"type":"software","config":"cpu_clock","bpf_cookie":0,"pids":[{"pid":134187,"comm":"perf_event"}]},{"id":8,"type":"perf_event","prog_id":42,"type":"hw_cache","config":"ll-read-miss","bpf_cookie":0,"pids":[{"pid":134187,"comm":"perf_event"}]},{"id":9,"type":"perf_event","prog_id":42,"type":"hardware","config":"cpu_cycles","bpf_cookie":0,"pids":[{"pid":134187,"comm":"perf_event"}]},{"id":13,"type":"perf_event","prog_id":72,"retprobe":0,"name":"/home/waken/bpf/uprobe/a.out","offset":4920,"addr":0,"bpf_cookie":0,"pids":[{"pid":164506,"comm":"uprobe"}]},{"id":14,"type":"perf_event","prog_id":73,"retprobe":1,"name":"/home/waken/bpf/uprobe/a.out","offset":4920,"addr":0,"bpf_cookie":0,"pids":[{"pid":164506,"comm":"uprobe"}]}] For generic perf events, the displayed information in bpftool is limited to the type and configuration, while other attributes such as sample_period, sample_freq, etc., are not included. Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 178 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index a2b75f4..7c02540 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -195,6 +195,100 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info) kernel_syms_destroy(&dd); } +static void +show_perf_event_probe_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + jsonw_uint_field(wtr, "retprobe", info->perf_event.probe.retprobe); + jsonw_string_field(wtr, "name", + u64_to_ptr(info->perf_event.probe.name)); + jsonw_uint_field(wtr, "offset", info->perf_event.probe.offset); + jsonw_uint_field(wtr, "addr", info->perf_event.probe.addr); +} + +static void +show_perf_event_tp_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + jsonw_string_field(wtr, "tp_name", + u64_to_ptr(info->perf_event.tp.tp_name)); +} + +static const char *perf_config_hw_cache_str(__u64 config) +{ +#define PERF_HW_CACHE_LEN 128 + const char *hw_cache, *result, *op; + char *str = malloc(PERF_HW_CACHE_LEN); + + if (!str) { + p_err("mem alloc failed"); + return NULL; + } + hw_cache = libbpf_perf_hw_cache_str(config & 0xff); + if (hw_cache) + snprintf(str, PERF_HW_CACHE_LEN, "%s-", hw_cache); + else + snprintf(str, PERF_HW_CACHE_LEN, "%lld-", config & 0xff); + op = libbpf_perf_hw_cache_op_str((config >> 8) & 0xff); + if (op) + snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str), + "%s-", op); + else + snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str), + "%lld-", (config >> 8) & 0xff); + result = libbpf_perf_hw_cache_op_result_str(config >> 16); + if (result) + snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str), + "%s", result); + else + snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str), + "%lld", config >> 16); + + return str; +} + +static const char *perf_config_str(__u32 type, __u64 config) +{ + const char *perf_config; + + switch (type) { + case PERF_TYPE_HARDWARE: + perf_config = libbpf_perf_hw_str(config); + break; + case PERF_TYPE_SOFTWARE: + perf_config = libbpf_perf_sw_str(config); + break; + case PERF_TYPE_HW_CACHE: + perf_config = perf_config_hw_cache_str(config); + break; + default: + perf_config = NULL; + break; + } + return perf_config; +} + +static void +show_perf_event_event_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + __u64 config = info->perf_event.event.config; + __u32 type = info->perf_event.event.type; + const char *perf_type, *perf_config; + + perf_type = libbpf_perf_type_str(type); + if (perf_type) + jsonw_string_field(wtr, "type", perf_type); + else + jsonw_uint_field(wtr, "type", type); + + perf_config = perf_config_str(type, config); + if (perf_config) + jsonw_string_field(wtr, "config", perf_config); + else + jsonw_uint_field(wtr, "config", config); + + if (type == PERF_TYPE_HW_CACHE && perf_config) + free((void *)perf_config); +} + static int show_link_close_json(int fd, struct bpf_link_info *info, const struct bpf_prog_info *prog_info) { @@ -245,6 +339,14 @@ static int show_link_close_json(int fd, struct bpf_link_info *info, case BPF_LINK_TYPE_KPROBE_MULTI: show_kprobe_multi_json(info, json_wtr); break; + case BPF_LINK_TYPE_PERF_EVENT: + if (prog_info->type == BPF_PROG_TYPE_PERF_EVENT) + show_perf_event_event_json(info, json_wtr); + else if (prog_info->type == BPF_PROG_TYPE_TRACEPOINT) + show_perf_event_tp_json(info, json_wtr); + else + show_perf_event_probe_json(info, json_wtr); + break; default: break; } @@ -407,6 +509,56 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info) kernel_syms_destroy(&dd); } +static void show_perf_event_probe_plain(struct bpf_link_info *info) +{ + const char *buf; + __u32 retprobe; + + buf = (const char *)u64_to_ptr(info->perf_event.probe.name); + if (buf[0] == '\0' && !info->perf_event.probe.addr) + return; + + retprobe = info->perf_event.probe.retprobe; + printf("\n\tretprobe %u name %s ", retprobe, buf); + if (info->perf_event.probe.offset) + printf("offset %#x ", info->perf_event.probe.offset); + if (info->perf_event.probe.addr) + printf("addr %llx ", info->perf_event.probe.addr); +} + +static void show_perf_event_tp_plain(struct bpf_link_info *info) +{ + const char *buf; + + buf = (const char *)u64_to_ptr(info->perf_event.tp.tp_name); + if (buf[0] == '\0') + return; + + printf("\n\ttp_name %s ", buf); +} + +static void show_perf_event_event_plain(struct bpf_link_info *info) +{ + __u64 config = info->perf_event.event.config; + __u32 type = info->perf_event.event.type; + const char *perf_type, *perf_config; + + perf_type = libbpf_perf_type_str(type); + if (perf_type) + printf("\n\ttype %s ", perf_type); + else + printf("\n\ttype %u ", type); + + perf_config = perf_config_str(type, config); + if (perf_config) + printf("config %s ", perf_config); + else + printf("config %llu ", config); + + if (type == PERF_TYPE_HW_CACHE && perf_config) + free((void *)perf_config); +} + static int show_link_close_plain(int fd, struct bpf_link_info *info, const struct bpf_prog_info *prog_info) { @@ -450,6 +602,14 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info, case BPF_LINK_TYPE_KPROBE_MULTI: show_kprobe_multi_plain(info); break; + case BPF_LINK_TYPE_PERF_EVENT: + if (prog_info->type == BPF_PROG_TYPE_PERF_EVENT) + show_perf_event_event_plain(info); + else if (prog_info->type == BPF_PROG_TYPE_TRACEPOINT) + show_perf_event_tp_plain(info); + else + show_perf_event_probe_plain(info); + break; default: break; } @@ -479,6 +639,7 @@ static int do_show_link(int fd) memset(&prog_info, 0, sizeof(info)); memset(&info, 0, sizeof(info)); + buf[0] = '\0'; again: err = bpf_link_get_info_by_fd(fd, &info, &len); if (err) { @@ -520,7 +681,24 @@ static int do_show_link(int fd) goto again; } } + if (info.type == BPF_LINK_TYPE_PERF_EVENT) { + if (prog_info.type == BPF_PROG_TYPE_PERF_EVENT) + goto out; + if (prog_info.type == BPF_PROG_TYPE_TRACEPOINT && + !info.perf_event.tp.tp_name) { + info.perf_event.tp.tp_name = (unsigned long)&buf; + info.perf_event.tp.name_len = sizeof(buf); + goto again; + } + if (prog_info.type == BPF_PROG_TYPE_KPROBE && + !info.perf_event.probe.name) { + info.perf_event.probe.name = (unsigned long)&buf; + info.perf_event.probe.name_len = sizeof(buf); + goto again; + } + } +out: if (json_output) show_link_close_json(fd, &info, &prog_info); else