From patchwork Sun May 28 14:20:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13257850 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 EDFD63D75 for ; Sun, 28 May 2023 14:20:37 +0000 (UTC) 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 8BA88BD for ; Sun, 28 May 2023 07:20:36 -0700 (PDT) Received: by mail-qv1-xf31.google.com with SMTP id 6a1803df08f44-626117a8610so4857486d6.1 for ; Sun, 28 May 2023 07:20:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685283635; x=1687875635; 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=KBoEWBqQyZgr3XlDnho9z66TJOllaOiv5eDkisHvHIo=; b=ki7M5e+7P3IHf3LUN9HC9GugaP4aUF0NIils16+Y24HFWg2PeeXUZ3DSJMJPirh55B dkg61W4zy3YbNDwXuVPiSQX5F323bLjBWoZ/1bgzMFF4qS+K0TJY+ETr+NwQXNaYQd9U XOWyOFMSww6nNzU59pAjjae4fK6OzeZ/HyTe0tffMcNdzQRreJ3FL/SCsw/k/h3ytWGH 5ZaKDXHCjqJMepelXsxDBINwDgj64YnbH/UnHvr4oj2xgaBFguOgc8Qijg4K2jBSRxwG jtwWYTU5kz0gjextdJklm69SwbMBj+RHHKQESsr5zUI3dHaWK5E67ywObzqSwPkI7Abj eM9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685283635; x=1687875635; 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=KBoEWBqQyZgr3XlDnho9z66TJOllaOiv5eDkisHvHIo=; b=dRoBn8SRO3BEyvOukMQ19kyS9dJh2x110LieACB3tF4nOv0s2Clc/jYhBBO4gb5p/J QVEllndI5rfrEiBoZjoA9CHAN2FTJ+dRoqFzFJjdzqh/T+aZ2sLaMPov+NR09e4/hUWU QnSp9eV31ecq7GWAdZRPQPDQdujXnhU8Nj/OHXkNL+jDe61d9LjYyp+5parG0FbTClu+ NfKrn+23iyKHoZQaGFIpCNQPKlQZLw4mk3/OBmKcp10WKrkiFcnQ2vYwZi0HMucbY1M1 wSjCX/jiR3wLrGL73j8o/dKJ3mcEvZyRMfYj8JZzfP0rGJUbCxlaJ8on7/58aU6WAFFz SV1A== X-Gm-Message-State: AC+VfDy3JSzrKcEPVcJjWw7nlNNVnJJZASFBHTKlAg3PYzDggtMs/5as KZWrNMdpBr+Uud4I5SJzPq8= X-Google-Smtp-Source: ACHHUZ4p4g1uTBECLOLWDkQeXDg5a4VTrhMWgn2TScw09pUFV4HKnIOQHrW1XTa3PqSs3RmnOshMqw== X-Received: by 2002:a05:6214:3011:b0:626:199e:1b7d with SMTP id ke17-20020a056214301100b00626199e1b7dmr2713120qvb.61.1685283635591; Sun, 28 May 2023 07:20:35 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5:38f3:5400:4ff:fe74:5668]) by smtp.gmail.com with ESMTPSA id l11-20020a0cc20b000000b006238dc71f5csm10qvh.144.2023.05.28.07.20.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 May 2023 07:20:35 -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 Cc: bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 1/8] bpf: Support ->show_fdinfo for kprobe_multi Date: Sun, 28 May 2023 14:20:20 +0000 Message-Id: <20230528142027.5585-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230528142027.5585-1-laoar.shao@gmail.com> References: <20230528142027.5585-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 X-Patchwork-State: RFC Currently, there is no way to check which functions are attached to a kprobe_multi link, causing confusion for users. It is important that we provide a means to expose these functions. The expected result is as follows, $ cat /proc/10936/fdinfo/9 pos: 0 flags: 02000000 mnt_id: 15 ino: 2094 link_type: kprobe_multi link_id: 2 prog_tag: a04f5eef06a7f555 prog_id: 11 func_count: 4 func_addrs: ffffffffaad475c0 ffffffffaad47600 ffffffffaad47640 ffffffffaad47680 $ cat /proc/10936/fdinfo/9 | grep "func_addrs" -A 4 | \ awk '{ if (NR ==1) {print $2} else {print $1}}' | \ awk '{"grep " $1 " /proc/kallsyms"| getline f; print f}' ffffffffaad475c0 T schedule_timeout_interruptible ffffffffaad47600 T schedule_timeout_killable ffffffffaad47640 T schedule_timeout_uninterruptible ffffffffaad47680 T schedule_timeout_idle Signed-off-by: Yafang Shao --- kernel/trace/bpf_trace.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 2bc41e6..0d84a7a 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2548,9 +2548,26 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) kfree(kmulti_link); } +static void bpf_kprobe_multi_link_show_fdinfo(const struct bpf_link *link, + struct seq_file *seq) +{ + struct bpf_kprobe_multi_link *kmulti_link; + int i; + + kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); + seq_printf(seq, "func_count:\t%d\n", kmulti_link->cnt); + for (i = 0; i < kmulti_link->cnt; i++) { + if (i == 0) + seq_printf(seq, "func_addrs:\t%lx\n", kmulti_link->addrs[i]); + else + seq_printf(seq, " \t%lx\n", kmulti_link->addrs[i]); + } +} + static const struct bpf_link_ops bpf_kprobe_multi_link_lops = { .release = bpf_kprobe_multi_link_release, .dealloc = bpf_kprobe_multi_link_dealloc, + .show_fdinfo = bpf_kprobe_multi_link_show_fdinfo, }; static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv) From patchwork Sun May 28 14:20:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13257851 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 95C12566E for ; Sun, 28 May 2023 14:20:38 +0000 (UTC) Received: from mail-qt1-x82c.google.com (mail-qt1-x82c.google.com [IPv6:2607:f8b0:4864:20::82c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50163BE for ; Sun, 28 May 2023 07:20:37 -0700 (PDT) Received: by mail-qt1-x82c.google.com with SMTP id d75a77b69052e-3f6c0d651adso28578171cf.2 for ; Sun, 28 May 2023 07:20:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685283636; x=1687875636; 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=dXpGPWz5BLT8lQdv0GfwGSvcA9bkfg8xrata1pia1ig=; b=IZqBLOkdA6PU2/Gnw3erZji9JnkMEl3cysWjcWlMOfA8iKJuXqLR0MRS22OxPlgBPa lWH6NDoiR4xMeRpPcwG8PyvSQN+KsHG24Zf9IZD/2JyZ3RVqZfOE1xTgv8Y2J64nOfgN ZqE7K5ZwWla34OD7/8E/R7nlEvXxTWSdOUyx/h5RZXZFWeAVWmsVRVMD1KpPpVGGikIh rAAlSnLRTkQAC+8hLgM1reiVNYU+pG2SjLaneq2a2rxS+WZ9ItmIy7QzPdikTO5VaK5F otMXIy9hihEO2jWq34enkvzlk4eu2VnF9Tn3QBme+X/XAnkfkK9q0DJpG1x2Xj/Zi+la MI9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685283636; x=1687875636; 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=dXpGPWz5BLT8lQdv0GfwGSvcA9bkfg8xrata1pia1ig=; b=ZAnEAwYCCopKd/EbnrRkMkB2KM4r6Tdre8H6fg/OZgVbeeO9KeEhKDEODA5CBiJRjG kLrj8waM7XK9IumQ6kwfnnxigzg2WTGn7x5Gf7eJQceiU76P4edMLbDOReanhSmyoTmo W7YRgTi7kmrGw6v/WLq/+07QoZl9XvREC03W4U4M03qjrKMZS1uOE/haGCEYCuTQ2f22 lUxzMr6RV8zfLbxyUqx2X1q/zvrOT32716gqNrcfLKR+NAvhbzVBPWJKWU67f9ZZ/8eD WPKyX/cE6FUcQ3tq7JbObDtlPApsm2GIlkKaDXAT8yfj2w4hmVS5lw6b45OaVP8ps/Mi xMPw== X-Gm-Message-State: AC+VfDxclsU2OizCACgwisXM6qu/FYaRl+yWHL5T0UQKspnJ79Q3rnYI QS3BvVl4qSuhlc3WgDEnUTc= X-Google-Smtp-Source: ACHHUZ54XQ1mo94lUZ3X/9MvbA5KFLF8WZ0elnJlBmzxlOwhBTK9g9lYaAxYrk3kZllHsyCvrOVTSQ== X-Received: by 2002:ad4:5f4d:0:b0:621:6bcb:e49 with SMTP id p13-20020ad45f4d000000b006216bcb0e49mr9550036qvg.0.1685283636479; Sun, 28 May 2023 07:20:36 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5:38f3:5400:4ff:fe74:5668]) by smtp.gmail.com with ESMTPSA id l11-20020a0cc20b000000b006238dc71f5csm10qvh.144.2023.05.28.07.20.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 May 2023 07:20:35 -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 Cc: bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 2/8] bpf: Support ->fill_link_info for kprobe_multi Date: Sun, 28 May 2023 14:20:21 +0000 Message-Id: <20230528142027.5585-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230528142027.5585-1-laoar.shao@gmail.com> References: <20230528142027.5585-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 X-Patchwork-State: RFC By adding support for ->fill_link_info to the kprobe_multi link, users will be able to inspect it using `bpftool link show`. This enhancement will expose both the count of probed functions and their respective addresses to the user. Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 4 ++++ kernel/trace/bpf_trace.c | 31 +++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 4 ++++ 3 files changed, 39 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 9273c65..6be9b1d 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6434,6 +6434,10 @@ struct bpf_link_info { __s32 priority; __u32 flags; } netfilter; + struct { + __aligned_u64 addrs; + __u32 count; + } kprobe_multi; }; } __attribute__((aligned(8))); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 0d84a7a..00a0009 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2564,10 +2564,41 @@ static void bpf_kprobe_multi_link_show_fdinfo(const struct bpf_link *link, } } +static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link, + struct bpf_link_info *info) +{ + struct bpf_kprobe_multi_link *kmulti_link; + u64 *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs); + u32 ucount = info->kprobe_multi.count; + int i; + + if (!uaddrs ^ !ucount) + return -EINVAL; + + kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); + if (!uaddrs) { + info->kprobe_multi.count = kmulti_link->cnt; + return 0; + } + + if (!ucount) + return 0; + + if (ucount != kmulti_link->cnt) + return -EINVAL; + + for (i = 0; i < ucount; i++) + if (copy_to_user(uaddrs + i, kmulti_link->addrs + i, + 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, .show_fdinfo = bpf_kprobe_multi_link_show_fdinfo, + .fill_link_info = bpf_kprobe_multi_link_fill_link_info, }; static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 9273c65..6be9b1d 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6434,6 +6434,10 @@ struct bpf_link_info { __s32 priority; __u32 flags; } netfilter; + struct { + __aligned_u64 addrs; + __u32 count; + } kprobe_multi; }; } __attribute__((aligned(8))); From patchwork Sun May 28 14:20:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13257852 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 99308566E for ; Sun, 28 May 2023 14:20:39 +0000 (UTC) Received: from mail-qv1-xf33.google.com (mail-qv1-xf33.google.com [IPv6:2607:f8b0:4864:20::f33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42CF5B8 for ; Sun, 28 May 2023 07:20:38 -0700 (PDT) Received: by mail-qv1-xf33.google.com with SMTP id 6a1803df08f44-6261a25e9b6so3230646d6.0 for ; Sun, 28 May 2023 07:20:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685283637; x=1687875637; 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=AsZic1Ur1WdSbptVgbWV/7Kr8PTZ6BiLFray3Omc+wk=; b=D7z/2zPooVWUeIRRKrFUR362pGLXCC+0P4srK+ZruKScFGuSCoNeCEooEj5jxwhYqU BG/C/1/T/U1zmq/hKSG49ZzMFK00C1QoBaQfET+3n6wmNMi4yQFYbxKMtcfyi26ooqpA ObRrOzLsPQ003S+UXvRWbRVNC2oZVvr63/y0xBhguI+Ga263ThfjsYowyk2qpSO9XDY7 1K1ei+7gPgLXUFh1OMbN7Bx8FdcWXr+kq7I4pUfc2r0yUsl/4exqbdJ8EkK4EMfrki2B hP5P94dboGWlS8WpV67GyoKVp99bvdCkreGLSOvBmZLJy2x0Y2x99rdjGSXRDrE3RIN/ oPoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685283637; x=1687875637; 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=AsZic1Ur1WdSbptVgbWV/7Kr8PTZ6BiLFray3Omc+wk=; b=ea1CGwB06K37PrBEARK+O1ITrSb9yi2ZBk/iNgF5HcAmhZvUv5FlQHPRrDF8s2mF49 tWb7HIjLFLd2wpqnygj1irpTznGXG38R0GXbekCSB91unFO3fA27WQO0AyQKo7zhsrqD G2rOknOV7+aP11W2WbdCHcUEihK6MFojY4/3KbeDIEACjB/S6lsOMhpINqcywd8X1R7i OxEM3Y/7W517X2+w2ry23h7Wsq+5SRCnmg5SuRv5gJtRGpKVN+k12iprBKlSansrSq50 rDwAEsCmHBnc6ACvsb3YXTnxIu+XI74bWE4OoVgjgCARmaDrycm053hsHT2FrWoBB7H1 5meQ== X-Gm-Message-State: AC+VfDyvgsGp0ygpy8K7UJqwDDadKHauaGOKusXzZyomHAY64nCIJeIx PFraUMTq/L12cpOC0fPaCAA= X-Google-Smtp-Source: ACHHUZ6b0nynY/xkM0Rfgho2fx5tBNFR6YMdpw20CQ8PzdLV6pcmgHNxUHh2fz55gthGtecBkb3qgw== X-Received: by 2002:ad4:4ee7:0:b0:626:9e7:7347 with SMTP id dv7-20020ad44ee7000000b0062609e77347mr7449778qvb.55.1685283637390; Sun, 28 May 2023 07:20:37 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5:38f3:5400:4ff:fe74:5668]) by smtp.gmail.com with ESMTPSA id l11-20020a0cc20b000000b006238dc71f5csm10qvh.144.2023.05.28.07.20.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 May 2023 07:20:36 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 3/8] bpftool: Show probed function in kprobe_multi link info Date: Sun, 28 May 2023 14:20:22 +0000 Message-Id: <20230528142027.5585-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230528142027.5585-1-laoar.shao@gmail.com> References: <20230528142027.5585-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 X-Patchwork-State: RFC Show the already expose kprobe_multi link info in bpftool. The result as follows, $ bpftool link show 2: kprobe_multi prog 11 func_cnt 4 addrs ffffffffaad475c0 ffffffffaad47600 ffffffffaad47640 ffffffffaad47680 pids trace(10936) $ bpftool link show -j [{"id":1,"type":"perf_event","prog_id":5,"bpf_cookie":0,"pids":[{"pid":10658,"comm":"trace"}]},{"id":2,"type":"kprobe_multi","prog_id":11,"func_cnt":4,"addrs":[18446744072280634816,18446744072280634880,18446744072280634944,18446744072280635008],"pids":[{"pid":10936,"comm":"trace"}]},{"id":120,"type":"iter","prog_id":266,"target_name":"bpf_map"},{"id":121,"type":"iter","prog_id":267,"target_name":"bpf_prog"}] $ bpftool link show | grep -A 1 "func_cnt" | \ awk '{if (NR == 1) {print $4; print $5;} else {print $1; print $2} }' | \ awk '{"grep " $1 " /proc/kallsyms" | getline f; print f;}' ffffffffaad475c0 T schedule_timeout_interruptible ffffffffaad47600 T schedule_timeout_killable ffffffffaad47640 T schedule_timeout_uninterruptible ffffffffaad47680 T schedule_timeout_idle Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 2d78607..76f1bb2 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -218,6 +218,20 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) jsonw_uint_field(json_wtr, "map_id", info->struct_ops.map_id); break; + case BPF_LINK_TYPE_KPROBE_MULTI: + const __u64 *addrs; + __u32 i; + + jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count); + if (!info->kprobe_multi.count) + break; + jsonw_name(json_wtr, "addrs"); + jsonw_start_array(json_wtr); + addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs); + for (i = 0; i < info->kprobe_multi.count; i++) + jsonw_lluint(json_wtr, addrs[i]); + jsonw_end_array(json_wtr); + break; default: break; } @@ -396,6 +410,24 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) case BPF_LINK_TYPE_NETFILTER: netfilter_dump_plain(info); break; + case BPF_LINK_TYPE_KPROBE_MULTI: + __u32 indent, cnt, i; + const __u64 *addrs; + + cnt = info->kprobe_multi.count; + if (!cnt) + break; + printf("\n\tfunc_cnt %d addrs", cnt); + for (i = 0; cnt; i++) + cnt /= 10; + indent = strlen("func_cnt ") + i + strlen(" addrs"); + addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs); + for (i = 0; i < info->kprobe_multi.count; i++) { + if (i && !(i & 0x1)) + printf("\n\t%*s", indent, ""); + printf(" %0*llx", 16, addrs[i]); + } + break; default: break; } @@ -417,7 +449,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 +475,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 = malloc(count * sizeof(__u64)); + if (!addrs) { + p_err("mem alloc failed"); + close(fd); + return -1; + } + info.kprobe_multi.addrs = (unsigned long)addrs; + goto again; + } + } if (json_output) show_link_close_json(fd, &info); else show_link_close_plain(fd, &info); + if (addrs) + free(addrs); close(fd); return 0; } From patchwork Sun May 28 14:20:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13257853 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 44C09566E for ; Sun, 28 May 2023 14:20:40 +0000 (UTC) Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 109D6BE for ; Sun, 28 May 2023 07:20:39 -0700 (PDT) Received: by mail-qk1-x729.google.com with SMTP id af79cd13be357-75cbbb10c69so223635385a.2 for ; Sun, 28 May 2023 07:20:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685283638; x=1687875638; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QuzOX4XFKmJOiLjt9d1z+UYL8/sI/TO2hkUeE9dOaVY=; b=pIgpfthuhb6Mnhw56ecRXBRcJW7j7c+VluFti7CCeBBkPQnolVzro99n2d2k3q9wVh tyfN1xiO5jfi+d+tlbW3+NWekJ7h8Y6xRnAwHEsGPihhN8+9XmYAdNUD/Lt+Aw64Q1so rjNhogHoiwaNmmGqUjafIirLMVrYkAdGdG9y2S995F56f+MgPuiIjVSiPa0zR1CBvYQi h1ya5fEltpM9yRVCQC88u54zZFmLl9mxKSP2ZNRbZVeASAcsNEUeDX2kfFgFDXGJGEE8 2bnEv8X+tDuc33kDx/H6paD4ZujvpsnDdLDFm9uFLhSm+o5eqNhewYr3+b9deOA21wki AH0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685283638; x=1687875638; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QuzOX4XFKmJOiLjt9d1z+UYL8/sI/TO2hkUeE9dOaVY=; b=SUdK064vgcLu1EmWVoFFdsgOy5Wtqj+NEfhUueLFGYScI5wJbGpYTHVWADSqSmIRd3 t8GH4U/8AFJlApG7zpKt+yrIp3bCIArQFtICdx+UCYu7GwSfeOLW9od2dWVgA3JljjT2 8z8xxmArGEZr4M9jlTI1UhJv/xa0WOSQel0S6FX7W60dgBV8gfYlmNBZwSRCbWgFgYzA TNoCs0k06u63xeNnhLKWh10U2MW0DJVa6ffSW3zULNNjK2DZATneGe9Qw/urbMT0Fi5d 7zB0+aIb/nltHGwgbV5mfbRyEqT4BAPGseGcL5PMvHrm6UqKdj9GltavFFgh/ZyR4H8c 39fw== X-Gm-Message-State: AC+VfDzq5NjPKpv4k8TtWKLCu/17ujVJD1EE2nHVLY3aEUd5cxgiBL4C +0cBoY3DSGB1EG2vhCRrWTs= X-Google-Smtp-Source: ACHHUZ57oNNg1hSza9KcPDdCpPFY57VKUIzi+oKQ2Q1nc1sxswNBL2+BbgUC1D47dho3Y4wzplVlfg== X-Received: by 2002:a05:6214:1c48:b0:619:ca55:9709 with SMTP id if8-20020a0562141c4800b00619ca559709mr8789386qvb.21.1685283638186; Sun, 28 May 2023 07:20:38 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5:38f3:5400:4ff:fe74:5668]) by smtp.gmail.com with ESMTPSA id l11-20020a0cc20b000000b006238dc71f5csm10qvh.144.2023.05.28.07.20.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 May 2023 07:20:37 -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 Cc: bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 4/8] bpf: Always expose the probed address Date: Sun, 28 May 2023 14:20:23 +0000 Message-Id: <20230528142027.5585-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230528142027.5585-1-laoar.shao@gmail.com> References: <20230528142027.5585-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 X-Patchwork-State: RFC Since different symbols can share the same name, it is insufficient to only expose the symbol name. It is essential to also expose the symbol address so that users can accurately identify which one is being probed. Signed-off-by: Yafang Shao --- kernel/trace/trace_kprobe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 59cda19..a7a905a 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -1547,7 +1547,7 @@ int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type, if (tk->symbol) { *symbol = tk->symbol; *probe_offset = tk->rp.kp.offset; - *probe_addr = 0; + *probe_addr = (unsigned long)tk->rp.kp.addr; } else { *symbol = NULL; *probe_offset = 0; From patchwork Sun May 28 14:20:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13257854 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 A194C566E for ; Sun, 28 May 2023 14:20:41 +0000 (UTC) Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF501B8 for ; Sun, 28 May 2023 07:20:39 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id d75a77b69052e-3f6a3a76665so19225241cf.1 for ; Sun, 28 May 2023 07:20:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685283639; x=1687875639; 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=ODhBZoAYfxi0mcbicAztGxp9Mvcqb5LLQ3RqkHtqQYg=; b=I9ZmF+wzWUPhDuN2zSxdiCKsckosQPRetOGdeUYNHgBsSyYsB+jLHF6fYGzmjU3pgm yTk7fVuE8e+7OJPiygxXZIz4G7oiQKS29vhoRGpPogfKa3v9k6Td3F3dLw4VB7IwfX+d qqNDVbzZcsHvmEOKQxhvZGR83+r2XEfDo4WayjtEFKKD8vKXuyC5dtKuKjSfdCQI4gp/ DWfSY8Ly4YbvW9l2XIn2IyghFojrMhUnG5NvCSdnVpmEnH4QmiDSQfrHBYBIRIOqVnL4 invJu/u7RkTdpw9rIyvkEf2yt1X3h900rOzJQTroEIVUFbicKbJvTmrpspzi60nGOv0J 5xyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685283639; x=1687875639; 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=ODhBZoAYfxi0mcbicAztGxp9Mvcqb5LLQ3RqkHtqQYg=; b=JSMZnBwGVXcsDqX2ERgDNReUJKRL40HEfNsH/Vi8uJJActiImo9mQv6k0NpKZtG642 sN5QUnT+QCvHnBf9hZJZ+5HBoWS2jyNaqDNcbKtDcxtuhfnJNfPy/b5QqfR0qHyRghHt 4SRrFzmMCm37TvRR0QPQgo/+bYrytLv36tu4LAjRWfrlpGjTnMsmBOxR5awprKV7kzcQ fw09kpWCC3K2+JboXr3Y0n6h+T3gw8aXYVJFoVJIoxOmtNy7HQiauP4KlGpP3SfLq5h5 qhnnPqHFxnHCVsBF9IRJ6ccHVhplnjScY5+ZLiXxGBAWH8xCo3c8SNTU7K67xOb0guQa q+2A== X-Gm-Message-State: AC+VfDxueM3EYkWjA/UScEurV79gFQ2ZmPBTLxgR5TCRQXW+n1Aqf8fC TX6mP1Oml7qLfu0QxO1zlqs= X-Google-Smtp-Source: ACHHUZ57HJVGSogcsFD2ZVInOh2ZubtsJ6ZbdjVqg3+mARP5OCFEDBcKd5Kk31nhcGxPsQbLibbQ3Q== X-Received: by 2002:a05:6214:4015:b0:616:859a:471a with SMTP id kd21-20020a056214401500b00616859a471amr4814126qvb.17.1685283638991; Sun, 28 May 2023 07:20:38 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5:38f3:5400:4ff:fe74:5668]) by smtp.gmail.com with ESMTPSA id l11-20020a0cc20b000000b006238dc71f5csm10qvh.144.2023.05.28.07.20.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 May 2023 07:20:38 -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 Cc: bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 5/8] bpf: Support ->show_fdinfo for perf_event Date: Sun, 28 May 2023 14:20:24 +0000 Message-Id: <20230528142027.5585-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230528142027.5585-1-laoar.shao@gmail.com> References: <20230528142027.5585-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 X-Patchwork-State: RFC By adding support for ->show_fdinfo to the perf_event link, users will be able to examine it through the task's fdinfo. The expected result is as follows: $ cat /proc/9637/fdinfo/11 pos: 0 flags: 02000000 mnt_id: 15 ino: 2094 link_type: perf link_id: 1 prog_tag: a04f5eef06a7f555 prog_id: 5 func: kernel_clone addr: ffffffff8d0bc310 offset: 0 Signed-off-by: Yafang Shao --- kernel/bpf/syscall.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 92a57ef..e6b5127 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3297,9 +3297,36 @@ static void bpf_perf_link_dealloc(struct bpf_link *link) kfree(perf_link); } +static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, + struct seq_file *seq) +{ + struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); + const struct perf_event *event; + u64 probe_offset, probe_addr; + u32 prog_id, fd_type; + const char *buf; + int err; + + event = perf_get_event(perf_link->perf_file); + if (IS_ERR(event)) + return; + + err = bpf_get_perf_event_info(event, &prog_id, &fd_type, + &buf, &probe_offset, + &probe_addr); + if (err) + return; + + if (buf) + seq_printf(seq, "func:\t%s\n", buf); + seq_printf(seq, "addr:\t%llx\n", probe_addr); + seq_printf(seq, "offset:\t%llu\n", probe_offset); +} + static const struct bpf_link_ops bpf_perf_link_lops = { .release = bpf_perf_link_release, .dealloc = bpf_perf_link_dealloc, + .show_fdinfo = bpf_perf_link_show_fdinfo, }; static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) From patchwork Sun May 28 14:20:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13257855 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 2B92B566E for ; Sun, 28 May 2023 14:20:42 +0000 (UTC) Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1DB9BE for ; Sun, 28 May 2023 07:20:40 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-75b0df7b225so148996785a.1 for ; Sun, 28 May 2023 07:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685283640; x=1687875640; 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=3m3WbT+7kNIyhOS6++rBbmSoW09np9ycUVNSp6pOAWc=; b=lk5ZrpwCrOGLuVLDT3//gA79yQ0PK+BCM9RcKk+1b6JIJur22f4yG0zoE9hgP2olQX CA8YW4xNqtptJWPwo6O9uiLIqDDgCw/dwYHVflyizEQqA66zKTz9Zls8eXg0kYfjgc7/ /3Q4NkYND1ImSjkDaR7i4LazxjKg6uXZfKCu2SZlv+1zF6C3O/Z3mL9uyw5pgz+/YM0l j5qIf459U+dKWUZIErDH7SXgRyyy+mQaBUCMxyygy84TadtZ58LuqYYWXS9pqwZJQ19K EDhjhiMY/JzAuzkSo8N0t16ZNNo7EHHhWrxu17PabyBZDeN8DXMr2aIKm4uzvGU/HPvV KA/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685283640; x=1687875640; 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=3m3WbT+7kNIyhOS6++rBbmSoW09np9ycUVNSp6pOAWc=; b=HQB3wkOKEMu7qghXOQlIgMmGOIrgqmmGCsf7vJT6qGAfeXG9I/wmCS2Pq5ZYr1kps1 XFIVqBQ8/kHKK4WY8bbm84CsEXpE7c6HiuoPULdI6ITXUj7RR0dswAuYeW4AygEy9GS7 CSK8gLSHLqWozjj88AhyeFG5RogNzi4IzZsLCi93xlIVvf2Q9pUj4dVKTKWMSwjoQQV4 QMtrTzqSCl0leDCR1zi6eBqtO9zmRcAmQ2uafd78F0ncdVnfB1Yo6uTZQGW8JtGt2bF4 4XJPtTBoMsHUrax9E42NPCduujVX5284WPwreNZWM4cLo/oAM44GJbkmavRRldAUx7DR 9IjA== X-Gm-Message-State: AC+VfDx5i25HDeWENvEmJTHTbLt6qbWq/7OwIjR+9+pbws+rxtZs+tGq i+0GfQ4+r4TctiSQoI6X9E0= X-Google-Smtp-Source: ACHHUZ6Fhp+9nLvYI6UT2Be3F6yZtd09gLtsMXQ6sdHAG0n7eTcylYkRFSrKNqgvlXr8ZQUjhuK6TQ== X-Received: by 2002:a05:6214:27c6:b0:625:b3b1:71da with SMTP id ge6-20020a05621427c600b00625b3b171damr8372995qvb.56.1685283639878; Sun, 28 May 2023 07:20:39 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5:38f3:5400:4ff:fe74:5668]) by smtp.gmail.com with ESMTPSA id l11-20020a0cc20b000000b006238dc71f5csm10qvh.144.2023.05.28.07.20.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 May 2023 07:20:39 -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 Cc: bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 6/8] bpf: Add a common helper bpf_copy_to_user() Date: Sun, 28 May 2023 14:20:25 +0000 Message-Id: <20230528142027.5585-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230528142027.5585-1-laoar.shao@gmail.com> References: <20230528142027.5585-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 X-Patchwork-State: RFC Add a common helper bpf_copy_to_user(), which will be used at multiple places. Signed-off-by: Yafang Shao --- kernel/bpf/syscall.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index e6b5127..33a72ec 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 Sun May 28 14:20:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13257856 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 567F6566E for ; Sun, 28 May 2023 14:20:43 +0000 (UTC) Received: from mail-qv1-xf36.google.com (mail-qv1-xf36.google.com [IPv6:2607:f8b0:4864:20::f36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABB51C7 for ; Sun, 28 May 2023 07:20:41 -0700 (PDT) Received: by mail-qv1-xf36.google.com with SMTP id 6a1803df08f44-6260a9ef126so10476476d6.2 for ; Sun, 28 May 2023 07:20:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685283641; x=1687875641; 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=0BZgF3NwqqEP5WsJnEoKQ69scbxD7d0B2xVAyMAX9Is=; b=pZQBuvwNcT9NGwDibmjUsvSU6I5Q97XBJ8BrLAV+z3dYjbMQJksiVDHGKzHs2Rn2pu uUGNMWuM9SEnrz+hsqNF/8z643Ds0p5IdjVxqHOTPIcmJp6LQ7g8eqm2LQeatnEOx27s MMZzj6KvhAk6GJlRYqjAS9uWbEHyWHqcbDx7iHl84RSp4wluZxqkixjRVpS8VX3kepr6 BF5anr21B8RHBo8TgjgzAbbi4afQ+BEqrI6tVj80la/b+KEQv7UiRbBZGCZXMIUAaXfz nKqVZCuiADE84GHHqS5xopGWdsTzr+SMs1Q+S5SvP2YPjaOoZG5tOGJW2K/co0102qoy Z39A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685283641; x=1687875641; 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=0BZgF3NwqqEP5WsJnEoKQ69scbxD7d0B2xVAyMAX9Is=; b=LtO3Sn4p9FrY4ItPt9nsclSpyL3b+Ti9lPBDXMiVKtsd0IEbWQs8A+uGiCpQsMEGU8 h9vFyVnotVzjuO0QpcITt+tAYUhvqxcAVQD30sLV9AHgeqb/t9i/j7J0bSZo/4Pq8RLI gb85uJ9osV7icZnAzvzU5HRL21I9S+Eeu8jtKoqZRAGH7xDWG4emEfuL9cXW2jPj5WKG vfpYWiNz3RLKdKZEXM0aW3MVYzDiQSAmoJ+XXq09sGdUHBfmlA36JPS4aDqTiBiML8h1 R10fQYMDpHSKu8izk38dwm/2RTsuElcsH8FfDrs7f5e+MVd/t5LHMwsE62GeQHJDJMvh +Zbg== X-Gm-Message-State: AC+VfDz2SrtN1fP8NN5B4q38IpfrSSaYXsARFq1J4r8OmfcnmBYygtGS LXYDhC74WB/dIwJ7HLpIfEfPfjxervllX6Y9uoI= X-Google-Smtp-Source: ACHHUZ6vXe5ADKAAPgI0St1FUXlr36q3Sg/gipHwN4cs9eykV4DOgz+ondpbWcMsOjyr3ZYNm1Fmxw== X-Received: by 2002:a05:6214:1ccd:b0:623:9126:8d7b with SMTP id g13-20020a0562141ccd00b0062391268d7bmr6316616qvd.28.1685283640818; Sun, 28 May 2023 07:20:40 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5:38f3:5400:4ff:fe74:5668]) by smtp.gmail.com with ESMTPSA id l11-20020a0cc20b000000b006238dc71f5csm10qvh.144.2023.05.28.07.20.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 May 2023 07:20:40 -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 Cc: bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 7/8] bpf: Support ->fill_link_info for perf_event Date: Sun, 28 May 2023 14:20:26 +0000 Message-Id: <20230528142027.5585-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230528142027.5585-1-laoar.shao@gmail.com> References: <20230528142027.5585-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 X-Patchwork-State: RFC By adding support for ->fill_link_info to the perf_event link, users will be able to inspect it using `bpftool link show`. While users can currently access this information via `bpftool perf show`, consolidating the link information for all link types in one place would be more convenient. Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 6 ++++++ kernel/bpf/syscall.c | 46 ++++++++++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 6 ++++++ 3 files changed, 58 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 6be9b1d..1f2be1d 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6438,6 +6438,12 @@ struct bpf_link_info { __aligned_u64 addrs; __u32 count; } kprobe_multi; + struct { + __aligned_u64 name; + __aligned_u64 addr; + __u32 name_len; + __u32 offset; + } perf_event; }; } __attribute__((aligned(8))); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 33a72ec..b12707e 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3329,10 +3329,56 @@ static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, seq_printf(seq, "offset:\t%llu\n", probe_offset); } +static int bpf_perf_link_fill_link_info(const struct bpf_link *link, + struct bpf_link_info *info) +{ + struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); + char __user *ubuf = u64_to_user_ptr(info->perf_event.name); + u32 ulen = info->perf_event.name_len; + const struct perf_event *event; + u64 probe_offset, probe_addr; + u32 prog_id, fd_type; + const char *buf; + size_t len; + int err; + + if (!ulen ^ !ubuf) + return -EINVAL; + if (!ubuf) + return 0; + + event = perf_get_event(perf_link->perf_file); + if (IS_ERR(event)) + return PTR_ERR(event); + + err = bpf_get_perf_event_info(event, &prog_id, &fd_type, + &buf, &probe_offset, + &probe_addr); + if (err) + return err; + + len = strlen(buf); + info->perf_event.name_len = len + 1; + if (buf) { + err = bpf_copy_to_user(ubuf, buf, ulen, len); + if (err) + return err; + } else { + char zero = '\0'; + + if (put_user(zero, ubuf)) + return -EFAULT; + } + info->perf_event.addr = probe_addr; + info->perf_event.offset = probe_offset; + return 0; +} + static const struct bpf_link_ops bpf_perf_link_lops = { .release = bpf_perf_link_release, .dealloc = bpf_perf_link_dealloc, .show_fdinfo = bpf_perf_link_show_fdinfo, + .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 6be9b1d..1f2be1d 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6438,6 +6438,12 @@ struct bpf_link_info { __aligned_u64 addrs; __u32 count; } kprobe_multi; + struct { + __aligned_u64 name; + __aligned_u64 addr; + __u32 name_len; + __u32 offset; + } perf_event; }; } __attribute__((aligned(8))); From patchwork Sun May 28 14:20:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13257857 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 250F7566E for ; Sun, 28 May 2023 14:20:44 +0000 (UTC) Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB660B8 for ; Sun, 28 May 2023 07:20:42 -0700 (PDT) Received: by mail-qk1-x72d.google.com with SMTP id af79cd13be357-75b1219506fso151517885a.1 for ; Sun, 28 May 2023 07:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685283642; x=1687875642; 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=s+1lG7fYk1KAlA2kK4vySeVdKUS0VEfV5ykRXmC3a24=; b=HUpk4I9OrwvuC8v9ZP4Rv82epTrtAr30LRiDyxDIRW300E3ox/idingYq/v+vp0Ueo 2ZCxm+SV2pcstVALLEIqCz9nioW4qrAoCQtzfacdOMO/WpiXH1IALKUjL2FRrehvELIQ g9f+70M1g95xGRXTn3RRHgT8sBRx2WHR+CWmqY9e4H432wZXK5mvUBnhIprx6sas4cv4 txWj7Fxrg5weWk5telO6T/oIsIrsnjna7ZParcYTUiOOLu3Clmn1ExxMWstdnwXB6Nkg 6xzTSpd0XJwWo7XkDDO0iPS+aTeiuq3pmtn4oUuSUqN1nZL1KD/TNW3CV3eF3O/woGXx qHdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685283642; x=1687875642; 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=s+1lG7fYk1KAlA2kK4vySeVdKUS0VEfV5ykRXmC3a24=; b=IA9ILlirqpMK28cG3A7p+2WNgT6Ro9Hivb774FRqBetHPyYgC5qjhKRZXk7kO1o0Gz RGdzv3ulxa7fy0Qq++UqUUbo2jdv+Pki9HU24jx4Yighd/wRtc7bvA1ZVueUpk/d13lB 1dwX+/HyCMfLitlXqrT2xbzo95eliLUUl90rOiPSyl6wGDx6ebziymcVNc6XAOfaWjZe rhKNhcp+L1E9RYMo/uMEVmsNXHXOZG4J9GyJ5yq97u1kHxeGtlBUEw+KO0N0fzNLMypZ KTmK4+ZndsXHVHZVDM6ENgDAzDyTMHGDOFE3nHKidbsfe4GLlnsffkrdbyoi5W91F540 EKpQ== X-Gm-Message-State: AC+VfDwXwDSH9BkMJObZWgdFO95dB+CMcIRTojBFWqnnfjdt1eTaIiOI S+cJIyJW84NfBMwQkjPjBOw= X-Google-Smtp-Source: ACHHUZ6LH3V0exew9UYl+eJEX93578UmtTeeTsy+3Yo732HahjTXiDl7zBelrH8NK197ygj1oKE37g== X-Received: by 2002:ad4:5be9:0:b0:625:d55:eaab with SMTP id k9-20020ad45be9000000b006250d55eaabmr6991697qvc.9.1685283641695; Sun, 28 May 2023 07:20:41 -0700 (PDT) Received: from vultr.guest ([2001:19f0:5:38f3:5400:4ff:fe74:5668]) by smtp.gmail.com with ESMTPSA id l11-20020a0cc20b000000b006238dc71f5csm10qvh.144.2023.05.28.07.20.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 May 2023 07:20:41 -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 Cc: bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 8/8] bpftool: Show probed function in perf_event link info Date: Sun, 28 May 2023 14:20:27 +0000 Message-Id: <20230528142027.5585-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230528142027.5585-1-laoar.shao@gmail.com> References: <20230528142027.5585-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 X-Patchwork-State: RFC Show the exposed perf_event link info in bpftool. The result as follows, $ bpftool link show 1: perf_event prog 5 func kernel_clone addr ffffffffb40bc310 offset 0 bpf_cookie 0 pids trace(9726) $ bpftool link show -j [{"id":1,"type":"perf_event","prog_id":5,"func":"kernel_clone","addr":18446744072435254032,"offset":0,"bpf_cookie":0,"pids":[{"pid":9726,"comm":"trace"}]}] Signed-off-by: Yafang Shao --- tools/bpf/bpftool/link.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 76f1bb2..8493a05 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -232,6 +232,12 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) jsonw_lluint(json_wtr, addrs[i]); jsonw_end_array(json_wtr); break; + case BPF_LINK_TYPE_PERF_EVENT: + jsonw_string_field(json_wtr, "func", + u64_to_ptr(info->perf_event.name)); + jsonw_uint_field(json_wtr, "addr", info->perf_event.addr); + jsonw_uint_field(json_wtr, "offset", info->perf_event.offset); + break; default: break; } @@ -368,7 +374,7 @@ void netfilter_dump_plain(const struct bpf_link_info *info) static int show_link_close_plain(int fd, struct bpf_link_info *info) { struct bpf_prog_info prog_info; - const char *prog_type_str; + const char *prog_type_str, *buf; int err; show_link_header_plain(info); @@ -428,6 +434,12 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) printf(" %0*llx", 16, addrs[i]); } break; + case BPF_LINK_TYPE_PERF_EVENT: + buf = (const char *)u64_to_ptr(info->perf_event.name); + if (buf[0] != '\0' || info->perf_event.addr) + printf("\n\tfunc %s addr %llx offset %d ", buf, + info->perf_event.addr, info->perf_event.offset); + break; default: break; } @@ -454,6 +466,7 @@ static int do_show_link(int fd) int count; int err; + buf[0] = '\0'; memset(&info, 0, sizeof(info)); again: err = bpf_link_get_info_by_fd(fd, &info, &len); @@ -489,6 +502,12 @@ static int do_show_link(int fd) goto again; } } + if (info.type == BPF_LINK_TYPE_PERF_EVENT && + !info.perf_event.name) { + info.perf_event.name = (unsigned long)&buf; + info.perf_event.name_len = sizeof(buf); + goto again; + } if (json_output) show_link_close_json(fd, &info);