From patchwork Fri Jun 23 14:15:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290669 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 740D28F5D for ; Fri, 23 Jun 2023 14:16:19 +0000 (UTC) Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1A4B1FE3; Fri, 23 Jun 2023 07:16:17 -0700 (PDT) Received: by mail-ot1-x333.google.com with SMTP id 46e09a7af769-6b2c3ec38f0so535697a34.1; Fri, 23 Jun 2023 07:16:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529777; x=1690121777; 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=XAZRw8vajFuODFkWd0plHtuUiOq+1ZKOAt8yEy7ldYVPh3llA64dm+uptKo7kKCifh SeYIKhVmratGs6p4h0m/+8NSpaAGgcYkyy8chX2OkeWOGFNWGSRPMKwcdrGPJeQH7CgQ WmEYxzbTyljLsKpVVPyiv9hahupaRCwjjiyX34AA4RS0vFff8nolx5+5GlKLXU/9ypLa JGFuD4uFjwDwn4JOx4b4RDBVv5NWQQsCTFZoPYUliA9jD7zeTFZ/VDf4Fm/4i6qUs//0 7JBODTBDfmUesKE7MXnPCLitQLITxXRI+46yq0yGIn589obv3chvxF0oJWkJXBhrNT1w F9bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529777; x=1690121777; 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=a1V52yv9qk9MhOq3sG+M1XPmkrsKiW/aiF2hEpe+yrPTg0RdJMcst5mgE2c5YQ+n6U Zib4eiuw/faOuxdvrCPl01a69IZqA9do2nVVzJC/GaWiw5xe3s2nvOdO9Wc/wNnYGhBI QItWPmGtuYplnF47ZQS/PZ6Y9vQnxSSeDhfXaFccHCg0Y6wdTAisIiSsVfszx+1fWtUh PO0wO912WyR02XENfxOtlFi4DP+STWTcA4lO+evPkbpqyjiZQctEa5CgCfaQMeU9eubZ 1VWposg3jUXX1NspZ5dNMiL1tFHHj2oYpW9fCHZRYYYI1iDANxzZUrZ3QI8vVNwEtnYA oONg== X-Gm-Message-State: AC+VfDwbqRhRhZRL1WVwaKUAlxLQTMvfA9qruADyLLAM1fp5spWLwSnN OK3QGuER3B1YGCP2Y0VZEco= X-Google-Smtp-Source: ACHHUZ4qMvsiDtG0Kn4xihh6gv+bz+axMmIg56sCFqFPVVv/druj3efQuaNd8Mc4SVAFt2vFQGzPvw== X-Received: by 2002:a9d:6ad1:0:b0:6b5:e4f9:9202 with SMTP id m17-20020a9d6ad1000000b006b5e4f99202mr5134104otq.3.1687529777153; Fri, 23 Jun 2023 07:16:17 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16:16 -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 v5 bpf-next 01/11] bpf: Support ->fill_link_info for kprobe_multi Date: Fri, 23 Jun 2023 14:15:36 +0000 Message-Id: <20230623141546.3751-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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`. 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 Fri Jun 23 14:15:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290671 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 7E90DA926 for ; Fri, 23 Jun 2023 14:16:20 +0000 (UTC) Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B41D2126; Fri, 23 Jun 2023 07:16:19 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-666edfc50deso546320b3a.0; Fri, 23 Jun 2023 07:16:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529779; x=1690121779; 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=sLm8qIJgN8FGYOi0nzzy6oD6S3zWWuJ+4NIC6zvHq7wtQnOohgy4gdjBstl9Oh+Rkb q2up3Tw/YsERiIikmzJ8jotr/GZZJi0BSTq97p1/HMit9cw2nEMLx7IyIKnQ56JoIp2/ 6/o7VlFunYrB6FAe3krG7joqoKkpJgmbDxECMBsFgZLnJv/H83S/ESng6oJIpyvkEZQH o6d8PERhXmWtslzdk2hPZYcHTge3fvwhljhi4XU/i/18QBrrFlLs+9nejrQBvOmK4+07 CBWoLP4L14mYi7PyT7mBCBstf3BmeGUwIAd94aMzlB+B/A6zbpJ9YZjC5LYVSF6MTSA4 WH5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529779; x=1690121779; 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=Te1DyezbYHYRq+3PpRRLQeDZUS8sgeOnYx3XZAaON14I9LK2BbVhYAZIplwFf5NRX8 kOLP7bLNnXKteCS6DtYcZapNySl/GoyakqGgJd3KLZrU+sc9Rmkw/x2gW4Jg78TkP0Tw 8KP1clfpledZ+TkXlWHnJljeQ1Z4vbAY+OR0qXnuMcq94V0b4UV7Um0ml+eyIZeqAYEA I62B4QCZvfe+XCk5XkqhYlVBDIU4D+SNh/3pIVSZrUPeLXBy/3Hx1uZnBtBO8Zjk6WhB zVeN07BTfHNaxRGMvhFzvD6FLX9RwGL5Q7Wj4uWY/oCrroPxD+xb1KtN394xaenGGDtM UjAg== X-Gm-Message-State: AC+VfDzp4kFmftq4crdlkOyB4KNTSCEV9XSfNpkvMXMPGAMRYQLiy7o7 6CWCyU42dZzLZ4k+prg/U6U= X-Google-Smtp-Source: ACHHUZ60PCAucPZtgr018uHG8m7nIzteypWnXQXLrGFQGjV4ll1/lLj8F54tdc6XVyxtt5Pvxz5W0Q== X-Received: by 2002:a05:6a20:3d10:b0:125:c3f7:f863 with SMTP id y16-20020a056a203d1000b00125c3f7f863mr2454255pzi.8.1687529778834; Fri, 23 Jun 2023 07:16:18 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16:18 -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 v5 bpf-next 02/11] bpftool: Dump the kernel symbol's module name Date: Fri, 23 Jun 2023 14:15:37 +0000 Message-Id: <20230623141546.3751-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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 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 Reviewed-by: Quentin Monnet --- 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 Fri Jun 23 14:15:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290672 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 3BB54AD3B for ; Fri, 23 Jun 2023 14:16:23 +0000 (UTC) Received: from mail-ot1-x330.google.com (mail-ot1-x330.google.com [IPv6:2607:f8b0:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 475731FE3; Fri, 23 Jun 2023 07:16:21 -0700 (PDT) Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-6b720fe3431so620703a34.3; Fri, 23 Jun 2023 07:16:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529780; x=1690121780; 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=x89rCGzIlm3iq2VodxuXVFQMkLYGEL2HattOp/c5i4Y=; b=X36zRlsPLIZqSY9xMSkGTq/s+H7v5WlTi0Yd2b/RraDtiiz0Gkn7lu86yAhWhNeJlC fFhRUW2G8ao4U4+NOvxSycaZ0PVCfFMJuAbz2AdFt9+EOKmTZ5dGhnIa6Pz+iEUFqiHM XoyRVicQ7nvJD7mGPOaHyt9rMHaD0BTelvFpNEFsvONlHTo54nGN3cuoaxFmQ3NVWpxu 5hS71BIH4QdCH2Kmcc7uV6+1/WoechSXBbImRgfGaa5zh9RZz4zJj4mjYdF2Xq3DNIG6 vkLy58V1SIwwUSuK0EOvRfuC+TnmDyuUvtgC/q4SlUyZ62f6xAJGkWa+8kNFuKMy9XOA wfSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529780; x=1690121780; 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=x89rCGzIlm3iq2VodxuXVFQMkLYGEL2HattOp/c5i4Y=; b=iYmKdKpXtWy0tvTuLW30edJE7OCeYnUeMJCs9z+vQSQxbAtF1tCSdm5ikHviqEaRB2 2cVVvd5JwDz8+C44ZkK4dAS7WDV7143EbseIne/FZvMiuWcyRFxEM1nXsxH3GZvzFcBh hjonJOzBLVk+T8E3EFHKL2BqjCF+0l0mOF19T/kilAdHCeid/0F06XWk50i3RNROjgPE D1oZrrR43s+6QHZFrLW92TdsB1MuqxyQ0YgEiMd9zPt/I3eoTBvWhLi0qPlncqslGrag sAFchSEqY1y9ZUgeMfpuJyYqpUS05nvKMAtOGSkGoAEtP+XQrsaYooAssPRJIYXGFP7p XktQ== X-Gm-Message-State: AC+VfDyvOwF6C//i+Murqg8TFF6c8+toA4XjlJ69wpHNS+C2lp2Pre9K 4p7qI/7tMeO6DL5y30P0IQ0= X-Google-Smtp-Source: ACHHUZ54zTdupKNg/2XT9HU+SXhAIaJsk3+jnYSbwkYSEExJvJm5XZ9avCTE6uKiKVtGO06wcaFOug== X-Received: by 2002:a05:6830:1182:b0:6b5:8be9:3b8d with SMTP id u2-20020a056830118200b006b58be93b8dmr13146854otq.17.1687529780457; Fri, 23 Jun 2023 07:16:20 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16: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 v5 bpf-next 03/11] bpftool: Show kprobe_multi link info Date: Fri, 23 Jun 2023 14:15:38 +0000 Message-Id: <20230623141546.3751-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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 22 kprobe.multi func_cnt 7 addr func [module] ffffffffbbc44f20 schedule_timeout_interruptible ffffffffbbc44f60 schedule_timeout_killable ffffffffbbc44fa0 schedule_timeout_uninterruptible ffffffffbbc44fe0 schedule_timeout_idle ffffffffc08028d0 xfs_trans_get_efd [xfs] ffffffffc080fa10 xfs_trans_get_buf_map [xfs] ffffffffc0813320 xfs_trans_get_dqtrx [xfs] pids kprobe_multi(1434978) 5: kprobe_multi prog 22 kretprobe.multi func_cnt 7 addr func [module] ffffffffbbc44f20 schedule_timeout_interruptible ffffffffbbc44f60 schedule_timeout_killable ffffffffbbc44fa0 schedule_timeout_uninterruptible ffffffffbbc44fe0 schedule_timeout_idle ffffffffc08028d0 xfs_trans_get_efd [xfs] ffffffffc080fa10 xfs_trans_get_buf_map [xfs] ffffffffc0813320 xfs_trans_get_dqtrx [xfs] pids kprobe_multi(1434978) $ tools/bpf/bpftool/bpftool link show -j [{"id":4,"type":"kprobe_multi","prog_id":22,"retprobe":false,"func_cnt":7,"funcs":[{"addr":18446744072564789024,"func":"schedule_timeout_interruptible","module":""},{"addr":18446744072564789088,"func":"schedule_timeout_killable","module":""},{"addr":18446744072564789152,"func":"schedule_timeout_uninterruptible","module":""},{"addr":18446744072564789216,"func":"schedule_timeout_idle","module":""},{"addr":18446744072644208848,"func":"xfs_trans_get_efd","module":"xfs"},{"addr":18446744072644262416,"func":"xfs_trans_get_buf_map","module":"xfs"},{"addr":18446744072644277024,"func":"xfs_trans_get_dqtrx","module":"xfs"}],"pids":[{"pid":1434978,"comm":"kprobe_multi"}]},{"id":5,"type":"kprobe_multi","prog_id":22,"retprobe":true,"func_cnt":7,"funcs":[{"addr":18446744072564789024,"func":"schedule_timeout_interruptible","module":""},{"addr":18446744072564789088,"func":"schedule_timeout_killable","module":""},{"addr":18446744072564789152,"func":"schedule_timeout_uninterruptible","module":""},{"addr":18446744072564789216,"func":"schedule_timeout_idle","module":""},{"addr":18446744072644208848,"func":"xfs_trans_get_efd","module":"xfs"},{"addr":18446744072644262416,"func":"xfs_trans_get_buf_map","module":"xfs"},{"addr":18446744072644277024,"func":"xfs_trans_get_dqtrx","module":"xfs"}],"pids":[{"pid":1434978,"comm":"kprobe_multi"}]}] When kptr_restrict is 2, the result is, $ tools/bpf/bpftool/bpftool link show 4: kprobe_multi prog 22 kprobe.multi func_cnt 7 5: kprobe_multi prog 22 kretprobe.multi func_cnt 7 Signed-off-by: Yafang Shao Reviewed-by: Quentin Monnet --- 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..8461e6d 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 [module]"); + 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 Fri Jun 23 14:15:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290674 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 0B02BAD42 for ; Fri, 23 Jun 2023 14:16:25 +0000 (UTC) Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A41622133; Fri, 23 Jun 2023 07:16:22 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-6685421cdb3so1314679b3a.1; Fri, 23 Jun 2023 07:16:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529782; x=1690121782; 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=Du4qFBhVRDavQkjSfw1UhiKH87F0Og4EJXGDRJA8147dzuQf81Hrb1r0iSbe3Zcqob DUpB5PGz+8H5VIelumpfCsw1s+76Kh1xcvuJLz7HdhKGOuvBkm3Tb5XFwUPjaY3TsYOz cNDD0ugNByKP6tU1VyGXddf2TShai1657KLml4Qwk1nHJQWgVG438k4bypP7NmZKSEP+ u9hXgIGa0dvpKtXZYC2z62p7YIExGRi56bU2aHwulqlQnsmkoCs6GYgwTqF1IDGjrI0e wBEap7C6AX/XGu/EbUoMgbzKy1eqMe4oRt50FVtT5y081LQGHdWJSKYmjLigvjyh6JSw qdMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529782; x=1690121782; 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=doqAwBR1olxlHi//nHooGKZkLXF5lWYKRurM8KC2/2KinlVfdJQ01eyWAEYh+deWm/ CD9c7QnWn55xxmP1YdVbnmaftm00Bcas32UNfLlyICRgIAWRDKMvdkTy/f778hCAvi2m r2xPXvTfqbRcCMkWBpX4x6DCV43pHfvY2JXfSasQSjDBlyS+20U9p7fbPujkDZ5U2KkM T8s+8n9K9BgP42+0V2eFN2/suommcarh+usUkZOaubnWWB9yMLggyGXfAW5WA9FhQtTI j2o/D58MG1UkPn26fxYug1/Ms0rtmFKHQLTnJtKChWaalL7RLkDz/YThehEW1TXHCBhN TLgA== X-Gm-Message-State: AC+VfDzRGMnYMaK9NrTEug6Z2qgTktQLMzOn57b7womhQu91l64Pa+3W 94JyHAU4SYiwofGtDGUbWSs= X-Google-Smtp-Source: ACHHUZ55e8bjY7iXV2b6JkCIfWVjUPItrlMUjZIkfG+VE9WwjS2G3DTM5c5W7wEON9WwLU9b05P1cQ== X-Received: by 2002:a17:902:f68c:b0:1b6:c193:96e0 with SMTP id l12-20020a170902f68c00b001b6c19396e0mr3702156plg.1.1687529782117; Fri, 23 Jun 2023 07:16:22 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16: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 v5 bpf-next 04/11] bpf: Protect probed address based on kptr_restrict setting Date: Fri, 23 Jun 2023 14:15:39 +0000 Message-Id: <20230623141546.3751-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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..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 Fri Jun 23 14:15:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290673 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 E2AE6AD3B for ; Fri, 23 Jun 2023 14:16:25 +0000 (UTC) Received: from mail-il1-x133.google.com (mail-il1-x133.google.com [IPv6:2607:f8b0:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A60A1FF7; Fri, 23 Jun 2023 07:16:24 -0700 (PDT) Received: by mail-il1-x133.google.com with SMTP id e9e14a558f8ab-341c81d71f6so2493965ab.0; Fri, 23 Jun 2023 07:16:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529783; x=1690121783; 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=A98VvmhhRgKal2Xwydck3qqcSj5Uc9sK6yv+OTJRvdpBq/Pg3nCLWU5paYBbveCs0P XjpOwlqbeyPiIRqu19BL6MwkgAJKRaW9mcQnRt1BpO71Tc6IiHTMzF3AqlYW/Lf+kqa2 dHDFqh3cjXmCYVEq00prb011XYDhNHz++ZZd9N/N+PuK0xF9jcWno2N20E/n06OFHQN5 8E906y//EtbBTE0anwFLwb+vE+XsWcAkNSHqe+7DjwkIAEoTEapNtCilbjH7UO5kAgST INcEP7mozcrv0T6/vy4KziHhHQ4QTcmAJT9a5kdf2JZEHlgutF5L3nKf5Mbp3BC05WcQ /Gdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529784; x=1690121784; 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=eZ2sQiwl4NOYfC7ztaQxaR1f8uEeCMryj5j0OB8QbovmzuW6Wb4YW7V5qI65OYTpkk Ah3tXw3l00yANxxs+fWSENcz++ITFDlV1lEQgoR9Gq7MyQYLrHtYoShdSncfLalrrQD+ RkIRkmVM5Ad+Kd4NPRMezXpb41Zys4je/mSowu7WrLVO0zDTyQ0DdHggc/afKurO8QGq tlYGW7MpqGZ/wxZiKyy96zozC2iepPZRqDwTHXOV2Oy81MaNc6tSl/tqcvTlwyOF4wG3 FAhsCkc3HMaGR/FGP9RFYFRKHSiX8tLowPbM0mNbMvTwk6Do//Y2IFg8OUU+WGPawipO /jxQ== X-Gm-Message-State: AC+VfDyeY57E07D2wxOz95rJKWbeZQA7xBnGSZH6e+seh1u6c58kROYh doGwDOncU4mLRAcPatXLo4Lqw1ogCsbRUqDMln0= X-Google-Smtp-Source: ACHHUZ7SpwJpEwFCBkFbsemwIHOCNqBb+UaB8QDb3Ht2TGuVUzNAK4YdN4B+IggRiG4UzIgZjWtVLg== X-Received: by 2002:a92:d249:0:b0:342:55d2:d3bb with SMTP id v9-20020a92d249000000b0034255d2d3bbmr15655638ilg.28.1687529783741; Fri, 23 Jun 2023 07:16:23 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16: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 v5 bpf-next 05/11] bpf: Clear the probe_addr for uprobe Date: Fri, 23 Jun 2023 14:15:40 +0000 Message-Id: <20230623141546.3751-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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 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 Fri Jun 23 14:15:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290675 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 E60EFAD42 for ; Fri, 23 Jun 2023 14:16:26 +0000 (UTC) Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0D831FE3; Fri, 23 Jun 2023 07:16:25 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-666edfc50deso546391b3a.0; Fri, 23 Jun 2023 07:16:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529785; x=1690121785; 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=rc4C3ccfx0jwfetoVqdJtQiE1K+Bj6ap+X+TfgvxuOXVg1tRGjttnP4T2olJOa5hKm 2mCK0RnFB1HbBD/Yc00jlCFh8ANdNB31ru1xcfL9sfm47/TtcCKyIXKkHwSeLV3q/kg8 ydT1UgZ2lxdPBXI59AYStUdC6jCY70y6w2x8qCPc7qZ4M/pb6junIiUVQ4y2XmHrjWvo LSIUs7TVPp4u8CZ1BFn35gmLgslHR1RqEeovtkMZpZBIcg7LDHx5qjEADscHBnvPN6Ym o3GUz9Z+F3480hGU144fc3e2Xo1yH/8fWYR/jLfaEVlZsaIqkxkJSK45L4qJi6ohkiYh ITjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529785; x=1690121785; 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=H/2a+tGJQ8npukiVs5FOarT+i3bt16+rTR+526nLFWQpGS1uOqQUJwuRhFCplYdQE4 rIYUI145ERFTjw81Pn/EEq+INvCZZSL+0CSCUTydp4Mv6ZKl5ivPUs6vQWEYYfvJSTgv TW2LMfY0o+vl0gk3b+9BEMtR020EC7YKLWgdzJifH4U2rXMsmmTG6+KW1dUMV4t+JszE +jGkd77vwW5phyUM8R82KkU9JXedjG+BcIYWNJCJlHkTqIGFn+eJWFdROvdoi4thdq9E SoFDUG8DAUGATPWyGezmVT2UvBDBSABjxBx9a7E5UwU/0Y5fWySq0R8oa+k6tfMDETrJ ZjgA== X-Gm-Message-State: AC+VfDzSREgnOK3w9L9By3HTXiuWWVkWDqM1yyhs9uHYdzO3pGKIMRxb wf67Lc7tqWMHe9lQG4zABeQ= X-Google-Smtp-Source: ACHHUZ4oOBkCzl13+tx5iOSJz7xToUEBctfORkjSC7M6BiBU+LQQrZS3eeccQiFD8jdE6zKiMCw+NA== X-Received: by 2002:a05:6a20:a122:b0:103:7b36:f21 with SMTP id q34-20020a056a20a12200b001037b360f21mr36271012pzk.21.1687529785347; Fri, 23 Jun 2023 07:16:25 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16:24 -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 v5 bpf-next 06/11] bpf: Expose symbol's respective address Date: Fri, 23 Jun 2023 14:15:41 +0000 Message-Id: <20230623141546.3751-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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 | 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 Fri Jun 23 14:15:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290676 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 266C09441 for ; Fri, 23 Jun 2023 14:16:28 +0000 (UTC) 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 B161C1BDF; Fri, 23 Jun 2023 07:16:27 -0700 (PDT) Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-1acfce1fc0bso544507fac.2; Fri, 23 Jun 2023 07:16:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529787; x=1690121787; 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=VR64Cmt8i5FJsJGupQTPYJtgTllYuN+cFKijdvjcj5o=; b=EQ7iNkE7ti0f7zBc5e//BENDi552fhrE7IpR9u5FeBTGZdzSKCVDhsvM2Aa77gqDcw sHKdRyqJcO2vIufs5qlCx7uo6hwODOzuWkI689nmevebIK2EB1ezWJ3brS8bEjJ23am4 NibywcXiPYFOMaqeMb9/oJo889m0lADlR6hkV67HhHPk6LWx0CJvDmHgyB85yZN5Uxg6 1D9RPnzxN+LVS20sHas8jQ7F7nu+b29jTqE45Dj3z/+Ta2PCoRGzx1FkjQVeH4Bq9Pk8 Kop+RjrkxNBSp6X1gt/dU1041lqsn+Wb42D8ROdytdWsycyXSfXv9ZhRbFzrkeoeHYef zgwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529787; x=1690121787; 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=VR64Cmt8i5FJsJGupQTPYJtgTllYuN+cFKijdvjcj5o=; b=CmXotASDEYeYgS5EPiFmHMj6kMUh5ckTDZcpxZ9o3XfvBwqvFuJaSa4WANxfJUq7Gn 57lrQwYLx0B7if9VpMXOSCSgvEJfaPl5cH9c/dFn9uZbc10QcoQ7RWzYpXgvEQJWjJmp ogg6mKWjDHRmvInvcwXpSOCHkRdlv5En0YY5OEYfQxWKp974xc9HZIQzf26+VsN6mf63 ubSsUqBWLfrCVDUhSGuRWXa7XXHwdwVRpX51s+wHEApFWE7uuYvU4KUCoSaMr3dcOp7c hwxtHQhQccjDkb1fLAMdwIzeLsVKJevnT/M8IjWwJX1mEXCIymxqQx0M2UiEW6DklMP9 YBvg== X-Gm-Message-State: AC+VfDwrPc1wIjDfp9LjKP7aiS7i9ql6spsDujNmOwh4mx2ne6qEuJKf m3ypAS1brWp5GKW/fImbWNE= X-Google-Smtp-Source: ACHHUZ42HZuuQHS2WbQ0zQXjoMb8L5bxZPsRe8wyzSeG/Hj0exEthhJb++8LC4+zFak4KVBYeS12DA== X-Received: by 2002:a05:6870:c805:b0:1a6:c04a:84ef with SMTP id ee5-20020a056870c80500b001a6c04a84efmr10844401oab.6.1687529786912; Fri, 23 Jun 2023 07:16:26 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16:26 -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 v5 bpf-next 07/11] bpf: Add a common helper bpf_copy_to_user() Date: Fri, 23 Jun 2023 14:15:42 +0000 Message-Id: <20230623141546.3751-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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 a75c54b..f3e2d4e 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3291,6 +3291,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) { @@ -3309,20 +3328,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, if (!ubuf) return 0; - if (ulen >= tp_len + 1) { - if (copy_to_user(ubuf, tp_name, tp_len + 1)) - return -EFAULT; - } else { - char zero = '\0'; - - if (copy_to_user(ubuf, tp_name, ulen - 1)) - return -EFAULT; - if (put_user(zero, ubuf + ulen - 1)) - return -EFAULT; - return -ENOSPC; - } - - return 0; + return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len); } static const struct bpf_link_ops bpf_raw_tp_link_lops = { From patchwork Fri Jun 23 14:15:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290677 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 498619441 for ; Fri, 23 Jun 2023 14:16:30 +0000 (UTC) Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CF9A2133; Fri, 23 Jun 2023 07:16:28 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-666e64e97e2so383965b3a.1; Fri, 23 Jun 2023 07:16:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529788; x=1690121788; 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=85X6KQWnSdNZoA/Rwsv1xnmZmalDiEqK88cz5kJKV98=; b=MrdTVZyjilVoB+3VqpPfbeQHN0JudOYuw5cRBU38ssk7QxLwBGmGx8Rhex4oP94etP sn4C5pfwZ+w+qr6cjdC9W5wLDHv00KcZRx9CPVycxnAITTjQur8Hc7qdIwuYMf5RSSSd SFekaXl0wUCh24IvcRI56kqenXBAKFfA06ePXZVOvmGycXIZMXCo0nKtKZruwh2dR38y s62k/fTk8osiJYWYoO/odYfvAqgYddsRNRI6eqoqofWELSUzG5f+kGekFsEVyjskhcAC a9GOjZwXQ5I3IOa1fUS15nqbo2foglbVbZ+6uakNxVRpSfPzhGERF1ZzuSLkN8MlMtoN JvQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529788; x=1690121788; 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=85X6KQWnSdNZoA/Rwsv1xnmZmalDiEqK88cz5kJKV98=; b=bmFEj/+i0c2MAt4PQU9S8fAUP8j++U/1f6dp74Kr6g9xsRNrDNip23e0NS2Qmn2YBH 3Vi7azHd8Y9QbI5FFyLBKtGHVP+efoqwtSSMJ8fq3Hka8WboLMd1YitWIWConu17KScw +LrmRsLGdBJvCF0VOGlRjQJfnLlLgfog0vLaegLx8T7nQ8P3VdNqxApmxqUyRWeJPlCi 41L8vrWnkC9mUCzb7xIbwrEBYK+ihf/WuGNhuYyZoIFCXLRjqpV45nnTZhSmmncm/gLU We2qHjZydeCJyx2uY9ew8GKeRYEeGp3K2ao8hOK4H4sdhhewCqlkqzo0hR1kMf5yG6VP MLsw== X-Gm-Message-State: AC+VfDxRYXSr5INarJf/rJkoCV2yjQ1ucB7umsqRRSw6eQyhPNqEDPWN ZxwfCPZIcFYY+sK5G/yuqE4= X-Google-Smtp-Source: ACHHUZ4LCd/8ayM9s6bHAwLApOEWnaDLX7aNc8RZfJbO8you44CgH+g8BLlMkKd5XoVqBeMeJWKDrQ== X-Received: by 2002:a05:6a00:2291:b0:668:7fb2:d9a9 with SMTP id f17-20020a056a00229100b006687fb2d9a9mr14511080pfe.17.1687529788464; Fri, 23 Jun 2023 07:16:28 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16:28 -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 v5 bpf-next 08/11] bpf: Add bpf_perf_link_fill_common() Date: Fri, 23 Jun 2023 14:15:43 +0000 Message-Id: <20230623141546.3751-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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 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 f3e2d4e..c863d39 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3360,6 +3360,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 Fri Jun 23 14:15:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290678 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 239319441 for ; Fri, 23 Jun 2023 14:16:32 +0000 (UTC) Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 899BC2136; Fri, 23 Jun 2023 07:16:30 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-66615629689so454751b3a.2; Fri, 23 Jun 2023 07:16:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529790; x=1690121790; 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=VooKI3hb6tmxFHhMRTTom+VhnYdoKpXBVCANjBy4MfQ=; b=ncUrBjvlwlXmLo+ysV5yWmp16yQuuxKlVXdc5/y+BfRFyRB2t8UM2ywf/sORMnsdNd gbLL8MeQH8pyo0eVnlyNb87jLVEauuhxAkHIHsyScqylycxmY5sGGc95SPsLz+ihvGvP prUV1hfIR2iMGGorCDzB34Ik9wy4EzJ8wHGu1X217OpZTY6rnV4J2Xwi46HVZIRoID9i CNpcMawv6zIsHApcVJbNIaql6XEPvDWuV1LySh2iDkKwx3wKMAg/2SAko0pIY2xS8d9H Io1qCKeeJjTKBN3shIZLnYqJEt/IRlTArgb6i0xBr/8zKCpPu152rRJe2Z0MdwqsmpRG 2Lgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529790; x=1690121790; 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=VooKI3hb6tmxFHhMRTTom+VhnYdoKpXBVCANjBy4MfQ=; b=SVbFZUuKg5udk8a09Rqe/Unt1KPP7PtdVT2ZCAWEfk01Nin/UQ2CXig4EqsNJlEg9g YZuBoqYBLNaNWyFOHnMgGVnENvLkIgikpwkn16Nw+W6Y29GdGw5DZJujmHxM7Ry2WQ6W Nut4kbEBEs6g9CjfdMil0fdXZMnzPTFSp3Pp+KVqcAihQkmm7YmRQ2OcZHBYhjgx8/uv Mnp7XgNV7VecO0/FuB6u66giVZMFaNjgIQGqFy6E2tbXzT7cwrCqEeMjrRJ2GZGMNJbk FoyE1b3U66r86zza5qUeP2c4XpYTxZz49yhEJxu8sZsS5nnjv4tNPjzmzgRPuO8UPYRO CHag== X-Gm-Message-State: AC+VfDxfa2v157MOvZ19ZDqlO4Fplp7kLIkzg6Yy+cp6NXYf5Ii5dnDf ky44RnbXfrZV0TSgIyxoEew= X-Google-Smtp-Source: ACHHUZ7Psykz1FdscsIEBVLsfywTaykysuOsf8iIn6OH0aLYc6yAj/nxsShMMYJM+/hTtmOZ1rU3uA== X-Received: by 2002:a05:6a00:a8a:b0:641:3bf8:6514 with SMTP id b10-20020a056a000a8a00b006413bf86514mr26094155pfl.10.1687529789924; Fri, 23 Jun 2023 07:16:29 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16: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 v5 bpf-next 09/11] bpf: Support ->fill_link_info for perf_event Date: Fri, 23 Jun 2023 14:15:44 +0000 Message-Id: <20230623141546.3751-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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 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 | 35 +++++++++++++ kernel/bpf/syscall.c | 115 +++++++++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 35 +++++++++++++ 3 files changed, 185 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 23691ea..1c579d5 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1056,6 +1056,14 @@ 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, +}; + /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command * * NONE(default): No further bpf programs allowed in the subtree. @@ -6443,6 +6451,33 @@ struct bpf_link_info { __u32 count; __u32 flags; } kprobe_multi; + struct { + __u32 type; /* enum bpf_perf_event_type */ + __u32 :32; + 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 c863d39..02dad3c 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3394,9 +3394,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..1c579d5 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1056,6 +1056,14 @@ 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, +}; + /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command * * NONE(default): No further bpf programs allowed in the subtree. @@ -6443,6 +6451,33 @@ struct bpf_link_info { __u32 count; __u32 flags; } kprobe_multi; + struct { + __u32 type; /* enum bpf_perf_event_type */ + __u32 :32; + 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 Fri Jun 23 14:15:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290679 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 6C0A79441 for ; Fri, 23 Jun 2023 14:16:33 +0000 (UTC) Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5752D1BC6; Fri, 23 Jun 2023 07:16:32 -0700 (PDT) Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-1a991886254so557923fac.2; Fri, 23 Jun 2023 07:16:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529791; x=1690121791; 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=mUX+eNaIsY1dCSY5M/dWpNxYFFpDcEU4gYYhWrPWXOA=; b=JkK4hR8qd0peqaVuUqe0oWMXg284QvQKB4FpaYbZrYPTOs5y2XIW3f4PrGvqW9ncZ1 iDRmtPMbKoioctbgNs88WydCZWC1fRnv10g6EEWe0i4I+Px0tYdPm3DPkVtyxD3iuw1+ oxGOr14xK84Dvc2R7K6++yPBXG1+fFl535qN2JksDfDE9PXK3s6T3KxUgmLFKMnTG+Zq Mz9ckHeOYJkO2FSRW3vi2wUXooNSvCdB7CS7vykWB8MKGH6NFiftIaECjvo31zb93VxG c/miyNVjlY+FkPT0jS9MPeQFnejIpJ2qTI5vQUN8zYdYiGXFmCfffS/jga99kgUIxOOa fXlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529791; x=1690121791; 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=mUX+eNaIsY1dCSY5M/dWpNxYFFpDcEU4gYYhWrPWXOA=; b=bjw4DF1CDS4RS7Js7Sbh9q4+uxC05SHAWInEGnNgqYALSpzE6v1NaLJ6Vzja/v2dw6 kpL8JrdofsBm15YH5jymCli2+7uE244p5QtnrxvDlGpCnrVQoWIOmr6mgbM8+Q1PmR86 QBAX9IkhX1QUPMNhNR2Mf6lrngNmkO1nhZuyyBfKu6U/1fCAtlCZrRH3CEzKfZF8hVvR 8c3pnW/mVPjT1qdiCUdoAoQdCuJ7iSCvqrexgY9DrBxFyv2bvsP1uvnc2Wi/Eh5kYIab Z8VYaXbYflHf4VdJsKthPID/12YcshfjFLV8Us3jVq8YnWFZ0anvCU4NSIpEgXo8Ymkr Yn6Q== X-Gm-Message-State: AC+VfDwxMYz2CCbYMoI4fK5eoovBVwNiZ1i9uIJP+FZ/sWyI1mKVDHNQ A4njNwyeFtGmQXZRUV/ra5A= X-Google-Smtp-Source: ACHHUZ5D8E+x6QZx44clwxkP3FNsj559M0UdWNKUW8M8tUoDmUgVLoQBDbb9fp/zrF1HLYSn1k6org== X-Received: by 2002:a05:6870:3895:b0:19a:2343:b69e with SMTP id y21-20020a056870389500b0019a2343b69emr17367598oan.40.1687529791543; Fri, 23 Jun 2023 07:16:31 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16: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 , Jiri Olsa Subject: [PATCH v5 bpf-next 10/11] bpftool: Add perf event names Date: Fri, 23 Jun 2023 14:15:45 +0000 Message-Id: <20230623141546.3751-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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 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 Reviewed-by: Quentin Monnet --- 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 8461e6d..e5aeee3 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 Fri Jun 23 14:15:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13290680 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 222419441 for ; Fri, 23 Jun 2023 14:16:36 +0000 (UTC) Received: from mail-oa1-x36.google.com (mail-oa1-x36.google.com [IPv6:2001:4860:4864:20::36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E58032135; Fri, 23 Jun 2023 07:16:33 -0700 (PDT) Received: by mail-oa1-x36.google.com with SMTP id 586e51a60fabf-1ad10ad65f1so449397fac.1; Fri, 23 Jun 2023 07:16:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687529793; x=1690121793; 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=ZJ6T5kwycyurskhmS9zj3bwZSicgtrlBOojL4fycfgc=; b=k/4ZxUlrglsqz73krpHvkxhCK5A/rz4XF1t9/Oqzh5EGBij/ZMkUwnN+N4nrIwydSu 3a9bh29R4glmmQzCIQMi29Pu70RHnSbr37sWyN/OCCq3RLnSkKo4QZOte0LQVP//fvX3 OSojQQcSFx3ZvOFbYEMnIzUL8Ofbsq1/FS6rk9W+6skYcyLtzwvyyofiQgkuB9TOeAjC RRvjIe3JFq5nvpHJvF2iE0MbrsSZ1SaKm1AVd39OMlX1s1DAh2Jde101vhQB92eSz+u7 XuLffyd1/quAG23DjXD6CLI3pY51ZCAHhsJ88hTu3gqT5+Yg52iTj7LjWHlVRhreDqYD 8B6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687529793; x=1690121793; 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=ZJ6T5kwycyurskhmS9zj3bwZSicgtrlBOojL4fycfgc=; b=LWruusy64zC7wIEN3SSFmEbQG+vjImcr7K5qkM703Ti3rfel+3BPmqwYhv1Mtkc04k qYSYu8EtNUo6SPZCOKVYznVCBGjkiylsJmJUb9f8+U99GUj6n7vdi3UAtM7oGRqStDJY baJxQIvBca0klxmyG47uq/0lduuqkcQQ/oINPs7+3kbBlhPRC1OBNkoUiUvymvGVT+EF LJe49HYc3f89rNDbcP1S0X00N6lvysKHFl+RO6XZNs62l4O1lHEClH1u2Gp6Em+sBa+M cFWiQrxEr5V2O1RbMHI1jahb3ntG9U+dvJhU7qHkZkq4YaEWCpsCVZcV78GypNtTB4J6 13Ww== X-Gm-Message-State: AC+VfDzJ5oI0n9TZb9IwPH0qJ/WaQIvozWKNZszI2tojHI8ULyf+zFx8 bSbMCdV1IEHJE+nC68atk+0= X-Google-Smtp-Source: ACHHUZ5GJ+VVnmgFB9yXlUrABi2709l51cNIcBp9ZaCrWwJil7lcvcKzpN7RJi9ziYY+QmwRvNrDYA== X-Received: by 2002:a05:6870:9885:b0:19a:695:15a5 with SMTP id eg5-20020a056870988500b0019a069515a5mr13606405oab.25.1687529793063; Fri, 23 Jun 2023 07:16:33 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:1058:5400:4ff:fe7c:972]) by smtp.gmail.com with ESMTPSA id p14-20020a63e64e000000b005533c53f550sm6505942pgj.45.2023.06.23.07.16.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:16: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 v5 bpf-next 11/11] bpftool: Show perf link info Date: Fri, 23 Jun 2023 14:15:46 +0000 Message-Id: <20230623141546.3751-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230623141546.3751-1-laoar.shao@gmail.com> References: <20230623141546.3751-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 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 e5aeee3..31bee95 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