From patchwork Mon Jun 12 15:15: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: 13276834 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 24CB2C7EE2F for ; Mon, 12 Jun 2023 15:16:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239450AbjFLPQ2 (ORCPT ); Mon, 12 Jun 2023 11:16:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238430AbjFLPQT (ORCPT ); Mon, 12 Jun 2023 11:16:19 -0400 Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22141E63; Mon, 12 Jun 2023 08:16:18 -0700 (PDT) Received: by mail-qv1-xf2d.google.com with SMTP id 6a1803df08f44-62de5392c7bso7809416d6.2; Mon, 12 Jun 2023 08:16:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582977; x=1689174977; 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=MWqmDiEzHKgFvHuqwuHTHpXuTD6o/LnYiSeoxlbJNGU=; b=AiOKuw4q7501E7JJ3RUs+Ar6uL7O0KL0o4e0BY+DX0bmdDyevJadnjuNLw9Qa448KI t0i/qEGzuqe+CImXulkA40zjqK5IRClcuaBsHxDlI1PRcmZ7mCYU/F/VH0mTYZPem9Ez 64HLVdwKKn3M/fHaPXzNMARKqMnihvQ+3W5Jkrj1hP4GxFYnZwx3zNqo+vIUELNjDiT1 CP8GYDMPFnu90Y0iywaHg0gZZKTKYysQoDUb5C5s+sDHbkjv58QKXwAmarMjjzAEhpW9 D5Yz1l6cbgmmsD+HMzWe8uSTebyXxX3uvKoS5LoPzRqKTHq5YlyRfrqxvPygudU7F5LW NsKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582977; x=1689174977; 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=MWqmDiEzHKgFvHuqwuHTHpXuTD6o/LnYiSeoxlbJNGU=; b=E+5SC6VCgL6/ZEDSbC374YLZTNdmvn8S5Ly0EPDP7Rrq0fsSVf4jXb7fSEGYNVDN17 9qH4Zgh5LR+o9DBoPmbYyPAmnSG/DscB6E6bjlj/OHxApg25yYh1NjtKlEyKE9lzaeU1 crXr3vhdWcY750wrdxseJ8scF4wQhB7Z3ECJRq19jnBufMODa22veG0MdAp464Z/KnOD Qf5fyTX/4oCW+kqgKtYDgbPlxUbdH/qI9SOliTl2HD5mBq4fhZuRl/PyfcwPTrUIqwLF 2C8qcVbAThUX7FuReOClkhNgc+AP/ZQjw9d4T2sXJQgl+fG1SPcY2s+s7Dx+TEM2cF5j GjqA== X-Gm-Message-State: AC+VfDwaotRaX7ag1XIzTqINqqzg4ow19+iM/b1Ob55ht3z7E8J1q6Hf Kv8yxEtXyzm2E5HE5KV6HpU= X-Google-Smtp-Source: ACHHUZ7lkiw3vkuaXeYb3gsez9A7gf7zO12Nou/7uw9jIc4lhB3Hf8kOxIIQNYjLLIhIoxpZ5Y2stw== X-Received: by 2002:a05:6214:130b:b0:623:42c5:612f with SMTP id pn11-20020a056214130b00b0062342c5612fmr9115703qvb.49.1686582977138; Mon, 12 Jun 2023 08:16:17 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08: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 v3 bpf-next 01/10] bpf: Support ->fill_link_info for kprobe_multi Date: Mon, 12 Jun 2023 15:15:59 +0000 Message-Id: <20230612151608.99661-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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..742047c 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2548,9 +2548,36 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) kfree(kmulti_link); } +static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link, + struct bpf_link_info *info) +{ + u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs); + struct bpf_kprobe_multi_link *kmulti_link; + u32 ucount = info->kprobe_multi.count; + + if (!uaddrs ^ !ucount) + return -EINVAL; + + kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); + if (!uaddrs) { + info->kprobe_multi.count = kmulti_link->cnt; + return 0; + } + + if (ucount < kmulti_link->cnt) + return -EINVAL; + info->kprobe_multi.flags = kmulti_link->fp.flags; + 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) @@ -2890,6 +2917,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr return err; } + link->fp.flags = flags; return bpf_link_settle(&link_primer); error: 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 Mon Jun 12 15:16: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: 13276835 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 B3B59C88CB4 for ; Mon, 12 Jun 2023 15:16:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235862AbjFLPQ3 (ORCPT ); Mon, 12 Jun 2023 11:16:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239491AbjFLPQU (ORCPT ); Mon, 12 Jun 2023 11:16:20 -0400 Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B269E57; Mon, 12 Jun 2023 08:16:19 -0700 (PDT) Received: by mail-qt1-x82d.google.com with SMTP id d75a77b69052e-3f9e207f5f0so12343291cf.1; Mon, 12 Jun 2023 08:16:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582978; x=1689174978; 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=Xkj+HxvtsWB3P7BPDfdU8j3ErfZlhCpn+4TayEbVjl0=; b=WLDguKq4d+ZIiQk+a0THiwRYc2SOqsEyA9pgexw1AOcr4KaoZZQApukXsQ6jp25XD4 wZOIWlxvEd+bfzQHNJObTfWUEdi90f9F/fUryjXB/K+lJsRriN4lPNz7ZrocAxKQxuDZ LCFXx5HBHjuTthSdu2Acu/aIJrPygubJuoZCYWBOGI4zw/SUmqKTbFxZiLXA7ToMSkWC PzLW8w7MIveynGvbCyMFySE35DglbK9D/OSUgvlgYDDFn5a6NCTI2OqjN7KTbTEEWzuL 411IeCcyo2z7h7HiTq59MrMA1gTI2pY3ioKqNA7ZGvO3slOiKxYriuJQ0LDjFxkYZ2xH U5nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582978; x=1689174978; 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=Xkj+HxvtsWB3P7BPDfdU8j3ErfZlhCpn+4TayEbVjl0=; b=YZ5pV0AjuSWKB7oNQdpzgQtKxEM45vmyr4fUKLzSHFesd5x/spqfuXcU68vi2p9LRe DndWnSFoLsDgH6g2NT1UecR1XJYXdWaH6RJdVLyzBMED5Ljgj1z9zSOpgwUa7vzu7Zuy +WlWDIv1zcHR2ybeAwNPfG8OZbw1gaNU1a5XuxkFm0TKmJX3KJuUrdMFJzbV/H7kQ7+1 EGZ/r9L09/HkMmbGf1xOG9YzvVSboqeFIDcnr6tZSWqJN1WGHHSKQvmxrQNf1/TSooZ/ uNws3IPKEfhKH0M4gzgSM4adoIY77yb1RigmURnymIHKGtx8AiiNWAUuM9UhM9kRKyos XfCw== X-Gm-Message-State: AC+VfDwUBeyIJCVYs6k+klj4wdwRBU0Iaa+68e8ydUl8FO2AnFvrbzgz Zjx2JszddVePUzBt0gBF1Fk= X-Google-Smtp-Source: ACHHUZ5HcCz0cl2iTr6Y8+ezyIij3ZOgt8SA+gKvZS3o1j0k7i6+rD59Ssx+WlLbJlMSV5MGaHW9aw== X-Received: by 2002:a05:6214:268e:b0:62b:6f7e:f5f with SMTP id gm14-20020a056214268e00b0062b6f7e0f5fmr16315744qvb.23.1686582978149; Mon, 12 Jun 2023 08:16:18 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08:16: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 v3 bpf-next 02/10] bpftool: Dump the kernel symbol's module name Date: Mon, 12 Jun 2023 15:16:00 +0000 Message-Id: <20230612151608.99661-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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. 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..dd917f3 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'; + 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..5df8025 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_NAME_LEN 64 struct bpf_prog_linfo; struct kernel_sym { unsigned long address; char name[SYM_MAX_NAME]; + char module[MODULE_NAME_LEN]; }; struct dump_data { From patchwork Mon Jun 12 15:16: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: 13276837 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 EF5A0C88CB7 for ; Mon, 12 Jun 2023 15:16:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239524AbjFLPQa (ORCPT ); Mon, 12 Jun 2023 11:16:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239496AbjFLPQ1 (ORCPT ); Mon, 12 Jun 2023 11:16:27 -0400 Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com [IPv6:2607:f8b0:4864:20::f2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62A9DE7E; Mon, 12 Jun 2023 08:16:20 -0700 (PDT) Received: by mail-qv1-xf2c.google.com with SMTP id 6a1803df08f44-62de1a3e354so11409256d6.3; Mon, 12 Jun 2023 08:16:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582979; x=1689174979; 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=8l7b5rq3eSLprlEi3xdXeP4h4jMB/EYIkVxo2L3a3jU=; b=nCKBAmq1X5cFQGJE35tTaTXsbcw0BbKgWBUviilansHhDb3Ek1becaiWeg0iw3PnHV KDjppXU+qtRlNEfUaMt44GMbeIVQjNOcSHzPFmm1dAWXICIxLihgF6UYTE0cXvk5Ycwf 8cCRq7Eq1YSWF1H6ELedhmpg4UNz01aw7fg3ihfi20OVf9tJUqSLr2H8EORmI0JoDUGx zq1B+FbL04WjhizZSWS7slUW9O+nP9PugRqO3483lLh9yWUaxClker7W1SkFXzUEe2cR 3EdeFry6ZnHIF/I728dUH1lJuIq64p33BOvx/mdw+1Gcm0MB51Wc5XTf0Nsgdtuqja1j XEEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582979; x=1689174979; 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=8l7b5rq3eSLprlEi3xdXeP4h4jMB/EYIkVxo2L3a3jU=; b=c3nVfKisaQrOpEUe4rJ9saDJZnCvcEls4vSJYMKguPpTdViLRoZV+MymGagCYTI6WQ rXgZuWwYnAryv3Zn2fGARt8UeA6qIWN0NkmZ1a7EndtmEAZ0ckNl/SAWgl1/eRaXH2wB qWhzUBFNHDqMMn0N1vsaz+tf2KJFDBaoNb6QvX0QQw8npeCwuVgYiYvCph4CiOUdoers tODzOLci4TJWc6pA6+Q3TkQiHd8cJyfZLKenS2XFHamZA5Gb8ENR5Qpn6cGCY4rsoY7L l6l6+X+/t2uSYD4la64s7EELyog8s6M2L4smIBIRx0Lt3dRxn5waS2rLJNhQsagB0Coi J0bg== X-Gm-Message-State: AC+VfDysqOnxbudPhaflqiMqbKiSD72GNLZmlho6Zk7kSZmDRgsOMsxK 4nDWIo842nEy7nrbu8n2TPE= X-Google-Smtp-Source: ACHHUZ7FlQpBDj5TzocpiybhF7zTRou0gS6GHMTj5lNL3PW+Oj5TUDNQ+rKGPdKiw4Hte1tOgVF8bA== X-Received: by 2002:a05:6214:27ce:b0:5f1:6892:7437 with SMTP id ge14-20020a05621427ce00b005f168927437mr10405139qvb.26.1686582979339; Mon, 12 Jun 2023 08:16:19 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08: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 v3 bpf-next 03/10] bpftool: Show probed function in kprobe_multi link info Date: Mon, 12 Jun 2023 15:16:01 +0000 Message-Id: <20230612151608.99661-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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, 52: kprobe_multi prog 381 retprobe 0 func_cnt 7 addrs ffffffff9ec44f20 funcs schedule_timeout_interruptible ffffffff9ec44f60 schedule_timeout_killable ffffffff9ec44fa0 schedule_timeout_uninterruptible ffffffff9ec44fe0 schedule_timeout_idle ffffffffc09468d0 xfs_trans_get_efd [xfs] ffffffffc0953a10 xfs_trans_get_buf_map [xfs] ffffffffc0957320 xfs_trans_get_dqtrx [xfs] pids kprobe_multi(559862) 53: kprobe_multi prog 381 retprobe 1 func_cnt 7 addrs ffffffff9ec44f20 funcs schedule_timeout_interruptible ffffffff9ec44f60 schedule_timeout_killable ffffffff9ec44fa0 schedule_timeout_uninterruptible ffffffff9ec44fe0 schedule_timeout_idle ffffffffc09468d0 xfs_trans_get_efd [xfs] ffffffffc0953a10 xfs_trans_get_buf_map [xfs] ffffffffc0957320 xfs_trans_get_dqtrx [xfs] pids kprobe_multi(559862) $ tools/bpf/bpftool/bpftool link show -j [{"id":52,"type":"kprobe_multi","prog_id":381,"retprobe":0,"func_cnt":7,"funcs":[{"addr":18446744072078249760,"func":"schedule_timeout_interruptible","module":""},{"addr":18446744072078249824,"func":"schedule_timeout_killable","module":""},{"addr":18446744072078249888,"func":"schedule_timeout_uninterruptible","module":""},{"addr":18446744072078249952,"func":"schedule_timeout_idle","module":""},{"addr":18446744072645535952,"func":"xfs_trans_get_efd","module":"[xfs]"},{"addr":18446744072645589520,"func":"xfs_trans_get_buf_map","module":"[xfs]"},{"addr":18446744072645604128,"func":"xfs_trans_get_dqtrx","module":"[xfs]"}],"pids":[{"pid":559862,"comm":"kprobe_multi"}]},{"id":53,"type":"kprobe_multi","prog_id":381,"retprobe":1,"func_cnt":7,"funcs":[{"addr":18446744072078249760,"func":"schedule_timeout_interruptible","module":""},{"addr":18446744072078249824,"func":"schedule_timeout_killable","module":""},{"addr":18446744072078249888,"func":"schedule_timeout_uninterruptible","module":""},{"addr":18446744072078249952,"func":"schedule_timeout_idle","module":""},{"addr":18446744072645535952,"func":"xfs_trans_get_efd","module":"[xfs]"},{"addr":18446744072645589520,"func":"xfs_trans_get_buf_map","module":"[xfs]"},{"addr":18446744072645604128,"func":"xfs_trans_get_dqtrx","module":"[xfs]"}],"pids":[{"pid":559862,"comm":"kprobe_multi"}]}] 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..0015582 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_uint_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; + + printf("\n\tretprobe %d func_cnt %u ", + info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN, + 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); + for (i = 0; i < dd.sym_count; i++) { + if (dd.sym_mapping[i].address != addrs[j]) + continue; + if (!j) + printf("\n\taddrs %016lx funcs %s", + dd.sym_mapping[i].address, + dd.sym_mapping[i].name); + else + printf("\n\t %016lx %s", + dd.sym_mapping[i].address, + dd.sym_mapping[i].name); + 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 Mon Jun 12 15:16: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: 13276836 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 897DBC88CB6 for ; Mon, 12 Jun 2023 15:16:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237944AbjFLPQ3 (ORCPT ); Mon, 12 Jun 2023 11:16:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237592AbjFLPQ1 (ORCPT ); Mon, 12 Jun 2023 11:16:27 -0400 Received: from mail-vs1-xe2f.google.com (mail-vs1-xe2f.google.com [IPv6:2607:f8b0:4864:20::e2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 378D610C7; Mon, 12 Jun 2023 08:16:21 -0700 (PDT) Received: by mail-vs1-xe2f.google.com with SMTP id ada2fe7eead31-43b56039611so802625137.1; Mon, 12 Jun 2023 08:16:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582980; x=1689174980; 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=jAMB3BAwtwsq9nji55JfquI9Qr8zzbiwQWdCMgNif5R1/4cb8dlr5x8BgztqBrwvIV XLhCuwpjSIjQEqRtI3NpK+rlmwNhWh+OiBq2RvfYz3bXEZpDryDcxZwXypFSSJ90CjeW BRVKRC1SiXfnKVyzk4tWutd8ITR8TzYFEqCry6GSLTBsQGci10jj24M5vbvesyRGBddc moXi6X8xTfv/uNo6Nlqw99K/R00h5wrqBI7tdt/QOVPT4OwZfqv4tca9npFst/BgEr3X 93NuC6pYogGjVdAj69Qo51KNiI6UanLOo95BFXgs+waNK3duc+0LGGCFTL57iEQBXbmA qQ5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582980; x=1689174980; 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=R1mNsv1wOFqJ26gYQkLN4d4OGgo70u24+JurNIFaRvTLZMVIgFD4D0IX3pPfMU5ikL 6tTiPvZ8SSo6GfdQYxERPWPFOV53sIYk6ERDlvBN3k48TlyxX5j8eqqjFiTsl0/uRbxp 6zCsCv3/bq2bq2YF5jSd+4dGtQe2JUK9jdY6MyCbz4V9etPW5xIvNoQRk63tCGlrV3OQ fOFqq+axSv5OefGqxlGMy+Fv5mzSNK/bnQ1rN3t01pM4oDoIznRj/zQVaivyTdoreWka 2fC0WWKTPhZdSxCAuqc7TS0jTzW1MpE8HNVpYy5h4WefLyyxuqU0F8gvDa4PvCb/CUbl CxzQ== X-Gm-Message-State: AC+VfDwIG7iZuyZO4fR2cpnz9qMfTDsGeDXc7mE04aw7u4eJy4P0PYda P0VSUrTWRBeHlZd9da416FE= X-Google-Smtp-Source: ACHHUZ5ILCbD9Vji7iqnZ1bZOvJR0jtZH1GU7vT2Piv/rlTyVMdErvt/kKy2XdlyOiMT0WesjueT9Q== X-Received: by 2002:a67:f85a:0:b0:42f:e944:7ea0 with SMTP id b26-20020a67f85a000000b0042fe9447ea0mr2527590vsp.6.1686582980213; Mon, 12 Jun 2023 08:16:20 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08: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 v3 bpf-next 04/10] bpf: Protect probed address based on kptr_restrict setting Date: Mon, 12 Jun 2023 15:16:02 +0000 Message-Id: <20230612151608.99661-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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 Mon Jun 12 15:16: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: 13276838 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 33115C88CB8 for ; Mon, 12 Jun 2023 15:16:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235724AbjFLPQb (ORCPT ); Mon, 12 Jun 2023 11:16:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239550AbjFLPQ1 (ORCPT ); Mon, 12 Jun 2023 11:16:27 -0400 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8432D10EA; Mon, 12 Jun 2023 08:16:22 -0700 (PDT) Received: by mail-qv1-xf29.google.com with SMTP id 6a1803df08f44-62de8bce252so12949316d6.0; Mon, 12 Jun 2023 08:16:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582981; x=1689174981; 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=k0nJQfHkgsS0Ado2J2Ry48mIlQU3QhX2gvGIfIXrVgk=; b=QBLG5tGfrScHuOA6XFKGgn3DhpcJVq/bYjJOoCU0Y0ug5hsh3id6idicIEnJD3J9xV T9Qu1cpLux2areAfn7tDjMicx5YR99OFmzz6dF+1nS6b56a0xr9K9D5/WVJ2oGz/OUdK rYGEnBGIocxu9TLKZoI8LorQgzB3R/lk+nO/Lt1NOAltyG/4g0S0i7kgoopDUkh88gLp Gaif/6VUegZy/zYiI4NUneR/I88K4xXy387h2gc0WDgpfj8bTYkaPdLmaPuvTuCiP3tj qn+oGfk9W8ike2kQbmFAZvuHheyWB10xnlEuCFYvBsXSAiSy864EwI/zs59loAXeScSL LzAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582981; x=1689174981; 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=k0nJQfHkgsS0Ado2J2Ry48mIlQU3QhX2gvGIfIXrVgk=; b=KKheDTRqwA/D8ewUlnm7N5zxHYh5dvNOmKrdbpJrpBeV+2npn87MnzXeb4sO2ZUXOC 9SHVQrz11u3JJc7V2aN5iPj7WsSXr1H5RbUXmbcjm41GmCgNkdPT/5dlw+SMZd/hGsFe /J5ul8ihh6TPcmr4lwyALpMK231dCEsbWBfoQ6uh7EBj+OQ+p9Co+/kRVy2CTELx+egk g8mKJILSzb1T3oB7MTRJpJPPxzy5zrzaf8oN10OPE5suxqsGWMg8L2SIpcVxXK7Rrdj5 MFyDm340jSk+8f2YbPoTesaOQHZ62dLz/myq67Vx9ri3COmIx2NXYwmRHme6XaHbpwnk DvHQ== X-Gm-Message-State: AC+VfDw5XktE5mdb42WeL7ihbawM2aEnRR1U86p0YiOy+uZLW7o8XpMg lPcm63PPpcRTH1D4valuhoU= X-Google-Smtp-Source: ACHHUZ5znqAnJ/eBGJjtPWzZYByiXXFtjmqWYhXvHgXNNA0ALpycV1vyfFWIcGdYhtGIV4s1dHNIsg== X-Received: by 2002:a05:6214:e48:b0:629:78ae:80e3 with SMTP id o8-20020a0562140e4800b0062978ae80e3mr11730568qvc.24.1686582981679; Mon, 12 Jun 2023 08:16:21 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08:16:20 -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 v3 bpf-next 05/10] bpf: Clear the probe_addr for uprobe Date: Mon, 12 Jun 2023 15:16:03 +0000 Message-Id: <20230612151608.99661-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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 Cc: Yonghong Song 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 742047c..97a5235 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 Mon Jun 12 15:16: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: 13276839 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 88EBEC88CB4 for ; Mon, 12 Jun 2023 15:16:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239487AbjFLPQc (ORCPT ); Mon, 12 Jun 2023 11:16:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239557AbjFLPQ1 (ORCPT ); Mon, 12 Jun 2023 11:16:27 -0400 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60B2810F5; Mon, 12 Jun 2023 08:16:23 -0700 (PDT) Received: by mail-qv1-xf31.google.com with SMTP id 6a1803df08f44-6261a25e9b6so31982256d6.0; Mon, 12 Jun 2023 08:16:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582982; x=1689174982; 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=mNdCtj9HTCNGVDB4wtHyVm0PHnFgjWUZ0vWch1QTpbs0DvcxGmgOe66cb1MmjOKP7i PsadeQXCo7hGRbliJjrAxYW7/GnItLVLvHmyb5DmaNRf0eEPLY+sRIUyisbTxGW3zzHR cZWjPS6OJgEIIkrMmVEpG7zpGUu0iWgsxkzfb9z31/6yU/+cigFOHGAh9GwoCbwpFEw9 aehLs7jHNvqrPCR91L3fCx1K5qgfe2OjX0648F2Uh/K2qK7pTpgO96eCEc85BGrNCwFV 3Fd288aAzzNOqm3SHWJLgNaUeH3Hqa8JZsexxWM4cCiMMwAI6HnQXCWAo505kIzacJQ+ rHtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582982; x=1689174982; 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=IcuOKaIDdi+kII59KH2Wu2pXCKOsg5JYLrvSzyU/trCqR4zvQm6ZIz0sjYxm+ODWTI CWIl83Z7NetB5GXzk8DP20d1L1BiG1q5swqASCyOHKmRR0Qq1Nb2mZiIW9ksjQ0DS6kj oSZUw3pviUzHwADhkGQlgMTtQrzQqQW5vfTVBGE4qeQ29jjbwLveV65KmxuG2+ZYeaSu R5fTpWe4K8/MaagMphXgv39FeJysZS1TPRc57q35wVtpSlnaI730yaUlqgX2hUHmuiA6 l5cDpG00CFVeupk4EHA10Sy6TJarRUg6fyUrckFKR2jkLdH9G1H1anXVKaxgeNfJOWMu oThg== X-Gm-Message-State: AC+VfDy01P0LPeqtOTVWCN7at3wmA5z+iICQN8TjcwiHuL0sx0bh37Nv H3QxxPUoPF0u3ETMUI+Yqlk= X-Google-Smtp-Source: ACHHUZ7Ogx6+gYEyYuqUe7NK81bKuhCS0jLXpZ66kiJDgoEx8VCB2hFP64/nANJywQa3ydHBBTgcQg== X-Received: by 2002:ad4:5dcf:0:b0:623:6b1a:c5f1 with SMTP id m15-20020ad45dcf000000b006236b1ac5f1mr10751548qvh.4.1686582982512; Mon, 12 Jun 2023 08:16:22 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08:16:22 -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 v3 bpf-next 06/10] bpf: Expose symbol's respective address Date: Mon, 12 Jun 2023 15:16:04 +0000 Message-Id: <20230612151608.99661-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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 Mon Jun 12 15:16: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: 13276840 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 1FB37C7EE2F for ; Mon, 12 Jun 2023 15:16:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239491AbjFLPQd (ORCPT ); Mon, 12 Jun 2023 11:16:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239489AbjFLPQ2 (ORCPT ); Mon, 12 Jun 2023 11:16:28 -0400 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D0D010FF; Mon, 12 Jun 2023 08:16:24 -0700 (PDT) Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-39a505b901dso2226159b6e.0; Mon, 12 Jun 2023 08:16:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582983; x=1689174983; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YIqt3XjrxTuvqftnfbbwKPC43OuqsK9HXwQWl45kfGw=; b=RRBrge+Nhcy7jAZZfCWSZCEKkBAUJAAMz2aCFmpbbsCWrYrEbKCF0cb2HjZ+m+R1FL SNfZkE7rcXQLXtwy8RspjrZLAa+ZlE5KXFNUkNfSzvmKXNERbczVnnXrqXVrzO0rN/d4 1/WD4uSk1CIT48nw5OjiIpDcN01MGitKP6F+c1fF6RsKTNffXcmQgd+VHXMSaOWc04zQ EAjjACm1E6GHPuEZP2/y4jdwReH/XMBi2UjQXlFNSJrMcIC0mTNn2WrBGe0nZS1HvRkw zgfLzkoHvkeb2qlqGYZ+pO4HmmIlA9U0Acr2wLfSXkigB3OHs7nMdlFnnHR+YIbSekqa F2OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582983; x=1689174983; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YIqt3XjrxTuvqftnfbbwKPC43OuqsK9HXwQWl45kfGw=; b=L4JNmMCV+jjRpaA6Nynwr+3Pa4U+ufa++rFwazZptS8yGSRoyVMpesmaAGtDopztFq +1BPZGX9+porI+cwxR3Vk+WBdLUAQd24jtkrMVeXERL738FPXUZKGrt/mgX8P6iBZZqy JLmZOXS45trR/TyaRB2g/jNpsG2+j3G0jjpXOg+bSmfgSrHPZvqbiWzB5YicxhFwBHnQ 7SB0wG/ImlC49a1xYg2T3XEAlsWasYggZNeGr5e307AEh8AK+plqEMuXsZvPIaDJtEmE Ee9Gm7wT6VWHyv4NQL79mXUjBbWfvmSm45vUj0o+E9p+npSO9ccQb4ePvFyNGPhnb3sN GOqw== X-Gm-Message-State: AC+VfDxrDnqDk67ITPst9Nu26sZLlWWfNGv7qj164QVjS4YYYdu5jIVJ au+sK2LBqLYA1JnqgS6g2+g= X-Google-Smtp-Source: ACHHUZ5sUABGNfZQb99E8VCmdyqm2qoyHxDRiC6VqLiE0eqObSsmd2/3igWTRdwf7A2ABCt7SKhP8A== X-Received: by 2002:a05:6808:1892:b0:39c:785a:9755 with SMTP id bi18-20020a056808189200b0039c785a9755mr5679900oib.9.1686582983516; Mon, 12 Jun 2023 08:16:23 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08:16:22 -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 v3 bpf-next 07/10] bpf: Add a common helper bpf_copy_to_user() Date: Mon, 12 Jun 2023 15:16:05 +0000 Message-Id: <20230612151608.99661-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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 92a57ef..80c9ec0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3234,6 +3234,25 @@ static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, raw_tp_link->btp->tp->name); } +static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen, + u32 len) +{ + if (ulen >= len + 1) { + if (copy_to_user(ubuf, buf, len + 1)) + return -EFAULT; + } else { + char zero = '\0'; + + if (copy_to_user(ubuf, buf, ulen - 1)) + return -EFAULT; + if (put_user(zero, ubuf + ulen - 1)) + return -EFAULT; + return -ENOSPC; + } + + return 0; +} + static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, struct bpf_link_info *info) { @@ -3252,20 +3271,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, if (!ubuf) return 0; - if (ulen >= tp_len + 1) { - if (copy_to_user(ubuf, tp_name, tp_len + 1)) - return -EFAULT; - } else { - char zero = '\0'; - - if (copy_to_user(ubuf, tp_name, ulen - 1)) - return -EFAULT; - if (put_user(zero, ubuf + ulen - 1)) - return -EFAULT; - return -ENOSPC; - } - - return 0; + return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len); } static const struct bpf_link_ops bpf_raw_tp_link_lops = { From patchwork Mon Jun 12 15:16: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: 13276841 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 EADB1C88CB4 for ; Mon, 12 Jun 2023 15:16:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239564AbjFLPQf (ORCPT ); Mon, 12 Jun 2023 11:16:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239512AbjFLPQa (ORCPT ); Mon, 12 Jun 2023 11:16:30 -0400 Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com [IPv6:2607:f8b0:4864:20::f2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BA40170A; Mon, 12 Jun 2023 08:16:25 -0700 (PDT) Received: by mail-qv1-xf2c.google.com with SMTP id 6a1803df08f44-62de1a3e354so11409986d6.3; Mon, 12 Jun 2023 08:16:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582984; x=1689174984; 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=GvHWDyuzXCb0xSQICwGSRrHmdLNiWivuKicwgtJGuF0=; b=acZmy/AZzVjotiDaxrFHHOi65rdBA+c7GziUNy6NKa4uMD/HHbv0tiX0536cjLym6E 7BTPUDAiPHyfz/cEYYFWoB9TQ9f4YjgEEMfN8UuFwJ2le4pwdy29Z5T8ljTiq9yy0Y2J h+6m4MbJREap02cESVzHPZVGK/T8itdYlt69zTxXBz2QnCMZTWoQxylHYCpLmHFTQgIJ uG28L7Cn6dgXrG2VXdbT1sr4j8Q0Z00wJl5QhZiWup6Elx1oesG0zWP3qAQSY1N5WRcA vsfrgNLTVNwLsbTPdOeBbIoRhe6Q1T+edcFoyUv3MKiqfvdEeP/3aC1xxV4Ar0wp0xWz mx8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582984; x=1689174984; 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=GvHWDyuzXCb0xSQICwGSRrHmdLNiWivuKicwgtJGuF0=; b=WqrL9XawBQrIKG6D7Vj590wiMIWVDmNlUgHMSUheOF3f3cXE9GL2UY0ksCJAmDCQyO R8ygElFaQuwIMFV3Sxt4Fjz2BNh4Yj77JkJyE/QHacJbntTSkxBPPwB8EH2cNIKN6VI+ HatjNIjQ/ooVZDQY63Q/265RaAQVq5wEqSjmehSjU9sEhxL7eokTYB2GOo1fP0H5+5Xo mmfQZ3YSdEeqCI0/wQIxEV96I44JQCc8ajN82yvXpzO6+q96tVGoAX8HWovu0tlc78z/ 7SHPTtpVVyyuBRUe4hn+5Lz2gR2Mg4NUAnIIGtb7uoKaQ+LlCUU+xAxNA8LVHksxlqqX wSQQ== X-Gm-Message-State: AC+VfDwfb1CXbZhHgRKmv7kDNyO3RlqLg2spexS9Ef2AWa0m5lMUt3Eu HzuDmMWJiDHObQ9qwDlCgcY= X-Google-Smtp-Source: ACHHUZ7ngTKYVJl8Ne8oAgxRsj3uYgqLzEw4vaff4vDYxVXleMJOfFNXgxuZfE2vjGsMqZSC9ROzuA== X-Received: by 2002:a05:6214:27e1:b0:62d:d809:1f7 with SMTP id jt1-20020a05621427e100b0062dd80901f7mr6812274qvb.21.1686582984597; Mon, 12 Jun 2023 08:16:24 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08: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 v3 bpf-next 08/10] bpf: Support ->fill_link_info for perf_event Date: Mon, 12 Jun 2023 15:16:06 +0000 Message-Id: <20230612151608.99661-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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_link_perf_event_type is introduced to help the user understand which struct is relevant. Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 32 +++++++++++ kernel/bpf/syscall.c | 124 +++++++++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 32 +++++++++++ 3 files changed, 188 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 23691ea..8d4556e 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_link_type { + BPF_PERF_LINK_UNSPEC = 0, + BPF_PERF_LINK_UPROBE = 1, + BPF_PERF_LINK_KPROBE = 2, + BPF_PERF_LINK_TRACEPOINT = 3, + BPF_PERF_LINK_PERF_EVENT = 4, + + MAX_BPF_LINK_PERF_EVENT_TYPE, +}; + /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command * * NONE(default): No further bpf programs allowed in the subtree. @@ -6443,7 +6453,29 @@ struct bpf_link_info { __u32 count; __u32 flags; } kprobe_multi; + struct { + __u64 config; + __u32 type; + } perf_event; /* BPF_LINK_PERF_EVENT_PERF_EVENT */ + struct { + __aligned_u64 file_name; /* in/out: buff ptr */ + __u32 name_len; + __u32 offset; /* offset from name */ + __u32 flags; + } uprobe; /* BPF_LINK_PERF_EVENT_UPROBE */ + struct { + __aligned_u64 func_name; /* in/out: buff ptr */ + __u32 name_len; + __u32 offset; /* offset from name */ + __u64 addr; + __u32 flags; + } kprobe; /* BPF_LINK_PERF_EVENT_KPROBE */ + struct { + __aligned_u64 tp_name; /* in/out: buff ptr */ + __u32 name_len; + } tracepoint; /* BPF_LINK_PERF_EVENT_TRACEPOINT */ }; + __u32 perf_link_type; /* enum bpf_perf_link_type */ } __attribute__((aligned(8))); /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 80c9ec0..fe354d5 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3303,9 +3303,133 @@ static void bpf_perf_link_dealloc(struct bpf_link *link) kfree(perf_link); } +static int bpf_perf_link_fill_name(const struct perf_event *event, + char __user *uname, u32 ulen, + u64 *probe_offset, u64 *probe_addr, + u32 *fd_type) +{ + const char *buf; + u32 prog_id; + size_t len; + int err; + + if (!ulen ^ !uname) + return -EINVAL; + if (!uname) + return 0; + + err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf, + probe_offset, probe_addr); + if (err) + return err; + + len = strlen(buf); + if (buf) { + err = bpf_copy_to_user(uname, buf, ulen, len); + if (err) + return err; + } else { + char zero = '\0'; + + if (put_user(zero, uname)) + return -EFAULT; + } + return 0; +} + +static int bpf_perf_link_fill_probe(const struct perf_event *event, + struct bpf_link_info *info) +{ + char __user *uname; + u64 addr, offset; + u32 ulen, type; + int err; + +#ifdef CONFIG_KPROBE_EVENTS + if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE) { + uname = u64_to_user_ptr(info->kprobe.func_name); + ulen = info->kprobe.name_len; + info->perf_link_type = BPF_PERF_LINK_KPROBE; + err = bpf_perf_link_fill_name(event, uname, ulen, &offset, + &addr, &type); + if (err) + return err; + + info->kprobe.offset = offset; + if (type == BPF_FD_TYPE_KRETPROBE) + info->kprobe.flags = 1; + if (!kallsyms_show_value(current_cred())) + return 0; + info->kprobe.addr = addr; + return 0; + } +#endif + +#ifdef CONFIG_UPROBE_EVENTS + if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE) { + uname = u64_to_user_ptr(info->uprobe.file_name); + ulen = info->uprobe.name_len; + info->perf_link_type = BPF_PERF_LINK_UPROBE; + err = bpf_perf_link_fill_name(event, uname, ulen, &offset, + &addr, &type); + if (err) + return err; + + info->uprobe.offset = offset; + if (type == BPF_FD_TYPE_URETPROBE) + info->uprobe.flags = 1; + return 0; + } +#endif + + return -EOPNOTSUPP; +} + +static int bpf_perf_link_fill_tracepoint(const struct perf_event *event, + struct bpf_link_info *info) +{ + char __user *uname = u64_to_user_ptr(info->tracepoint.tp_name); + u32 ulen = info->tracepoint.name_len; + u64 addr, off; + u32 type; + + info->perf_link_type = BPF_PERF_LINK_TRACEPOINT; + return bpf_perf_link_fill_name(event, uname, ulen, &off, &addr, &type); +} + +static int bpf_perf_link_fill_perf_event(const struct perf_event *event, + struct bpf_link_info *info) +{ + info->perf_event.type = event->attr.type; + info->perf_event.config = event->attr.config; + info->perf_link_type = BPF_PERF_LINK_PERF_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; + if (event->prog->type == BPF_PROG_TYPE_PERF_EVENT) + return bpf_perf_link_fill_perf_event(event, info); + if (event->prog->type == BPF_PROG_TYPE_TRACEPOINT) + return bpf_perf_link_fill_tracepoint(event, info); + return bpf_perf_link_fill_probe(event, info); +} + static const struct bpf_link_ops bpf_perf_link_lops = { .release = bpf_perf_link_release, .dealloc = bpf_perf_link_dealloc, + .fill_link_info = bpf_perf_link_fill_link_info, }; static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 23691ea..8d4556e 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_link_type { + BPF_PERF_LINK_UNSPEC = 0, + BPF_PERF_LINK_UPROBE = 1, + BPF_PERF_LINK_KPROBE = 2, + BPF_PERF_LINK_TRACEPOINT = 3, + BPF_PERF_LINK_PERF_EVENT = 4, + + MAX_BPF_LINK_PERF_EVENT_TYPE, +}; + /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command * * NONE(default): No further bpf programs allowed in the subtree. @@ -6443,7 +6453,29 @@ struct bpf_link_info { __u32 count; __u32 flags; } kprobe_multi; + struct { + __u64 config; + __u32 type; + } perf_event; /* BPF_LINK_PERF_EVENT_PERF_EVENT */ + struct { + __aligned_u64 file_name; /* in/out: buff ptr */ + __u32 name_len; + __u32 offset; /* offset from name */ + __u32 flags; + } uprobe; /* BPF_LINK_PERF_EVENT_UPROBE */ + struct { + __aligned_u64 func_name; /* in/out: buff ptr */ + __u32 name_len; + __u32 offset; /* offset from name */ + __u64 addr; + __u32 flags; + } kprobe; /* BPF_LINK_PERF_EVENT_KPROBE */ + struct { + __aligned_u64 tp_name; /* in/out: buff ptr */ + __u32 name_len; + } tracepoint; /* BPF_LINK_PERF_EVENT_TRACEPOINT */ }; + __u32 perf_link_type; /* enum bpf_perf_link_type */ } __attribute__((aligned(8))); /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed From patchwork Mon Jun 12 15:16: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: 13276842 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 C7C15C7EE2F for ; Mon, 12 Jun 2023 15:16:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239655AbjFLPQw (ORCPT ); Mon, 12 Jun 2023 11:16:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239561AbjFLPQd (ORCPT ); Mon, 12 Jun 2023 11:16:33 -0400 Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC8F8E5F; Mon, 12 Jun 2023 08:16:26 -0700 (PDT) Received: by mail-qv1-xf32.google.com with SMTP id 6a1803df08f44-62b671e0a0dso30655296d6.1; Mon, 12 Jun 2023 08:16:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582986; x=1689174986; 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=J4o/ujNBffQluZLgK8PAKCwIeuiFgex/iYZvXGfAS/o=; b=DESA7ZGwbmHUDJsTbustftcRp7e7gqkSP3OcqVCNE4W33CcDI0pjTO5jl+SZRB3miE 3cnquGYXHZ/o3tI3d1SFw5AAQQZZzuNnHa1cStvmcE/+6KeziEwybJsvkoZ8GXx2xttS nPQN+Z/L3fUtDgjvm8/nt8b3uObDHPHJhu+E4NrJMol/i55d/WmMg7OYsQouorp54AbC rSng+sSHqE+WTqQEf1bFKODIajOIR9gMldigfFPdLyrXOgjbLByt5lnw3owRQK5G5LmG lz2Q9X1hebnxTjQUJSno8qr8eZmDRY2d3dn2g0e4b/SAHIWdpLMBQdGiJaUrlzZ4+IlL xJ9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582986; x=1689174986; 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=J4o/ujNBffQluZLgK8PAKCwIeuiFgex/iYZvXGfAS/o=; b=T++g1i23Jxaqh7Ar6ypcicGJ+dAO0tKa7LMsiuwom+5Lgfa0lNZ1SyCc1dHFg7IrEo 7hI8hQv0M8xvLUyW/Ma98xqzAlcPwI7V9uHfPUN/noAUQ66EO71Tvg3mJN5FPdv047VW mD64JU4S12T4qFEIeOkPRZnAw+0o/MBYDeWCXnEiin1sRmSW7isQQF/UBASLZ4OcsPZr VItox1Xml5OBbnUAPLgNhsFVSTrunUw8JwtiVkf1pFYsy4UMDvhaQ3G0fLLCUtX1Nygo hkW/77oQP6/S+Civ5j1b9B/wtJCZdsV9m7HHycwuZEjFrBpkFA5fLf3uCd4fYVgjRuJ7 Ie6g== X-Gm-Message-State: AC+VfDxvMlmnry0qU802GFTvjBFCWnxTJtXE26DHKeIxkHZw/8x75Umu qxEdelVGpU1ZB9/2RojO58g= X-Google-Smtp-Source: ACHHUZ6/Ca7uEzoPbZX+VimYeKRMREZrQry3oY5SmSq4H7rAbfirMxXSYwvPCPkTq++xff53cYykcA== X-Received: by 2002:a05:6214:d4b:b0:623:c96a:e735 with SMTP id 11-20020a0562140d4b00b00623c96ae735mr13088356qvr.1.1686582985789; Mon, 12 Jun 2023 08:16:25 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08:16: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 , Jiri Olsa Subject: [PATCH v3 bpf-next 09/10] bpftool: Add perf event names Date: Mon, 12 Jun 2023 15:16:07 +0000 Message-Id: <20230612151608.99661-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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/perf.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++ tools/bpf/bpftool/perf.h | 11 +++++ 2 files changed, 118 insertions(+) create mode 100644 tools/bpf/bpftool/perf.h diff --git a/tools/bpf/bpftool/perf.c b/tools/bpf/bpftool/perf.c index 9174344..fbdf88c 100644 --- a/tools/bpf/bpftool/perf.c +++ b/tools/bpf/bpftool/perf.c @@ -18,6 +18,113 @@ #include #include "main.h" +#include "perf.h" + +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", +}; + +const char *perf_type_str(enum perf_type_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_type_name)) + return NULL; + + return perf_type_name[t]; +} + +const char *perf_hw_str(enum perf_hw_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(event_symbols_hw)) + return NULL; + + return event_symbols_hw[t]; +} + +const char *perf_hw_cache_str(enum perf_hw_cache_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(evsel__hw_cache)) + return NULL; + + return evsel__hw_cache[t]; +} + +const char *perf_hw_cache_op_str(enum perf_hw_cache_op_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(evsel__hw_cache_op)) + return NULL; + + return evsel__hw_cache_op[t]; +} + +const char *perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(evsel__hw_cache_result)) + return NULL; + + return evsel__hw_cache_result[t]; +} + +const char *perf_sw_str(enum perf_sw_ids t) +{ + if (t < 0 || t >= ARRAY_SIZE(event_symbols_sw)) + return NULL; + + return event_symbols_sw[t]; +} /* 0: undecided, 1: supported, 2: not supported */ static int perf_query_supported; diff --git a/tools/bpf/bpftool/perf.h b/tools/bpf/bpftool/perf.h new file mode 100644 index 0000000..3fd7e42 --- /dev/null +++ b/tools/bpf/bpftool/perf.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ +/* Copyright (C) 2023 Yafang Shao */ + +#include + +const char *perf_type_str(enum perf_type_id t); +const char *perf_hw_str(enum perf_hw_id t); +const char *perf_hw_cache_str(enum perf_hw_cache_id t); +const char *perf_hw_cache_op_str(enum perf_hw_cache_op_id t); +const char *perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t); +const char *perf_sw_str(enum perf_sw_ids t); From patchwork Mon Jun 12 15:16: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: 13276843 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 68676C88CB4 for ; Mon, 12 Jun 2023 15:16:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238383AbjFLPQx (ORCPT ); Mon, 12 Jun 2023 11:16:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235001AbjFLPQe (ORCPT ); Mon, 12 Jun 2023 11:16:34 -0400 Received: from mail-vk1-xa31.google.com (mail-vk1-xa31.google.com [IPv6:2607:f8b0:4864:20::a31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5E92E63; Mon, 12 Jun 2023 08:16:27 -0700 (PDT) Received: by mail-vk1-xa31.google.com with SMTP id 71dfb90a1353d-4635c158e2aso1218375e0c.3; Mon, 12 Jun 2023 08:16:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686582987; x=1689174987; 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=ipOtOLH0eCoqdtlt4P3S4Nuxsk5nZTTtOt/GxTeUjs8=; b=CHtHcUWeBWcutxQa80MWMJ2lA2Gxm8IJLN0dsqKweKUfslEJ5q8VY4d3td+/lY+hzh h+8XiIaxX9lVtXee2DFhBUGmGAMnf7wgy2KK3CXHNMj/W3Ev+TMOlvCl03VWhGJwPgSg GnLOwl1Q81kG96tDbaqlL7vkDCA2GjIqIeypKHQe16axaSbQp8iyW7lsTI8Tm61gVydv aZPtT2CtxRkO640cDjWZEi8bHFe8u6ROGIIwTVel6Nu1oPSeYeuEgxVkJf+LZBF0D8Xr /jNQZ8t4T4RTzb4lLpcl7h0F0eZc2BVdLw3cJZjQEykpCeLNyE6Vxy5nuD2wpumCen9G 6D+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686582987; x=1689174987; 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=ipOtOLH0eCoqdtlt4P3S4Nuxsk5nZTTtOt/GxTeUjs8=; b=UsmivWSNywN1qA15POgZlqQ+H+m++jtYMT/9JMSzl3I9XtXzeO05kC7pT3xosiM3/B yAek2JaL3t4QBBTtAAgnQc3DTU8u8sfBGeglXJSp5Q5oflK94II7stBYJ+1E6mOAQTIk XkmZlkADB3LVjQx1+5v4HuFzZR7VLJ3iGoqPSGDKkSWZSvTnXkXYwKDnTM+7L1yx1L79 i6O8V9ttOiIYov9XWar6WzPo+XJfecm7jkRA8ZVulf8qD7Hj60HKkZYhG48xKGtxEUze LfzudMZ/TZNrqptkDGL2yPZw7s9JyVeB/luVI4BQBcmN1ati68Xp92X3ZF7dvc4yPTqb Ee2A== X-Gm-Message-State: AC+VfDxyZcLKawp9cgS0t8pzV9/k64IrrmbARDXshd/Ez7YF5t+Ao+Gr 9Qwuiyv9pmEGYDQYZ7Fmgh8= X-Google-Smtp-Source: ACHHUZ4wcRSYDYhtp4x0qNwiAiDt5y0y7uLIBXM4hjsOVnTBdWj+5IXeliZLFS6VJAIgLLxTk8a7PA== X-Received: by 2002:a67:e418:0:b0:439:5904:6f6d with SMTP id d24-20020a67e418000000b0043959046f6dmr2893417vsf.33.1686582986757; Mon, 12 Jun 2023 08:16:26 -0700 (PDT) Received: from vultr.guest ([108.61.23.146]) by smtp.gmail.com with ESMTPSA id o17-20020a0cf4d1000000b0062de0dde008sm1533953qvm.64.2023.06.12.08.16.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 08: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 v3 bpf-next 10/10] bpftool: Show probed function in perf_event link info Date: Mon, 12 Jun 2023 15:16:08 +0000 Message-Id: <20230612151608.99661-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230612151608.99661-1-laoar.shao@gmail.com> References: <20230612151608.99661-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 3: perf_event prog 14 event_type software event_config cpu-clock bpf_cookie 0 pids perf_event(1379330) 4: perf_event prog 14 event_type hw-cache event_config LLC-load-misses bpf_cookie 0 pids perf_event(1379330) 5: perf_event prog 14 event_type hardware event_config cpu-cycles bpf_cookie 0 pids perf_event(1379330) 6: perf_event prog 20 retprobe 0 file_name /home/yafang/bpf/uprobe/a.out offset 0x1338 bpf_cookie 0 pids uprobe(1379706) 7: perf_event prog 21 retprobe 1 file_name /home/yafang/bpf/uprobe/a.out offset 0x1338 bpf_cookie 0 pids uprobe(1379706) 8: perf_event prog 27 tp_name sched_switch bpf_cookie 0 pids tracepoint(1381734) 10: perf_event prog 43 retprobe 0 func_name kernel_clone addr ffffffffad0a9660 bpf_cookie 0 pids kprobe(1384186) 11: perf_event prog 41 retprobe 1 func_name kernel_clone addr ffffffffad0a9660 bpf_cookie 0 pids kprobe(1384186) $ tools/bpf/bpftool/bpftool link show -j [{"id":3,"type":"perf_event","prog_id":14,"event_type":"software","event_config":"cpu-clock","bpf_cookie":0,"pids":[{"pid":1379330,"comm":"perf_event"}]},{"id":4,"type":"perf_event","prog_id":14,"event_type":"hw-cache","event_config":"LLC-load-misses","bpf_cookie":0,"pids":[{"pid":1379330,"comm":"perf_event"}]},{"id":5,"type":"perf_event","prog_id":14,"event_type":"hardware","event_config":"cpu-cycles","bpf_cookie":0,"pids":[{"pid":1379330,"comm":"perf_event"}]},{"id":6,"type":"perf_event","prog_id":20,"retprobe":0,"file_name":"/home/yafang/bpf/uprobe/a.out","offset":4920,"bpf_cookie":0,"pids":[{"pid":1379706,"comm":"uprobe"}]},{"id":7,"type":"perf_event","prog_id":21,"retprobe":1,"file_name":"/home/yafang/bpf/uprobe/a.out","offset":4920,"bpf_cookie":0,"pids":[{"pid":1379706,"comm":"uprobe"}]},{"id":8,"type":"perf_event","prog_id":27,"tp_name":"sched_switch","bpf_cookie":0,"pids":[{"pid":1381734,"comm":"tracepoint"}]},{"id":10,"type":"perf_event","prog_id":43,"retprobe":0,"func_name":"kernel_clone","offset":0,"addr":18446744072317736544,"bpf_cookie":0,"pids":[{"pid":1384186,"comm":"kprobe"}]},{"id":11,"type":"perf_event","prog_id":41,"retprobe":1,"func_name":"kernel_clone","offset":0,"addr":18446744072317736544,"bpf_cookie":0,"pids":[{"pid":1384186,"comm":"kprobe"}]}] 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 3: perf_event prog 14 event_type software event_config cpu-clock 4: perf_event prog 14 event_type hw-cache event_config LLC-load-misses 5: perf_event prog 14 event_type hardware event_config cpu-cycles 6: perf_event prog 20 retprobe 0 file_name /home/yafang/bpf/uprobe/a.out offset 0x1338 7: perf_event prog 21 retprobe 1 file_name /home/yafang/bpf/uprobe/a.out offset 0x1338 8: perf_event prog 27 tp_name sched_switch 10: perf_event prog 43 retprobe 0 func_name kernel_clone 11: perf_event prog 41 retprobe 1 func_name kernel_clone Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 0015582..c16f71d 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -15,6 +15,7 @@ #include "json_writer.h" #include "main.h" #include "xlated_dumper.h" +#include "perf.h" static struct hashmap *link_table; static struct dump_data dd = {}; @@ -207,6 +208,109 @@ 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_uint_field(wtr, "retprobe", info->kprobe.flags & 0x1); + jsonw_string_field(wtr, "func_name", + u64_to_ptr(info->kprobe.func_name)); + jsonw_uint_field(wtr, "offset", info->kprobe.offset); + jsonw_uint_field(wtr, "addr", info->kprobe.addr); +} + +static void +show_perf_event_uprobe_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + jsonw_uint_field(wtr, "retprobe", info->uprobe.flags & 0x1); + jsonw_string_field(wtr, "file_name", + u64_to_ptr(info->uprobe.file_name)); + jsonw_uint_field(wtr, "offset", info->uprobe.offset); +} + +static void +show_perf_event_tp_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + jsonw_string_field(wtr, "tp_name", + u64_to_ptr(info->tracepoint.tp_name)); +} + +static const char *perf_config_hw_cache_str(__u64 config) +{ +#define PERF_HW_CACHE_LEN 128 + const char *hw_cache, *result, *op; + char *str = malloc(PERF_HW_CACHE_LEN); + + if (!str) { + p_err("mem alloc failed"); + return NULL; + } + hw_cache = perf_hw_cache_str(config & 0xff); + if (hw_cache) + snprintf(str, PERF_HW_CACHE_LEN, "%s-", hw_cache); + else + snprintf(str, PERF_HW_CACHE_LEN, "%lld-", config & 0xff); + op = perf_hw_cache_op_str((config >> 8) & 0xff); + if (op) + snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str), + "%s-", op); + else + snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str), + "%lld-", (config >> 8) & 0xff); + result = perf_hw_cache_op_result_str(config >> 16); + if (result) + snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str), + "%s", result); + else + snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str), + "%lld", config >> 16); + + return str; +} + +static const char *perf_config_str(__u32 type, __u64 config) +{ + const char *perf_config; + + switch (type) { + case PERF_TYPE_HARDWARE: + perf_config = perf_hw_str(config); + break; + case PERF_TYPE_SOFTWARE: + perf_config = perf_sw_str(config); + break; + case PERF_TYPE_HW_CACHE: + perf_config = perf_config_hw_cache_str(config); + break; + default: + perf_config = NULL; + break; + } + return perf_config; +} + +static void +show_perf_event_event_json(struct bpf_link_info *info, json_writer_t *wtr) +{ + __u64 config = info->perf_event.config; + __u32 type = info->perf_event.type; + const char *perf_type, *perf_config; + + perf_type = perf_type_str(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; @@ -262,6 +366,16 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) case BPF_LINK_TYPE_KPROBE_MULTI: show_kprobe_multi_json(info, json_wtr); break; + case BPF_LINK_TYPE_PERF_EVENT: + if (info->perf_link_type == BPF_PERF_LINK_PERF_EVENT) + show_perf_event_event_json(info, json_wtr); + else if (info->perf_link_type == BPF_PERF_LINK_TRACEPOINT) + show_perf_event_tp_json(info, json_wtr); + else if (info->perf_link_type == BPF_PERF_LINK_KPROBE) + show_perf_event_kprobe_json(info, json_wtr); + else if (info->perf_link_type == BPF_PERF_LINK_UPROBE) + show_perf_event_uprobe_json(info, json_wtr); + break; default: break; } @@ -433,6 +547,71 @@ 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; + __u32 retprobe; + + buf = (const char *)u64_to_ptr(info->kprobe.func_name); + if (buf[0] == '\0' && !info->kprobe.addr) + return; + + retprobe = info->kprobe.flags & 0x1; + printf("\n\tretprobe %u func_name %s ", retprobe, buf); + if (info->kprobe.offset) + printf("offset %#x ", info->kprobe.offset); + if (info->kprobe.addr) + printf("addr %llx ", info->kprobe.addr); +} + +static void show_perf_event_uprobe_plain(struct bpf_link_info *info) +{ + const char *buf; + __u32 retprobe; + + buf = (const char *)u64_to_ptr(info->uprobe.file_name); + if (buf[0] == '\0') + return; + + retprobe = info->uprobe.flags & 0x1; + printf("\n\tretprobe %u file_name %s ", retprobe, buf); + if (info->uprobe.offset) + printf("offset %#x ", info->kprobe.offset); +} + +static void show_perf_event_tp_plain(struct bpf_link_info *info) +{ + const char *buf; + + buf = (const char *)u64_to_ptr(info->tracepoint.tp_name); + if (buf[0] == '\0') + return; + + printf("\n\ttp_name %s ", buf); +} + +static void show_perf_event_event_plain(struct bpf_link_info *info) +{ + __u64 config = info->perf_event.config; + __u32 type = info->perf_event.type; + const char *perf_type, *perf_config; + + perf_type = perf_type_str(type); + if (perf_type) + printf("\n\tevent_type %s ", perf_type); + else + printf("\n\tevent_type %u ", type); + + perf_config = perf_config_str(type, config); + if (perf_config) + printf("event_config %s ", perf_config); + else + printf("event_config %llu ", config); + + if (type == PERF_TYPE_HW_CACHE && perf_config) + free((void *)perf_config); +} + static int show_link_close_plain(int fd, struct bpf_link_info *info) { struct bpf_prog_info prog_info; @@ -481,6 +660,16 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) case BPF_LINK_TYPE_KPROBE_MULTI: show_kprobe_multi_plain(info); break; + case BPF_LINK_TYPE_PERF_EVENT: + if (info->perf_link_type == BPF_PERF_LINK_PERF_EVENT) + show_perf_event_event_plain(info); + else if (info->perf_link_type == BPF_PERF_LINK_TRACEPOINT) + show_perf_event_tp_plain(info); + else if (info->perf_link_type == BPF_PERF_LINK_KPROBE) + show_perf_event_kprobe_plain(info); + else if (info->perf_link_type == BPF_PERF_LINK_UPROBE) + show_perf_event_uprobe_plain(info); + break; default: break; } @@ -508,6 +697,7 @@ static int do_show_link(int fd) int err; memset(&info, 0, sizeof(info)); + buf[0] = '\0'; again: err = bpf_link_get_info_by_fd(fd, &info, &len); if (err) { @@ -542,7 +732,30 @@ static int do_show_link(int fd) goto again; } } + if (info.type == BPF_LINK_TYPE_PERF_EVENT) { + if (info.perf_link_type == BPF_PERF_LINK_PERF_EVENT) + goto out; + if (info.perf_link_type == BPF_PERF_LINK_TRACEPOINT && + !info.tracepoint.tp_name) { + info.tracepoint.tp_name = (unsigned long)&buf; + info.tracepoint.name_len = sizeof(buf); + goto again; + } + if (info.perf_link_type == BPF_PERF_LINK_KPROBE && + !info.kprobe.func_name) { + info.kprobe.func_name = (unsigned long)&buf; + info.kprobe.name_len = sizeof(buf); + goto again; + } + if (info.perf_link_type == BPF_PERF_LINK_UPROBE && + !info.uprobe.file_name) { + info.uprobe.file_name = (unsigned long)&buf; + info.uprobe.name_len = sizeof(buf); + goto again; + } + } +out: if (json_output) show_link_close_json(fd, &info); else