From patchwork Tue Jun 20 16:29:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286160 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A063EB64DB for ; Tue, 20 Jun 2023 16:30:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231335AbjFTQaX (ORCPT ); Tue, 20 Jun 2023 12:30:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231550AbjFTQaV (ORCPT ); Tue, 20 Jun 2023 12:30:21 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8F0C172E; Tue, 20 Jun 2023 09:30:18 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-25ecfb3947eso2876756a91.0; Tue, 20 Jun 2023 09:30:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278618; x=1689870618; 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=Nn+VvMrPWCKqoo4PubFGskWvnQBFZ4/nSXCXeyqhAzE=; b=cdBnE3Gn4CIi5QrJW5KtZE1S9YRHu5EaoSenrC4g41YYR++X0kMoowcj3rAWG0huly IMKvzIiw28kZHwMCh1Or5/qzu9uYYYrg4dSy4FUAIAQXg3RxExUmAeh1onhyURLxqUPS qm3HNmaT1k4EM4uKke0wLnzlvCLsla0WgNAvI0tKaHtngk8qF7QH6wL1sNKw7xlVAWwX IKKsApM9Jh9i2O0183KEq53ByQMwpc8Af+ZrnnWhg7eA6I0UBu87OjMeJBXTaI55t9SB HbF450HKZJM1Rqbl6Q+ll1ttnTQz3foWiTHZORLErEI/UqZAiwFxMA5WkVKcG40FNLu7 32mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278618; x=1689870618; 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=Nn+VvMrPWCKqoo4PubFGskWvnQBFZ4/nSXCXeyqhAzE=; b=UHI68nxpscnom5coxPnJPo+Ze5gVo9dJlTOvsHydUz/aj4VYpQUR8/MKbtHVygcnU5 OBuMJ9wblifqLTCbu+b3eEmjkejEcHbmOPRXxEKTKoioUZZ68IRwmyVhatIMBSIUNyHA Q2fAmdRgP2/UyiUNSjj/benurTLD33N1phYfmY6kQLaKr+eZFNRm/qqTmmMQx8PM6rXi bDLwNRfOvDoHVdcLM17WGsBr3MbfV+c/4AtgFUDiBqMXpZ1Kg3m4Ie4tCZ5WUTZemTeU vdtxTPTz96eAjaMsTLw/LDAX721u8TcPBYefcDnPZXhO+8Qt2sudLmRltQNQYTpeLrPG Gejw== X-Gm-Message-State: AC+VfDzmoEhRS/lCeNoj3VeXii4F7XGYlXoxSD4rDvCdPSj/il0YzW7m YCzeHVtz1eUHieEsIc6pSMMluDgnrEi2S73domo= X-Google-Smtp-Source: ACHHUZ5lAIN4/eKU4nFr/rTCd6cm2+EnugkJdsFnqa4TbXiEHSZCByJTVM9OEnj3XZ+gbGVz4OUjhA== X-Received: by 2002:a17:90b:147:b0:25c:571:8670 with SMTP id em7-20020a17090b014700b0025c05718670mr12244348pjb.46.1687278618226; Tue, 20 Jun 2023 09:30:18 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:17 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 01/11] bpf: Support ->fill_link_info for kprobe_multi Date: Tue, 20 Jun 2023 16:29:58 +0000 Message-Id: <20230620163008.3718-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org 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`. 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 not permitted, the probed address will not be exposed, ensuring security. Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 5 +++++ kernel/trace/bpf_trace.c | 28 ++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 38 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a7b5e91..23691ea 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; + __u32 flags; + } kprobe_multi; }; } __attribute__((aligned(8))); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 2bc41e6..2123197b 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2459,6 +2459,7 @@ struct bpf_kprobe_multi_link { u32 cnt; u32 mods_cnt; struct module **mods; + u32 flags; }; struct bpf_kprobe_multi_run_ctx { @@ -2548,9 +2549,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 __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); + info->kprobe_multi.count = kmulti_link->cnt; + info->kprobe_multi.flags = kmulti_link->flags; + + if (!uaddrs) + return 0; + if (ucount < kmulti_link->cnt) + return -EINVAL; + if (!kallsyms_show_value(current_cred())) + 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) @@ -2862,6 +2889,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr link->addrs = addrs; link->cookies = cookies; link->cnt = cnt; + link->flags = flags; if (cookies) { /* diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index a7b5e91..23691ea 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; + __u32 flags; + } kprobe_multi; }; } __attribute__((aligned(8))); From patchwork Tue Jun 20 16:29:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286161 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62280EB64D8 for ; Tue, 20 Jun 2023 16:30:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229693AbjFTQaZ (ORCPT ); Tue, 20 Jun 2023 12:30:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231629AbjFTQaW (ORCPT ); Tue, 20 Jun 2023 12:30:22 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9724B1980; Tue, 20 Jun 2023 09:30:20 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-25eb401995aso2189969a91.2; Tue, 20 Jun 2023 09:30:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278620; x=1689870620; 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=AizcmEHPbR6PfE4uUAF8Goz2I5beEO+/ft4PBffEArk=; b=Smv3nPK2LQCRLYIpX7JYrbGjQvAbjHbGttsPiIJGzzqPoasl4DU6nQGvWjBdSBjo59 uAldKWHjEUR0VvnInXg8CfADZ+LDuiAOztwAZfyL3+q32uvj59CcT/V/46pVIsed2u+f dXyjlBskOQm11j2K/6pgReL2igIUfSijnJZWqtWlYCplRgzXXeIpYr4QA2kSm6iYgLDM hAt6ivBZTwSDbdI9bLAzT8AupCa0tzsZjt5Dd4x/WYGpAz3N7TuT4edK3T+2NtPjr7L9 suU/05MR5iiRS3EqQhJnoCqD0vemsVTzCWa8AFRmN3TuiN3WkuydDJIChs7XDQVOP3wx I9Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278620; x=1689870620; 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=AizcmEHPbR6PfE4uUAF8Goz2I5beEO+/ft4PBffEArk=; b=KUQftoLlqdX7O0CY8aBvPKh1/X7YbqjzlowCo1Rtv6tHcwkAKDyglquZhE511R65qN mRXH5uoR4AQuBK+ilL2cnCPUCnBA41EkIvehjGcocApzr5hNe3r2UoqHF8fABXJaOqSm q4aZnGan8VDqPvNhujy29IAOaSUOVETBRx1DnW8r4j/ReK1ogW5MUDhx6R3Flx61emO5 rv7iNkMCfRDFcWvTBYLEKr7RcUMbi67pRnnMWSrxpiX0fBg1W1mhHSQGC8Pov4IOZft5 tLcd2ZdpARJEIFaoFMhhWjvHbm8RSVVBsKQUTu3OVtJbemGch7ljIQZ61GSX+KcQ0URj JXnQ== X-Gm-Message-State: AC+VfDxG1UPlZjCd8VOkku+ub+/UcCcz2u0yiU40tmXh0jV5EbfKz6v7 rjDOcFsttfPn3KJuVVF84g8= X-Google-Smtp-Source: ACHHUZ7hORov/0liiH06C00mtXxWypX/E0obGWOGEsYSKrCdpCls/Mht80VmzgsPohRVNufnkorJtA== X-Received: by 2002:a17:90b:11cf:b0:25e:df16:892a with SMTP id gv15-20020a17090b11cf00b0025edf16892amr4993079pjb.34.1687278619951; Tue, 20 Jun 2023 09:30:19 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:19 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 02/11] bpftool: Dump the kernel symbol's module name Date: Tue, 20 Jun 2023 16:29:59 +0000 Message-Id: <20230620163008.3718-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org If the kernel symbol is in a module, we will dump the module name as well. The square brackets around the module name are trimmed. Signed-off-by: Yafang Shao --- tools/bpf/bpftool/xlated_dumper.c | 6 +++++- tools/bpf/bpftool/xlated_dumper.h | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c index da608e1..567f56d 100644 --- a/tools/bpf/bpftool/xlated_dumper.c +++ b/tools/bpf/bpftool/xlated_dumper.c @@ -46,7 +46,11 @@ void kernel_syms_load(struct dump_data *dd) } dd->sym_mapping = tmp; sym = &dd->sym_mapping[dd->sym_count]; - if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2) + + /* module is optional */ + sym->module[0] = '\0'; + /* trim the square brackets around the module name */ + if (sscanf(buff, "%p %*c %s [%[^]]s", &address, sym->name, sym->module) < 2) continue; sym->address = (unsigned long)address; if (!strcmp(sym->name, "__bpf_call_base")) { diff --git a/tools/bpf/bpftool/xlated_dumper.h b/tools/bpf/bpftool/xlated_dumper.h index 9a94637..db3ba067 100644 --- a/tools/bpf/bpftool/xlated_dumper.h +++ b/tools/bpf/bpftool/xlated_dumper.h @@ -5,12 +5,14 @@ #define __BPF_TOOL_XLATED_DUMPER_H #define SYM_MAX_NAME 256 +#define MODULE_MAX_NAME 64 struct bpf_prog_linfo; struct kernel_sym { unsigned long address; char name[SYM_MAX_NAME]; + char module[MODULE_MAX_NAME]; }; struct dump_data { From patchwork Tue Jun 20 16:30:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286162 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F317EB64D7 for ; Tue, 20 Jun 2023 16:30:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229601AbjFTQa3 (ORCPT ); Tue, 20 Jun 2023 12:30:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230453AbjFTQaY (ORCPT ); Tue, 20 Jun 2023 12:30:24 -0400 Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF1F8100; Tue, 20 Jun 2023 09:30:22 -0700 (PDT) Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-54fbcfe65caso3962326a12.1; Tue, 20 Jun 2023 09:30:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278622; x=1689870622; 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=/gElHMt7ru6hC4YTlAvkaeRJX96aRe9JlJT1poAWmAI=; b=DNij9L7w88Hv9sa8c8tYmm8O1Qhi0UXS65+IN2iJxBSOSniQRhPxUBGFSMbGosQ8VN NzHsahy7k0Cy4WRicvUpmoEkfwZHPSiy5WjGLKpknq0LG7HE0oq7RO67riyJ7s9BD0MC AeCtquX2fjq45xbjy0JADiSfDorUrs8Qr5rSHYj2v3yDnNqXvVQiArbKmsr3Jg08csTa pyms5XRes9KUwQyzsXB2tZNaG05BQF7ITjfz4GiPr0b2+KjoQxD+RI7OScxKIoB2aHa1 HWwnktqnS0zJLJZVe9pZZ9GMGajkmvLMXRekbB9zlczGe3lw6fxg9RUcimedJWMApHYY 0Z+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278622; x=1689870622; 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=/gElHMt7ru6hC4YTlAvkaeRJX96aRe9JlJT1poAWmAI=; b=XlWiXJbIygrdgCequFTyOiEBK7hfqRE6BPXZ1CsY06ulFs3e0vU1kZN+sPUS66WXQf +x+dJHh/k96qCS76HBPafSQXKtts3JfNEKXVCT5A3/QpkZ8F9HHn2djE8IYGlw/uDcmT TwtgU7uNC/ATAzZuX/4J/tPvhJo6g+834De5+FKDYypAAsJjIHmIVBArTsNiUaqvMo/B 4pnabopsoRwA2D7ijRoJuaReyEPur8xSh6tWcA/ztoqy5+23n9TwIUSPkk4ucrnX4sw1 S9VDH0srr1U9OsJCnuFOujudLOr8E9NXdch4rrdhtQ3mtshrKlFm01bBXNew1QugO8Qx mCbA== X-Gm-Message-State: AC+VfDye0NFyo0MMJvahfVw/pt+mYtanJuQyJrqEioT1RQ8snl48wMMt WKrRWnXXRTbd9VXqYS9S6YuBlJiNFbqhY3l7oS4= X-Google-Smtp-Source: ACHHUZ5TZpkQbAB992bJO2WnlzBuCYfCBMrLLUAytxeVr8Pb239IMfVivcyxr8S15/j/NK9fvg4UIA== X-Received: by 2002:a17:90b:d89:b0:25e:8501:6662 with SMTP id bg9-20020a17090b0d8900b0025e85016662mr24455888pjb.7.1687278622046; Tue, 20 Jun 2023 09:30:22 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:21 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 03/11] bpftool: Show kprobe_multi link info Date: Tue, 20 Jun 2023 16:30:00 +0000 Message-Id: <20230620163008.3718-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org Show the already expose kprobe_multi link info in bpftool. The result as follows, $ tools/bpf/bpftool/bpftool link show 11: kprobe_multi prog 42 kprobe.multi func_cnt 7 addr func ffffffffa2c40f20 schedule_timeout_interruptible ffffffffa2c40f60 schedule_timeout_killable ffffffffa2c40fa0 schedule_timeout_uninterruptible ffffffffa2c40fe0 schedule_timeout_idle ffffffffc0fb18d0 xfs_trans_get_efd [xfs] ffffffffc0fbea10 xfs_trans_get_buf_map [xfs] ffffffffc0fc2320 xfs_trans_get_dqtrx [xfs] pids kprobe_multi(25725) 12: kprobe_multi prog 42 kretprobe.multi func_cnt 7 addr func ffffffffa2c40f20 schedule_timeout_interruptible ffffffffa2c40f60 schedule_timeout_killable ffffffffa2c40fa0 schedule_timeout_uninterruptible ffffffffa2c40fe0 schedule_timeout_idle ffffffffc0fb18d0 xfs_trans_get_efd [xfs] ffffffffc0fbea10 xfs_trans_get_buf_map [xfs] ffffffffc0fc2320 xfs_trans_get_dqtrx [xfs] pids kprobe_multi(25725) $ tools/bpf/bpftool/bpftool link show -j [{"id":11,"type":"kprobe_multi","prog_id":42,"retprobe":false,"func_cnt":7,"funcs":[{"addr":18446744072145342240,"func":"schedule_timeout_interruptible","module":""},{"addr":18446744072145342304,"func":"schedule_timeout_killable","module":""},{"addr":18446744072145342368,"func":"schedule_timeout_uninterruptible","module":""},{"addr":18446744072145342432,"func":"schedule_timeout_idle","module":""},{"addr":18446744072652265680,"func":"xfs_trans_get_efd","module":"xfs"},{"addr":18446744072652319248,"func":"xfs_trans_get_buf_map","module":"xfs"},{"addr":18446744072652333856,"func":"xfs_trans_get_dqtrx","module":"xfs"}],"pids":[{"pid":25725,"comm":"kprobe_multi"}]},{"id":12,"type":"kprobe_multi","prog_id":42,"retprobe":true,"func_cnt":7,"funcs":[{"addr":18446744072145342240,"func":"schedule_timeout_interruptible","module":""},{"addr":18446744072145342304,"func":"schedule_timeout_killable","module":""},{"addr":18446744072145342368,"func":"schedule_timeout_uninterruptible","module":""},{"addr":18446744072145342432,"func":"schedule_timeout_idle","module":""},{"addr":18446744072652265680,"func":"xfs_trans_get_efd","module":"xfs"},{"addr":18446744072652319248,"func":"xfs_trans_get_buf_map","module":"xfs"},{"addr":18446744072652333856,"func":"xfs_trans_get_dqtrx","module":"xfs"}],"pids":[{"pid":25725,"comm":"kprobe_multi"}]}] When kptr_restrict is 2, the result is, $ tools/bpf/bpftool/bpftool link show 11: kprobe_multi prog 42 kprobe.multi func_cnt 7 12: kprobe_multi prog 42 kretprobe.multi func_cnt 7 Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 2d78607..90b51de 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -14,8 +14,10 @@ #include "json_writer.h" #include "main.h" +#include "xlated_dumper.h" static struct hashmap *link_table; +static struct dump_data dd = {}; static int link_parse_fd(int *argc, char ***argv) { @@ -166,6 +168,45 @@ 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 +show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + __u32 i, j = 0; + __u64 *addrs; + + jsonw_bool_field(json_wtr, "retprobe", + info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN); + jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count); + jsonw_name(json_wtr, "funcs"); + jsonw_start_array(json_wtr); + addrs = (__u64 *)u64_to_ptr(info->kprobe_multi.addrs); + qsort((void *)addrs, info->kprobe_multi.count, sizeof(__u64), cmp_u64); + + /* Load it once for all. */ + if (!dd.sym_count) + kernel_syms_load(&dd); + for (i = 0; i < dd.sym_count; i++) { + if (dd.sym_mapping[i].address != addrs[j]) + continue; + 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); + /* Print none if it is vmlinux */ + jsonw_string_field(json_wtr, "module", dd.sym_mapping[i].module); + jsonw_end_object(json_wtr); + if (j++ == info->kprobe_multi.count) + break; + } + jsonw_end_array(json_wtr); +} + static int show_link_close_json(int fd, struct bpf_link_info *info) { struct bpf_prog_info prog_info; @@ -218,6 +259,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 +395,44 @@ 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) +{ + __u32 i, j = 0; + __u64 *addrs; + + if (!info->kprobe_multi.count) + return; + + if (info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN) + printf("\n\tkretprobe.multi "); + else + printf("\n\tkprobe.multi "); + printf("func_cnt %u ", info->kprobe_multi.count); + addrs = (__u64 *)u64_to_ptr(info->kprobe_multi.addrs); + qsort((void *)addrs, info->kprobe_multi.count, sizeof(__u64), cmp_u64); + + /* Load it once for all. */ + if (!dd.sym_count) + kernel_syms_load(&dd); + if (!dd.sym_count) + return; + + printf("\n\t%-16s %s", "addr", "func"); + for (i = 0; i < dd.sym_count; i++) { + if (dd.sym_mapping[i].address != addrs[j]) + continue; + printf("\n\t%016lx %s", + dd.sym_mapping[i].address, dd.sym_mapping[i].name); + if (dd.sym_mapping[i].module[0] != '\0') + printf(" [%s] ", dd.sym_mapping[i].module); + else + printf(" "); + + if (j++ == info->kprobe_multi.count) + break; + } +} + static int show_link_close_plain(int fd, struct bpf_link_info *info) { struct bpf_prog_info prog_info; @@ -396,6 +478,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 +502,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 +528,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; } @@ -471,7 +574,8 @@ static int do_show(int argc, char **argv) fd = link_parse_fd(&argc, &argv); if (fd < 0) return fd; - return do_show_link(fd); + do_show_link(fd); + goto out; } if (argc) @@ -510,6 +614,9 @@ static int do_show(int argc, char **argv) if (show_pinned) delete_pinned_obj_table(link_table); +out: + if (dd.sym_count) + kernel_syms_destroy(&dd); return errno == ENOENT ? 0 : -1; } From patchwork Tue Jun 20 16:30:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286164 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 95B71EB64D7 for ; Tue, 20 Jun 2023 16:30:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230422AbjFTQac (ORCPT ); Tue, 20 Jun 2023 12:30:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231629AbjFTQa0 (ORCPT ); Tue, 20 Jun 2023 12:30:26 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC292172C; Tue, 20 Jun 2023 09:30:24 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-25f0e0bbcaaso1336121a91.3; Tue, 20 Jun 2023 09:30:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278624; x=1689870624; 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=lzAEwWz2g31UMs4g4MUixj1iNbtJ/dM5TZGhqlQXR9c=; b=UQ6WFD4h9OxD0y4f9f3LdKVC6o2VYevQHKIdBZBY9ZsUWR0wlgh+hJFK7TFmZHxh3P 5FmQjvBFlqgfg6B8OQLMLZb0ZJyuAthh38Y/k4FxqZJiBpfL9titLy73qAFOC+a+5dCJ Ulvyxi08lUcpnzw44NJRg1Q4n4CJPgJygHBjhJ0dcgW8CFFJ7oyOzdwmKkueUdfLji7R Q4OeI3PvjxGfi2CbSz8B34J+p5qIT0T5GowFiGJpUrZpqzWdogBxIb+Rc54qixkTpKEw PYCSEESKzutUXkwmc6NcZ/XWPupCooEEX6D/NQymlPDq7BVE+bkHupZ9y5/fGmflM7tc khVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278624; x=1689870624; 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=lzAEwWz2g31UMs4g4MUixj1iNbtJ/dM5TZGhqlQXR9c=; b=dnp5mxahvONCD1BIpQejKgzY58xpyJxtf8qtdpxL5z+/Ie9hVw3k6Ag9bbQzcFr5BH hF6k8EKQaj60UEodFulHe5Zy/WlHZY/UjgWOUautvfvp0GJKpb0B73xFtbIxhJZVk35q l9fYZATrI62oY0/yMmYTjuzeD4WLgADMdggQnyneNv0Jo2DOX/vMH9lRL9Yi6vRztTLN z+7kViw2i1/3r46YPcLF1PX/6l0/skUQcTl57XRoe5EtydwxQ4C9Zz8kiMlkRxeXJ0G2 T3ig9CRC2Xn51ahgi5A3+pi8fH1rvxyHTFus40/VWYSvm15KumRUdM6N+5/vxVgzAbHY ycRg== X-Gm-Message-State: AC+VfDyxnROwYVqXia6R2DoWbcfozGV88seEPtSW/y2CuX6zofSpT2Hc 1TTX+ZBwho7lvzjs4NCHUPI= X-Google-Smtp-Source: ACHHUZ7C0y/gigQKKulCdgk5D0IrXKAkn7+AtANMzbaiQVx00e4ZZnlvu4oc7X76Z0zcZ6Z9o86XhQ== X-Received: by 2002:a17:90b:30c9:b0:25b:bff5:5310 with SMTP id hi9-20020a17090b30c900b0025bbff55310mr7291590pjb.14.1687278623951; Tue, 20 Jun 2023 09:30:23 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:23 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 04/11] bpf: Protect probed address based on kptr_restrict setting Date: Tue, 20 Jun 2023 16:30:01 +0000 Message-Id: <20230620163008.3718-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org 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..e4554db 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 (kallsyms_show_value(current_cred())) + *probe_addr = (unsigned long)tk->rp.kp.addr; + else + *probe_addr = 0; } return 0; } From patchwork Tue Jun 20 16:30:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286163 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1705FEB64D8 for ; Tue, 20 Jun 2023 16:30:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229838AbjFTQad (ORCPT ); Tue, 20 Jun 2023 12:30:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231492AbjFTQa1 (ORCPT ); Tue, 20 Jun 2023 12:30:27 -0400 Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E39361737; Tue, 20 Jun 2023 09:30:26 -0700 (PDT) Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-39ecf031271so2020305b6e.1; Tue, 20 Jun 2023 09:30:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278626; x=1689870626; 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=Gv+tPT5eAlvNoQ80V3EmyTwHswl30O9kG2EfoBAZWRg=; b=RV+EXQdU/oXDko6hZobrCco4399gIgixyQa4wQbLrtpu/iqbbdDfZh1n58Obp9EjPy 1hmDiI8YaP7XbGurd4mIMZvLUOl/cL/S61Uive6F9Br7KNJLnnKOY0v5W4u11FhKwRe3 2v4t+v66FwgC5ChwUC5C7VyMwOAC6o6ZkDwQoGWsw80PdS3n03uaniK+JvY8HWFpIy6b 9ueyAJhoY3Nlr+TXtDBLrhL+3KNay0V0CbvfyFz6lhRO4dmLGYHGc+bidQ0+3zTTn8Vl kFBRSFt/2URNyl60Fvzqw+7DGfJSqg7D0rTdfMTAIU8VbEkxWPEOM3hrGamQVfu4EQBZ tQXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278626; x=1689870626; 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=Gv+tPT5eAlvNoQ80V3EmyTwHswl30O9kG2EfoBAZWRg=; b=jlY5zq0JjF7A9ajPTC3I0NkgEyjd59F2DCgoHpQ7Bs9po6qkFru9lDBfcSV58iTods BN4mUa0VDDqBekUfmYre6W++C/lWQPskAd6a9TRqWUNDl+MNwKgQa6zTiw9bodrZ0UGW hr8AbsMjjjrgSV1UnUQ9feToG+Wd+JvC2musKQonc98lNLQIIqW/O1TEHTtgYo9XmbJN Ys0sZrHQDNPqZJA760ZGzdg7d61dVNGQv86QReEFYJBQxOQp9iblxSVtYmIRLLiNZ0As LI2tKx0A1i1R6beswpiGO0F/V08MjQnrEr5Y8At601Xgp/U3h2O/fhL+E4t6ZnUug87m +t0g== X-Gm-Message-State: AC+VfDyEXC6IECCFn4VoUK/YAghFUI17lI5uYiRNQmiVZSrw5sAbb8GR ZRknFtL0DOVuPP0Us9rr4DQ= X-Google-Smtp-Source: ACHHUZ4ZVWukC2718oZGTXeSBiYqDs+ZdjHVdhztfSqIZZ/6d1v9m2SCov33eQHLJLWLESJA1y/g6g== X-Received: by 2002:a05:6808:614:b0:3a0:3623:11f with SMTP id y20-20020a056808061400b003a03623011fmr3400802oih.21.1687278626052; Tue, 20 Jun 2023 09:30:26 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:25 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 05/11] bpf: Clear the probe_addr for uprobe Date: Tue, 20 Jun 2023 16:30:02 +0000 Message-Id: <20230620163008.3718-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org 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 Acked-by: 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 2123197b..45ee111 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 Tue Jun 20 16:30:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286166 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBCCCEB64D8 for ; Tue, 20 Jun 2023 16:30:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229893AbjFTQaf (ORCPT ); Tue, 20 Jun 2023 12:30:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229935AbjFTQaa (ORCPT ); Tue, 20 Jun 2023 12:30:30 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5105100; Tue, 20 Jun 2023 09:30:28 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-25eb777c7f2so2038801a91.0; Tue, 20 Jun 2023 09:30:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278628; x=1689870628; 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=gyYbQyqyiVM9g5n2rww2bISilT8aqkXPzSI5XyPezvE=; b=FHORSIsxyB2e5F+WDg4EC5eqGqMDHRX/lCSrq734fmaj03OnYfncUtDHC0+rEc5itg T0ySRkaw5Z6boAkXmu1+A8kzn3lc9rAUaPB8ln8mY2IqeSJKZXVyE8/YLePNFmC6c7au a6aZDquzWQywL1ZaE/GpEuJhMXVAgs/R/GB+Tpi5Pg6PfvuecTkhpyz4gouA92XDuDL8 3N/483dKBvHzWmj38JY3r9Hv82CCGYgHdsdrH8WBhtPtTO14SzpyNKmTGSVgFHVL0Y9E JdDSaJ6Nb/LfbAVrO1+/EJkLSMlG0b/cQGjVhEXHYj8O1xgygwG83jk7GZmjSypwBl+2 EHJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278628; x=1689870628; 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=gyYbQyqyiVM9g5n2rww2bISilT8aqkXPzSI5XyPezvE=; b=Gmj/VkGFQtS+V21aDkLN6qIw+kCbGa2YJwS3wOFnHEP5B3ZXoqdTAxpMaGUY1BKAfN PJi20lngkW3gPRnJRbLZk/48RU318Fb6/2LBudqxZsMnbNI9LQe+TcIDo7UgvrUaX92j jxbYp+SRIAXmcods6mS/Ne6ApIPsl7HsrufzjIlu09zivO2S1D9IVjviW4k4bQQjC0mF cZ4N5Q7+t+WE2jELY2q1GsPh7W5bmsP+O/SQsT6dRloTXoC3+LpKGbzbhlfrHaSwvYr1 PyLK6U4SUEE9as1V/WkoLhFaHygALqqyWb/5rzQSaXSHy+Ymluqrl2jHNBHqf7eXaVTs RLdg== X-Gm-Message-State: AC+VfDz0Nm0nonorIpZicPwhXMgBGY0Y4+M6RfXwOxSHqVh9Ao4GFt7t m2ZRYcl9q/BTfZmz7W4rJEw= X-Google-Smtp-Source: ACHHUZ63HvyaITyK3ygZ01lUuHv4DyBK9nJfxvO97p1KOgIQ+IOjTIqANDMK4m87BHtFz2aIwpHyUQ== X-Received: by 2002:a17:90a:fe8d:b0:255:b1d9:a206 with SMTP id co13-20020a17090afe8d00b00255b1d9a206mr6897606pjb.22.1687278628061; Tue, 20 Jun 2023 09:30:28 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:27 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 06/11] bpf: Expose symbol's respective address Date: Tue, 20 Jun 2023 16:30:03 +0000 Message-Id: <20230620163008.3718-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org 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 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index e4554db..17e1729 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -1547,15 +1547,15 @@ 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 (kallsyms_show_value(current_cred())) - *probe_addr = (unsigned long)tk->rp.kp.addr; - else - *probe_addr = 0; } + + if (kallsyms_show_value(current_cred())) + *probe_addr = (unsigned long)tk->rp.kp.addr; + else + *probe_addr = 0; return 0; } #endif /* CONFIG_PERF_EVENTS */ From patchwork Tue Jun 20 16:30:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286165 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6385EEB64DB for ; Tue, 20 Jun 2023 16:30:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229647AbjFTQae (ORCPT ); Tue, 20 Jun 2023 12:30:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230194AbjFTQab (ORCPT ); Tue, 20 Jun 2023 12:30:31 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8618B171F; Tue, 20 Jun 2023 09:30:30 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-25e89791877so2231753a91.2; Tue, 20 Jun 2023 09:30:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278630; x=1689870630; 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=MAQhcd36ZACkATZxiZq5szSI+Xon4GOwdKbkiICwAjg=; b=DKahbAEF5rWmYfNxytZBAmdngVIMQrFw3E1D0EfiwMwzi17VX3VXNzqi3hSUbYV7yh 85QXE6Yc4tilzJ/BcvY1XM1ziMi0C5PnKDHRmuP0vXJc1J9wSEA+/fqN4c5lqrADQebZ ia4FnfKE3o8ZxBcQCWstPrSc9EjwndXQjxRt9KGqT8UDmC2Hr5KOXyY0Sabfq9e3Re8c zzR1FxsRegwGoGA/L0vqj/P5OpHKCb03IaqoWkuzZorcin76kxRDQ3628PhToKlVpg79 5hNcmLgbdNe//uD4jiPuN+6g4pSPjO5DdfxiQFEbsGZsIcVe15DJN7UL4PR8ddiokvib 2THA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278630; x=1689870630; 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=MAQhcd36ZACkATZxiZq5szSI+Xon4GOwdKbkiICwAjg=; b=PZO9SVf2WcD/7dC0piakrJ7vmR6gA+N+VvFcmau5rSuzsGqMmjoMnt16Hqwxjwk+LY o+bf9FjH8zIpS6DVaHDmoFMzR1TAMD8gCli9zyVcHtk1Y2c3CGkq37+3daYyq/SfDI1A wfeF+dbZvyeR+zNamZcVGS3H3RG2gC0nHfX9wwyr9FFw2mTG/pVh4od/6m5m19G/z2f3 r2+T5J5+7fMW9X4On54axhQuPwkFf+8uDROV7tHurRf06+G/gfHiEetCkVVJOgjjHkUD eHaONrjw6KGZvBGklDi3SwnGhot8Wjh31K26GoVuR463bKlFC3HTiKK932v/p2IIyUI4 3WQg== X-Gm-Message-State: AC+VfDz5P7BYq7rPhc/E7bsFWY25LVPmfKVl3u4CKaSPutjISKouLUbJ 77q0HSRyzwrNREf7QGwDIYQ= X-Google-Smtp-Source: ACHHUZ409+etT6fEoE+GNlbUmxhwCza62ZoKLuH9rdyEdqHZgGlYj9zhNYZRBwee21cos3uOqUit6A== X-Received: by 2002:a17:90a:189:b0:25f:982:2d73 with SMTP id 9-20020a17090a018900b0025f09822d73mr3949947pjc.15.1687278629921; Tue, 20 Jun 2023 09:30:29 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:29 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 07/11] bpf: Add a common helper bpf_copy_to_user() Date: Tue, 20 Jun 2023 16:30:04 +0000 Message-Id: <20230620163008.3718-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org 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 1295541..6af003d 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3237,6 +3237,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) { @@ -3255,20 +3274,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 Tue Jun 20 16:30:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286167 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78277EB64DC for ; Tue, 20 Jun 2023 16:30:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231365AbjFTQaf (ORCPT ); Tue, 20 Jun 2023 12:30:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230429AbjFTQae (ORCPT ); Tue, 20 Jun 2023 12:30:34 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4330C171F; Tue, 20 Jun 2023 09:30:32 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-25e8545ea28so3810718a91.0; Tue, 20 Jun 2023 09:30:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278631; x=1689870631; 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=csnu/PPXuzoZEYX0jMZNNmLxIgRmJ8iwLveK+VUisPM=; b=MamyoigDD8/KYULAGuJyRQOiIYhHq405MFY/wtvJS65w1uvxJbjdmuFfUVZRk6K/Dw 2Bn8mrK80iEEW1MHg7eSbkDSLrV+nEiV9hD3XYdrn6zb2v8ZFTav+SheSxGBdY1/8p7x onSgNmJAb5VwvdrH8XG/k3Se90HqyKzJxBP5jonMjBFPDrcHJpVpoAsl+Y3rK0YnPasI t3QCS28BT8uIvxSYzZaPX5p3brVO0u1gMgRhqEwcC28FGf1acSYTczGfsh2VSy8GSQ1X qYlMI4GNT1dZT2LqvKxaxaMEQtk/9IxtO0NOZ9KWY+oGrB/draNVJwJepNLiwqJjNcIo +3Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278631; x=1689870631; 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=csnu/PPXuzoZEYX0jMZNNmLxIgRmJ8iwLveK+VUisPM=; b=IpboZL/OfJcdDJmYDooo5YiTDIuUwMCO6DVGZQp713dA1tDrV8QLCg3x0vSs3X4daq /q9Bm+Zxq5eoMj03/CEs+w9OF1tduGXRq2Mr+Mf3g5DImoOnWP2K0yOMbEsbBkLFfG/h cyk2kYH/m2i71/i8d7iLSp6urHHUtOe07bYOOV5Cl+9uVQ7n4lA2Zuc+sXN9gHth8Y28 qFycrEjtJDQyR4SiGCQl/h3LgPEcH/mKJ1oSkVw1ww3gswraoQ9ddBOQa+oIuE+HwlNZ SOR4KIjoq6EY3MWpNAoQVJylZAZHS4IY7z90f9BwqyYdWXevKXU8cn3hY6FbNguQKnji vUIA== X-Gm-Message-State: AC+VfDxoYRb/wb+n8YqoYvOejhThCyckoOjvPOsRlD5MoF9eMbSDU3tV 9avC9BI30DUP1WAiZJNQ6NM= X-Google-Smtp-Source: ACHHUZ6L7OQk+gxigAJp1mKsms3iXg1i07QBB1g9uLSR+2Zx1jRaBGI+4RSoIl7o1r3pSLMfje8nNQ== X-Received: by 2002:a17:90a:52:b0:25b:c53e:4697 with SMTP id 18-20020a17090a005200b0025bc53e4697mr12667051pjb.16.1687278631647; Tue, 20 Jun 2023 09:30:31 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:31 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 08/11] bpf: Add bpf_perf_link_fill_common() Date: Tue, 20 Jun 2023 16:30:05 +0000 Message-Id: <20230620163008.3718-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org Add a new helper bpf_perf_link_fill_common(), which will be used by perf_link based tracepoint, kprobe and uprobe. Signed-off-by: Yafang Shao --- kernel/bpf/syscall.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 6af003d..db9b500 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3306,6 +3306,40 @@ static void bpf_perf_link_dealloc(struct bpf_link *link) kfree(perf_link); } +static int bpf_perf_link_fill_common(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 const struct bpf_link_ops bpf_perf_link_lops = { .release = bpf_perf_link_release, .dealloc = bpf_perf_link_dealloc, From patchwork Tue Jun 20 16:30:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286168 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD96BEB64DB for ; Tue, 20 Jun 2023 16:30:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231563AbjFTQak (ORCPT ); Tue, 20 Jun 2023 12:30:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231492AbjFTQag (ORCPT ); Tue, 20 Jun 2023 12:30:36 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34E9D100; Tue, 20 Jun 2023 09:30:35 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-517bdc9e81dso1994467a12.1; Tue, 20 Jun 2023 09:30:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278633; x=1689870633; 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=yqvY4jtEfZYloFEPW7IsonK2EHfV4D55HFqHyhzZqMg=; b=fbPnzUhB9Swfydfk5XqM3+BdUViF1cTQ5fd3BIrCRnQGvJTRgJ3Ls4HWQ9nnIxTLUi ytlbC3aKLUrVrHh23W8GnzPm7navXtjySiP4126iatbd8b5yb9hxrZGoa2r/9w6oXUpP AT0akfnFcVVFV4pubbtwswzhVswNGSueW1bDiRgx0+5tIKEcamIS8jcHS+vf42sJto43 xBSNbDIBeVExPkCdfX4y8JDKoQ1p3+ZtEWjTwK+YqnKpmVLSDr5xpDTA7sJiERL/NJnd N6YCdKrqSB6aYeTI0Y7vQDI+7qmTGwVpGoB74GVyGEMqIrcl4wawsu+g+M2J6AjuRfZw F6sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278633; x=1689870633; 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=yqvY4jtEfZYloFEPW7IsonK2EHfV4D55HFqHyhzZqMg=; b=lKz7s+NoOCX2b+rf+xkS2fEK/9JzinslAN6mSiSF6Bc8DeHAZSUDvb8EB6kGc5/i5a 1O6Pi/8o765dZrM7j0RV/VY+3pclIGIM0yasKO/uw9Bwx2shjvG3UogGhwg0R//q1tJF O95jlR/wl5ZeP51W5fxSjz9xwEKibpNBDHd+pRd6jtyOUg+e8yQ2EmOUWf25OzVfVYIo ZLjMV8n8ulrkUP6WXDP/i8n+eKkfyEINcCaqwdZWqI0Dlx3cnDrGKadfhsYU8yWDaS+g +T5QzQizW4Omh1szvZx7LiZQaSs4BtsBDBGLwo8DwLOsZR5eHDpqLE4SlCNSCBLEFR40 DzAg== X-Gm-Message-State: AC+VfDyizJgPGsBWtHNgNrv/wgyfDqoA/mxBy7LquVIkpliWY5oH8ygL 54DfNo58HjWVC/hdziMYcbyy4SOPkYzfMW5YalI= X-Google-Smtp-Source: ACHHUZ6zN2FE4qcW4XBcsOL7YnoH1yERLmxdsJnyoNkoHN1j6NZysQUhZKF6HHzPl+A/HsDEjOvL4A== X-Received: by 2002:a17:90b:33c4:b0:25e:b9fc:7e0c with SMTP id lk4-20020a17090b33c400b0025eb9fc7e0cmr9971809pjb.45.1687278633524; Tue, 20 Jun 2023 09:30:33 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:32 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 09/11] bpf: Support ->fill_link_info for perf_event Date: Tue, 20 Jun 2023 16:30:06 +0000 Message-Id: <20230620163008.3718-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org 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 not permitted, the probed address will not be exposed, maintaining security measures. A new enum bpf_perf_event_type is introduced to help the user understand which struct is relevant. Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 36 +++++++++++++ kernel/bpf/syscall.c | 115 +++++++++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 36 +++++++++++++ 3 files changed, 187 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 23691ea..56528dd 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1056,6 +1056,16 @@ enum bpf_link_type { MAX_BPF_LINK_TYPE, }; +enum bpf_perf_event_type { + BPF_PERF_EVENT_UNSPEC = 0, + BPF_PERF_EVENT_UPROBE = 1, + BPF_PERF_EVENT_KPROBE = 2, + BPF_PERF_EVENT_TRACEPOINT = 3, + BPF_PERF_EVENT_EVENT = 4, + + MAX_BPF_PERF_EVENT_TYPE, +}; + /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command * * NONE(default): No further bpf programs allowed in the subtree. @@ -6443,6 +6453,32 @@ struct bpf_link_info { __u32 count; __u32 flags; } kprobe_multi; + struct { + __u32 type; /* enum bpf_perf_event_type */ + union { + struct { + __aligned_u64 file_name; /* in/out */ + __u32 name_len; + __u32 offset;/* offset from file_name */ + __u32 flags; + } uprobe; /* BPF_PERF_EVENT_UPROBE */ + struct { + __aligned_u64 func_name; /* in/out */ + __u32 name_len; + __u32 offset;/* offset from func_name */ + __u64 addr; + __u32 flags; + } kprobe; /* BPF_PERF_EVENT_KPROBE */ + struct { + __aligned_u64 tp_name; /* in/out */ + __u32 name_len; + } tracepoint; /* BPF_PERF_EVENT_TRACEPOINT */ + struct { + __u64 config; + __u32 type; + } event; /* BPF_PERF_EVENT_EVENT */ + }; + } perf_event; }; } __attribute__((aligned(8))); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index db9b500..9f8d378 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3340,9 +3340,124 @@ static int bpf_perf_link_fill_common(const struct perf_event *event, return 0; } +#ifdef CONFIG_KPROBE_EVENTS +static int bpf_perf_link_fill_kprobe(const struct perf_event *event, + struct bpf_link_info *info) +{ + char __user *uname; + u64 addr, offset; + u32 ulen, type; + int err; + + uname = u64_to_user_ptr(info->perf_event.kprobe.func_name); + ulen = info->perf_event.kprobe.name_len; + info->perf_event.type = BPF_PERF_EVENT_KPROBE; + err = bpf_perf_link_fill_common(event, uname, ulen, &offset, &addr, + &type); + if (err) + return err; + + info->perf_event.kprobe.offset = offset; + if (type == BPF_FD_TYPE_KRETPROBE) + info->perf_event.kprobe.flags = 1; + if (!kallsyms_show_value(current_cred())) + return 0; + info->perf_event.kprobe.addr = addr; + return 0; +} +#endif + +#ifdef CONFIG_UPROBE_EVENTS +static int bpf_perf_link_fill_uprobe(const struct perf_event *event, + struct bpf_link_info *info) +{ + char __user *uname; + u64 addr, offset; + u32 ulen, type; + int err; + + uname = u64_to_user_ptr(info->perf_event.uprobe.file_name); + ulen = info->perf_event.uprobe.name_len; + info->perf_event.type = BPF_PERF_EVENT_UPROBE; + err = bpf_perf_link_fill_common(event, uname, ulen, &offset, &addr, + &type); + if (err) + return err; + + info->perf_event.uprobe.offset = offset; + if (type == BPF_FD_TYPE_URETPROBE) + info->perf_event.uprobe.flags = 1; + return 0; +} +#endif + +static int bpf_perf_link_fill_probe(const struct perf_event *event, + struct bpf_link_info *info) +{ +#ifdef CONFIG_KPROBE_EVENTS + if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE) + return bpf_perf_link_fill_kprobe(event, info); +#endif +#ifdef CONFIG_UPROBE_EVENTS + if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE) + return bpf_perf_link_fill_uprobe(event, info); +#endif + return -EOPNOTSUPP; +} + +static int bpf_perf_link_fill_tracepoint(const struct perf_event *event, + struct bpf_link_info *info) +{ + char __user *uname; + u64 addr, offset; + u32 ulen, type; + + uname = u64_to_user_ptr(info->perf_event.tracepoint.tp_name); + ulen = info->perf_event.tracepoint.name_len; + info->perf_event.type = BPF_PERF_EVENT_TRACEPOINT; + return bpf_perf_link_fill_common(event, uname, ulen, &offset, &addr, + &type); +} + +static int bpf_perf_link_fill_perf_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; + info->perf_event.type = BPF_PERF_EVENT_EVENT; + 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; + + switch (event->prog->type) { + case BPF_PROG_TYPE_PERF_EVENT: + return bpf_perf_link_fill_perf_event(event, info); + case BPF_PROG_TYPE_TRACEPOINT: + return bpf_perf_link_fill_tracepoint(event, info); + case BPF_PROG_TYPE_KPROBE: + return bpf_perf_link_fill_probe(event, info); + default: + return -EOPNOTSUPP; + } +} + 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 23691ea..56528dd 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1056,6 +1056,16 @@ enum bpf_link_type { MAX_BPF_LINK_TYPE, }; +enum bpf_perf_event_type { + BPF_PERF_EVENT_UNSPEC = 0, + BPF_PERF_EVENT_UPROBE = 1, + BPF_PERF_EVENT_KPROBE = 2, + BPF_PERF_EVENT_TRACEPOINT = 3, + BPF_PERF_EVENT_EVENT = 4, + + MAX_BPF_PERF_EVENT_TYPE, +}; + /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command * * NONE(default): No further bpf programs allowed in the subtree. @@ -6443,6 +6453,32 @@ struct bpf_link_info { __u32 count; __u32 flags; } kprobe_multi; + struct { + __u32 type; /* enum bpf_perf_event_type */ + union { + struct { + __aligned_u64 file_name; /* in/out */ + __u32 name_len; + __u32 offset;/* offset from file_name */ + __u32 flags; + } uprobe; /* BPF_PERF_EVENT_UPROBE */ + struct { + __aligned_u64 func_name; /* in/out */ + __u32 name_len; + __u32 offset;/* offset from func_name */ + __u64 addr; + __u32 flags; + } kprobe; /* BPF_PERF_EVENT_KPROBE */ + struct { + __aligned_u64 tp_name; /* in/out */ + __u32 name_len; + } tracepoint; /* BPF_PERF_EVENT_TRACEPOINT */ + struct { + __u64 config; + __u32 type; + } event; /* BPF_PERF_EVENT_EVENT */ + }; + } perf_event; }; } __attribute__((aligned(8))); From patchwork Tue Jun 20 16:30:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286169 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CFAACEB64D7 for ; Tue, 20 Jun 2023 16:30:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231528AbjFTQam (ORCPT ); Tue, 20 Jun 2023 12:30:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231596AbjFTQak (ORCPT ); Tue, 20 Jun 2023 12:30:40 -0400 Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7E471726; Tue, 20 Jun 2023 09:30:36 -0700 (PDT) Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-1a49716e9c5so4880387fac.1; Tue, 20 Jun 2023 09:30:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278635; x=1689870635; 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=+yg3oyB1aA8EDGcuJKMbke6ndCq042NOuKuhG7HtCWg=; b=LmDEI2GJ6daj42TcQdHOfDcLrbzLSubN76eoGAZPfPyE1WWZU+nhmqSYwLKS/pPYnL eSzcc4WnrWsge3DKC1wstjCLwL50Ag7tLcrNco1ZiFjRyzFr8gXK+dvMeuad25OsRLDP nim8kv7lAPkC3oJyTBa7gjeFzdOjWAYr35EQDkkAkabXzShc+aOVT4Ps6V12F+bl11TF fVrdCTm+yINKeEJo17zBADNuHd2HWfVTHY6nqxUGZApVOmbdtSIgGYrrj6iiBqbu61QY aPHu9JBjS2qlxl+t6r4lUyg4ep8hBLmqTuKGmoCl0SQ5dEbfXin9ed5qsWMg+8UAYlm9 rXvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278635; x=1689870635; 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=+yg3oyB1aA8EDGcuJKMbke6ndCq042NOuKuhG7HtCWg=; b=hWzRQRGQkm2uPC26tz0KoAEh79+nN3zFigjUaxp12LrT5KYkI80dQ6QVJrc6hfd2Ub RtgGk3FLbjPU2LV8HXX2C/QfeAFf/TANCfSPbuahTMayHPdPA3y3m550U6DhTZM8GN5L U8KOWH//YNB9l2CnGDhoErUW/610KiYVtEp2YHV3k4zhRTaKv22f9uPZjRJCox+9nACo 5LgX1SsmW507xqPwXKkMS7zaCIyq9r12anvBzRpJLHUINDn/9RBgWV/J0+QKQ3N2+3+X FqrEFxlrw6OsH8qPv7P2TJtSZnpqf+3nRbigb/mIqIApJMBfP0atWJAK/9fxqu7KtgDe 3qkQ== X-Gm-Message-State: AC+VfDzGA1RSbFd1GeB98PM9NGzIgAdxMZ1nFVc05/4DTcSwrn+F0df3 2i86/D0sBJTlHRMkPPi6lao= X-Google-Smtp-Source: ACHHUZ4l0Nkewmd6HpHHCkwMRB/Uu1WXwvsoLgxJajd4WtNILtL8MyCpzCEJ6285aKko2gufSoqCrw== X-Received: by 2002:a54:4090:0:b0:398:4a82:76e1 with SMTP id i16-20020a544090000000b003984a8276e1mr13371472oii.36.1687278635392; Tue, 20 Jun 2023 09:30:35 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:34 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao , Jiri Olsa Subject: [PATCH v4 bpf-next 10/11] bpftool: Add perf event names Date: Tue, 20 Jun 2023 16:30:07 +0000 Message-Id: <20230620163008.3718-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org Add new functions and macros to get perf event names. These names are copied from tool/perf/util/{parse-events,evsel}.c, so that in the future we will have a good chance to use the same code. Suggested-by: Jiri Olsa Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 90b51de..9274d59 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,72 @@ static struct hashmap *link_table; static struct dump_data dd = {}; +static const char *perf_type_name[PERF_TYPE_MAX] = { + [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", +}; + +const char *event_symbols_hw[PERF_COUNT_HW_MAX] = { + [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-cycles", +}; + +const char *event_symbols_sw[PERF_COUNT_SW_MAX] = { + [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] = "minor-faults", + [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = "major-faults", + [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", +}; + +const char *evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX] = { + [PERF_COUNT_HW_CACHE_L1D] = "L1-dcache", + [PERF_COUNT_HW_CACHE_L1I] = "L1-icache", + [PERF_COUNT_HW_CACHE_LL] = "LLC", + [PERF_COUNT_HW_CACHE_DTLB] = "dTLB", + [PERF_COUNT_HW_CACHE_ITLB] = "iTLB", + [PERF_COUNT_HW_CACHE_BPU] = "branch", + [PERF_COUNT_HW_CACHE_NODE] = "node", +}; + +const char *evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX] = { + [PERF_COUNT_HW_CACHE_OP_READ] = "load", + [PERF_COUNT_HW_CACHE_OP_WRITE] = "store", + [PERF_COUNT_HW_CACHE_OP_PREFETCH] = "prefetch", +}; + +const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] = { + [PERF_COUNT_HW_CACHE_RESULT_ACCESS] = "refs", + [PERF_COUNT_HW_CACHE_RESULT_MISS] = "misses", +}; + +#define perf_event_name(array, id) ({ \ + const char *event_str = NULL; \ + \ + if ((id) >= 0 && (id) < ARRAY_SIZE(array)) \ + event_str = array[id]; \ + event_str; \ +}) + static int link_parse_fd(int *argc, char ***argv) { int fd; From patchwork Tue Jun 20 16:30:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13286170 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A826BEB64D7 for ; Tue, 20 Jun 2023 16:30:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231728AbjFTQas (ORCPT ); Tue, 20 Jun 2023 12:30:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231750AbjFTQam (ORCPT ); Tue, 20 Jun 2023 12:30:42 -0400 Received: from mail-oa1-x2c.google.com (mail-oa1-x2c.google.com [IPv6:2001:4860:4864:20::2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BD86172C; Tue, 20 Jun 2023 09:30:38 -0700 (PDT) Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-1a991886254so5059019fac.2; Tue, 20 Jun 2023 09:30:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687278637; x=1689870637; 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=e641tFJX/stK/Gf39GAsqpBjczt5QRhwdI0UiLVax+o=; b=pW8QvMR2l+3bX+GuR0TYeF+/a7vATEw1uuSwYhPNbpq7ygJCrf88aapb2wqi0mpjkR UTp6A4aUCyO/YCKjK7zx8vu/U/BWOpSMhcewirPVekXc1f59PzcCWBQ4B0/NinM6pAGH JbULN9Use2+8QhIyI4hf2IPOae/bP48BVVPVDhk45Mm/nXgqMrwawRpU3TIrtfSSJoEt kt2ki2n2lO+UB2DS0SECwC/rpQ0Vr6MRXE/iGUe34wSyDK1VS69CLY14xgnZ7lZWswQA NbmbrK/TGea7+k2Cd0tY3rg4GoFaGrrCwNaf8P7KUfrzUHHa1hn/9AKIklWOp2WVYMDN AEGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278637; x=1689870637; 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=e641tFJX/stK/Gf39GAsqpBjczt5QRhwdI0UiLVax+o=; b=B/8WJyhzoqHFjFFDlm06d2r3MdJMgEfbBifY2vyKHFYdPxO+973+xJygXDgtHfC6z4 fRlteLxAZTDLHrHQMYUQjOZtCOdZ3N+zLrLL/qRNuGKp2NscQSvb15jHRRBOeLOJMjih zX1iKcw1+PDSiEKf9HUlTkKKBiDYSaBBzEAG+kso8IW0hhtpAkX/xtya/BwVDw+GAc0s Qc/s65M0W+DMkRMG0AhNdSjHzCPlD6yvmVFOl9SQFdSWe78qtfOOVWBGLsIfaxDx532P R/id4MWFpQByOnqWHMXUo2slxEJI9wnqAvAXkwKc61JTBmOe+4YMFAJRTeP0GH9KMka6 8V0Q== X-Gm-Message-State: AC+VfDzqoEupdfdPfjjlIu81EcLBI8thkTuLZzi13O+Q5NwHKsSWnOY/ xzABQZfL2n2CsaLW/pt3e/s= X-Google-Smtp-Source: ACHHUZ41imBWC0XOT7Up9PojBIuhDPwtwS9OQqfM417nMpWBTb++09H2wQvCYjiOgFP+WiQD2p7FOQ== X-Received: by 2002:a05:6808:1188:b0:39c:532b:fe43 with SMTP id j8-20020a056808118800b0039c532bfe43mr15748512oil.21.1687278637540; Tue, 20 Jun 2023 09:30:37 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:b96:5400:4ff:fe7b:3b23]) by smtp.gmail.com with ESMTPSA id t8-20020a17090a3b4800b0025c1cfdb93esm1854286pjf.13.2023.06.20.09.30.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:30:36 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v4 bpf-next 11/11] bpftool: Show perf link info Date: Tue, 20 Jun 2023 16:30:08 +0000 Message-Id: <20230620163008.3718-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230620163008.3718-1-laoar.shao@gmail.com> References: <20230620163008.3718-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org 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 23 uprobe /home/dev/waken/bpf/uprobe/a.out+0x1338 bpf_cookie 0 pids uprobe(27503) 5: perf_event prog 24 uretprobe /home/dev/waken/bpf/uprobe/a.out+0x1338 bpf_cookie 0 pids uprobe(27503) 6: perf_event prog 31 kprobe ffffffffa90a9660 kernel_clone bpf_cookie 0 pids kprobe(27777) 7: perf_event prog 30 kretprobe ffffffffa90a9660 kernel_clone bpf_cookie 0 pids kprobe(27777) 8: perf_event prog 37 tracepoint sched_switch bpf_cookie 0 pids tracepoint(28036) 9: perf_event prog 43 event software:cpu-clock bpf_cookie 0 pids perf_event(28261) 10: perf_event prog 43 event hw-cache:LLC-load-misses bpf_cookie 0 pids perf_event(28261) 11: perf_event prog 43 event hardware:cpu-cycles bpf_cookie 0 pids perf_event(28261) $ tools/bpf/bpftool/bpftool link show -j [{"id":4,"type":"perf_event","prog_id":23,"retprobe":false,"file":"/home/dev/waken/bpf/uprobe/a.out","offset":4920,"bpf_cookie":0,"pids":[{"pid":27503,"comm":"uprobe"}]},{"id":5,"type":"perf_event","prog_id":24,"retprobe":true,"file":"/home/dev/waken/bpf/uprobe/a.out","offset":4920,"bpf_cookie":0,"pids":[{"pid":27503,"comm":"uprobe"}]},{"id":6,"type":"perf_event","prog_id":31,"retprobe":false,"addr":18446744072250627680,"func":"kernel_clone","offset":0,"bpf_cookie":0,"pids":[{"pid":27777,"comm":"kprobe"}]},{"id":7,"type":"perf_event","prog_id":30,"retprobe":true,"addr":18446744072250627680,"func":"kernel_clone","offset":0,"bpf_cookie":0,"pids":[{"pid":27777,"comm":"kprobe"}]},{"id":8,"type":"perf_event","prog_id":37,"tracepoint":"sched_switch","bpf_cookie":0,"pids":[{"pid":28036,"comm":"tracepoint"}]},{"id":9,"type":"perf_event","prog_id":43,"event_type":"software","event_config":"cpu-clock","bpf_cookie":0,"pids":[{"pid":28261,"comm":"perf_event"}]},{"id":10,"type":"perf_event","prog_id":43,"event_type":"hw-cache","event_config":"LLC-load-misses","bpf_cookie":0,"pids":[{"pid":28261,"comm":"perf_event"}]},{"id":11,"type":"perf_event","prog_id":43,"event_type":"hardware","event_config":"cpu-cycles","bpf_cookie":0,"pids":[{"pid":28261,"comm":"perf_event"}]}] 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. The kernel function address won't be exposed if it is not permitted by kptr_restrict. The result as follows when kptr_restrict is 2. $ tools/bpf/bpftool/bpftool link show 4: perf_event prog 23 uprobe /home/dev/waken/bpf/uprobe/a.out+0x1338 5: perf_event prog 24 uretprobe /home/dev/waken/bpf/uprobe/a.out+0x1338 6: perf_event prog 31 kprobe kernel_clone 7: perf_event prog 30 kretprobe kernel_clone 8: perf_event prog 37 tracepoint sched_switch 9: perf_event prog 43 event software:cpu-clock 10: perf_event prog 43 event hw-cache:LLC-load-misses 11: perf_event prog 43 event hardware:cpu-cycles Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 237 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 236 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 9274d59..0173d4e 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -17,6 +17,8 @@ #include "main.h" #include "xlated_dumper.h" +#define PERF_HW_CACHE_LEN 128 + static struct hashmap *link_table; static struct dump_data dd = {}; @@ -274,6 +276,110 @@ static int cmp_u64(const void *A, const void *B) jsonw_end_array(json_wtr); } +static void +show_perf_event_kprobe_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + jsonw_bool_field(wtr, "retprobe", info->perf_event.kprobe.flags & 0x1); + jsonw_uint_field(wtr, "addr", info->perf_event.kprobe.addr); + jsonw_string_field(wtr, "func", + u64_to_ptr(info->perf_event.kprobe.func_name)); + jsonw_uint_field(wtr, "offset", info->perf_event.kprobe.offset); +} + +static void +show_perf_event_uprobe_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + jsonw_bool_field(wtr, "retprobe", info->perf_event.uprobe.flags & 0x1); + jsonw_string_field(wtr, "file", + u64_to_ptr(info->perf_event.uprobe.file_name)); + jsonw_uint_field(wtr, "offset", info->perf_event.uprobe.offset); +} + +static void +show_perf_event_tracepoint_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + jsonw_string_field(wtr, "tracepoint", + u64_to_ptr(info->perf_event.tracepoint.tp_name)); +} + +static char *perf_config_hw_cache_str(__u64 config) +{ + const char *hw_cache, *result, *op; + char *str = malloc(PERF_HW_CACHE_LEN); + + if (!str) { + p_err("mem alloc failed"); + return NULL; + } + + hw_cache = perf_event_name(evsel__hw_cache, 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 = perf_event_name(evsel__hw_cache_op, (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 = perf_event_name(evsel__hw_cache_result, 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 = perf_event_name(event_symbols_hw, config); + break; + case PERF_TYPE_SOFTWARE: + perf_config = perf_event_name(event_symbols_sw, 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 = perf_event_name(perf_type_name, type); + if (perf_type) + jsonw_string_field(wtr, "event_type", perf_type); + else + jsonw_uint_field(wtr, "event_type", type); + + perf_config = perf_config_str(type, config); + if (perf_config) + jsonw_string_field(wtr, "event_config", perf_config); + else + jsonw_uint_field(wtr, "event_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) { struct bpf_prog_info prog_info; @@ -329,6 +435,24 @@ 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: + switch (info->perf_event.type) { + case BPF_PERF_EVENT_EVENT: + show_perf_event_event_json(info, json_wtr); + break; + case BPF_PERF_EVENT_TRACEPOINT: + show_perf_event_tracepoint_json(info, json_wtr); + break; + case BPF_PERF_EVENT_KPROBE: + show_perf_event_kprobe_json(info, json_wtr); + break; + case BPF_PERF_EVENT_UPROBE: + show_perf_event_uprobe_json(info, json_wtr); + break; + default: + break; + } + break; default: break; } @@ -500,6 +624,75 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info) } } +static void show_perf_event_kprobe_plain(struct bpf_link_info *info) +{ + const char *buf; + + buf = (const char *)u64_to_ptr(info->perf_event.kprobe.func_name); + if (buf[0] == '\0' && !info->perf_event.kprobe.addr) + return; + + if (info->perf_event.kprobe.flags & 0x1) + printf("\n\tkretprobe "); + else + printf("\n\tkprobe "); + if (info->perf_event.kprobe.addr) + printf("%llx ", info->perf_event.kprobe.addr); + printf("%s", buf); + if (info->perf_event.kprobe.offset) + printf("+%#x", info->perf_event.kprobe.offset); + printf(" "); +} + +static void show_perf_event_uprobe_plain(struct bpf_link_info *info) +{ + const char *buf; + + buf = (const char *)u64_to_ptr(info->perf_event.uprobe.file_name); + if (buf[0] == '\0') + return; + + if (info->perf_event.uprobe.flags & 0x1) + printf("\n\turetprobe "); + else + printf("\n\tuprobe "); + printf("%s+%#x ", buf, info->perf_event.uprobe.offset); +} + +static void show_perf_event_tracepoint_plain(struct bpf_link_info *info) +{ + const char *buf; + + buf = (const char *)u64_to_ptr(info->perf_event.tracepoint.tp_name); + if (buf[0] == '\0') + return; + + printf("\n\ttracepoint %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; + + printf("\n\tevent "); + perf_type = perf_event_name(perf_type_name, type); + if (perf_type) + printf("%s:", perf_type); + else + printf("%u :", type); + + perf_config = perf_config_str(type, config); + if (perf_config) + printf("%s ", perf_config); + else + printf("%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) { struct bpf_prog_info prog_info; @@ -548,6 +741,24 @@ 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: + switch (info->perf_event.type) { + case BPF_PERF_EVENT_EVENT: + show_perf_event_event_plain(info); + break; + case BPF_PERF_EVENT_TRACEPOINT: + show_perf_event_tracepoint_plain(info); + break; + case BPF_PERF_EVENT_KPROBE: + show_perf_event_kprobe_plain(info); + break; + case BPF_PERF_EVENT_UPROBE: + show_perf_event_uprobe_plain(info); + break; + default: + break; + } + break; default: break; } @@ -570,11 +781,12 @@ static int do_show_link(int fd) struct bpf_link_info info; __u32 len = sizeof(info); __u64 *addrs = NULL; - char buf[256]; + char buf[PATH_MAX]; int count; int err; memset(&info, 0, sizeof(info)); + buf[0] = '\0'; again: err = bpf_link_get_info_by_fd(fd, &info, &len); if (err) { @@ -609,7 +821,30 @@ static int do_show_link(int fd) goto again; } } + if (info.type == BPF_LINK_TYPE_PERF_EVENT) { + if (info.perf_event.type == BPF_PERF_EVENT_EVENT) + goto out; + if (info.perf_event.type == BPF_PERF_EVENT_TRACEPOINT && + !info.perf_event.tracepoint.tp_name) { + info.perf_event.tracepoint.tp_name = (unsigned long)&buf; + info.perf_event.tracepoint.name_len = sizeof(buf); + goto again; + } + if (info.perf_event.type == BPF_PERF_EVENT_KPROBE && + !info.perf_event.kprobe.func_name) { + info.perf_event.kprobe.func_name = (unsigned long)&buf; + info.perf_event.kprobe.name_len = sizeof(buf); + goto again; + } + if (info.perf_event.type == BPF_PERF_EVENT_UPROBE && + !info.perf_event.uprobe.file_name) { + info.perf_event.uprobe.file_name = (unsigned long)&buf; + info.perf_event.uprobe.name_len = sizeof(buf); + goto again; + } + } +out: if (json_output) show_link_close_json(fd, &info); else