From patchwork Thu Aug 27 00:06:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 261875 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61CFEC433E1 for ; Thu, 27 Aug 2020 00:06:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3E26620791 for ; Thu, 27 Aug 2020 00:06:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="XfObal2+" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726856AbgH0AG3 (ORCPT ); Wed, 26 Aug 2020 20:06:29 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:52122 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726786AbgH0AG0 (ORCPT ); Wed, 26 Aug 2020 20:06:26 -0400 Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 07R05YuX032208 for ; Wed, 26 Aug 2020 17:06:26 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=PoctbjKW2PqcwUYfWkP0gtzAdd17ljuE+ugTmwzAGbI=; b=XfObal2+nvUMOQ1b71lZ9DHb6y1ltHC5E8Nu6xjcmgVVokcNm2iTz+2Hy0aAWbxX3moo kb6EmhBwJWC+oYRMYL+Y+RXuFOvDdXje63peTtZiWzZdKyBOooqWHQ5es3J3KudU1IRV IV9EPp4mvVj+UDhV3CrMdBFHykYVrlKhua4= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 335up8jbxp-9 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 26 Aug 2020 17:06:26 -0700 Received: from intmgw003.03.ash8.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::d) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Wed, 26 Aug 2020 17:06:22 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id 103843705306; Wed, 26 Aug 2020 17:06:19 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Yonghong Song Smtp-Origin-Hostname: devbig003.ftw2.facebook.com To: , CC: Alexei Starovoitov , Daniel Borkmann , Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf-next 1/5] bpf: make bpf_link_info.iter similar to bpf_iter_link_info Date: Wed, 26 Aug 2020 17:06:19 -0700 Message-ID: <20200827000619.2711883-1-yhs@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200827000618.2711826-1-yhs@fb.com> References: <20200827000618.2711826-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235, 18.0.687 definitions=2020-08-26_14:2020-08-26,2020-08-26 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 malwarescore=0 lowpriorityscore=0 mlxlogscore=827 clxscore=1015 bulkscore=0 adultscore=0 impostorscore=0 priorityscore=1501 phishscore=0 spamscore=0 mlxscore=0 suspectscore=8 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2008260187 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org bpf_link_info.iter is used by link_query to return bpf_iter_link_info to user space. Fields may be different ,e.g., map_fd vs. map_id, so we cannot reuse the exact structure. But make them similar, e.g., struct bpf_link_info { /* common fields */ union { struct { ... } raw_tracepoint; struct { ... } tracing; ... struct { /* common fields for iter */ union { struct { __u32 map_id; } map; /* other structs for other targets */ }; }; }; }; so the structure is extensible the same way as bpf_iter_link_info. Fixes: 6b0a249a301e ("bpf: Implement link_query for bpf iterators") Signed-off-by: Yonghong Song --- include/uapi/linux/bpf.h | 6 ++++-- tools/include/uapi/linux/bpf.h | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 0388bc0200b0..ef7af384f5ee 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4251,8 +4251,10 @@ struct bpf_link_info { __aligned_u64 target_name; /* in/out: target_name buffer ptr */ __u32 target_name_len; /* in/out: target_name buffer len */ union { - __u32 map_id; - } map; + struct { + __u32 map_id; + } map; + }; } iter; struct { __u32 netns_ino; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 0388bc0200b0..ef7af384f5ee 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4251,8 +4251,10 @@ struct bpf_link_info { __aligned_u64 target_name; /* in/out: target_name buffer ptr */ __u32 target_name_len; /* in/out: target_name buffer len */ union { - __u32 map_id; - } map; + struct { + __u32 map_id; + } map; + }; } iter; struct { __u32 netns_ino; From patchwork Thu Aug 27 00:06:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 261874 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D182AC433E3 for ; Thu, 27 Aug 2020 00:06:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AE94720825 for ; Thu, 27 Aug 2020 00:06:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="Qa6ZuEW1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726894AbgH0AGe (ORCPT ); Wed, 26 Aug 2020 20:06:34 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:19248 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726845AbgH0AG3 (ORCPT ); Wed, 26 Aug 2020 20:06:29 -0400 Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 07R05Yuc032208 for ; Wed, 26 Aug 2020 17:06:28 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=Bj1FGEHaoqbiaRh/ZSXUetNzMCXqGCc22v1K92GuKZA=; b=Qa6ZuEW1V4JApraTk7w1bK9v4bySl69LE4d2lUQjJgXcXIlpQZsasaNKU/Kg35CmPkQ9 7L+y9UAH9phQP5u/1c6sXcxjX25bMD/Gy+P9ACIIj7J/0omcLzIO+RGFlkac33jtbaHy 65Rd8tdvBYsRxPxnIzCUe1LrU7tBZ4NHgUI= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 335up8jbxp-14 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 26 Aug 2020 17:06:28 -0700 Received: from intmgw003.03.ash8.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::d) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Wed, 26 Aug 2020 17:06:23 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id C3EE137052E0; Wed, 26 Aug 2020 17:06:21 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Yonghong Song Smtp-Origin-Hostname: devbig003.ftw2.facebook.com To: , CC: Alexei Starovoitov , Daniel Borkmann , Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf-next 3/5] bpf: add link_query support for newly added main_thread_only info Date: Wed, 26 Aug 2020 17:06:21 -0700 Message-ID: <20200827000621.2712111-1-yhs@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200827000618.2711826-1-yhs@fb.com> References: <20200827000618.2711826-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235, 18.0.687 definitions=2020-08-26_14:2020-08-26,2020-08-26 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 malwarescore=0 lowpriorityscore=0 mlxlogscore=999 clxscore=1015 bulkscore=0 adultscore=0 impostorscore=0 priorityscore=1501 phishscore=0 spamscore=0 mlxscore=0 suspectscore=8 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2008260187 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Added support for link_query for main_thread_only information with task/task_file iterators. Signed-off-by: Yonghong Song --- include/uapi/linux/bpf.h | 5 +++++ kernel/bpf/task_iter.c | 17 +++++++++++++++++ tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 27 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index af5c600bf673..595bdc4c9431 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4259,6 +4259,11 @@ struct bpf_link_info { struct { __u32 map_id; } map; + + struct { + __u32 main_thread_only:1; + __u32 :31; + } task; }; } iter; struct { diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index 362bf2dda63a..7636abe05f27 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -329,6 +329,19 @@ static int bpf_iter_attach_task(struct bpf_prog *prog, return 0; } +static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, + struct seq_file *seq) +{ + seq_printf(seq, "main_thread_only:\t%u\n", aux->main_thread_only); +} + +static int bpf_iter_task_fill_link_info(const struct bpf_iter_aux_info *aux, + struct bpf_link_info *info) +{ + info->iter.task.main_thread_only = aux->main_thread_only; + return 0; +} + BTF_ID_LIST(btf_task_file_ids) BTF_ID(struct, task_struct) BTF_ID(struct, file) @@ -343,6 +356,8 @@ static const struct bpf_iter_seq_info task_seq_info = { static struct bpf_iter_reg task_reg_info = { .target = "task", .attach_target = bpf_iter_attach_task, + .show_fdinfo = bpf_iter_task_show_fdinfo, + .fill_link_info = bpf_iter_task_fill_link_info, .ctx_arg_info_size = 1, .ctx_arg_info = { { offsetof(struct bpf_iter__task, task), @@ -361,6 +376,8 @@ static const struct bpf_iter_seq_info task_file_seq_info = { static struct bpf_iter_reg task_file_reg_info = { .target = "task_file", .attach_target = bpf_iter_attach_task, + .show_fdinfo = bpf_iter_task_show_fdinfo, + .fill_link_info = bpf_iter_task_fill_link_info, .ctx_arg_info_size = 2, .ctx_arg_info = { { offsetof(struct bpf_iter__task_file, task), diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index af5c600bf673..595bdc4c9431 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4259,6 +4259,11 @@ struct bpf_link_info { struct { __u32 map_id; } map; + + struct { + __u32 main_thread_only:1; + __u32 :31; + } task; }; } iter; struct { From patchwork Thu Aug 27 00:06:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 261873 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8457CC433DF for ; Thu, 27 Aug 2020 00:06:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5CD5820825 for ; Thu, 27 Aug 2020 00:06:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="J/um0pRd" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726929AbgH0AGh (ORCPT ); Wed, 26 Aug 2020 20:06:37 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:44476 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726880AbgH0AGd (ORCPT ); Wed, 26 Aug 2020 20:06:33 -0400 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 07R04lkH002596 for ; Wed, 26 Aug 2020 17:06:32 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=QKD71FgRdrV63rw8OMTz4tR+lNYd7sHK+PSEKlPqm18=; b=J/um0pRdurVRiSKk9WV6YNDlmTpTtqH+0F/VkgALRBLcboFPHmjFftasrqIs7+QXRIGd Q5JMiLYY6E/7vjqG8+UF56fakO1Jhu93x/q5Qk1n6SlihvFq8DMwkPW6Towi0AlU6lP5 y024PNSd0HrT1FyVpCI2nZJGWG3xzHTLkWw= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 335up82a3v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 26 Aug 2020 17:06:31 -0700 Received: from intmgw002.08.frc2.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:21d::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Wed, 26 Aug 2020 17:06:30 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id 0C7C037052E0; Wed, 26 Aug 2020 17:06:23 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Yonghong Song Smtp-Origin-Hostname: devbig003.ftw2.facebook.com To: , CC: Alexei Starovoitov , Daniel Borkmann , Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf-next 4/5] bpftool: support optional 'task main_thread_only' argument Date: Wed, 26 Aug 2020 17:06:22 -0700 Message-ID: <20200827000622.2712178-1-yhs@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200827000618.2711826-1-yhs@fb.com> References: <20200827000618.2711826-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235, 18.0.687 definitions=2020-08-26_14:2020-08-26,2020-08-26 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 bulkscore=0 suspectscore=8 adultscore=0 mlxlogscore=999 spamscore=0 impostorscore=0 clxscore=1015 phishscore=0 priorityscore=1501 mlxscore=0 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2008260187 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org For task and task_file bpf iterators, optional 'task main_thread_only' can signal the kernel to only iterate through main threads of each process. link_query will also print out main_thread_only value for task/task_file iterators. This patch also fixed the issue where if the additional arguments are not supported, bpftool will print an error message and exit. $ ./bpftool iter pin ./bpf_iter_task.o /sys/fs/bpf/p1 task main_thread_only $ ./bpftool iter pin ./bpf_iter_task_file.o /sys/fs/bpf/p2 task main_thread_only $ ./bpftool iter pin ./bpf_iter_task_file.o /sys/fs/bpf/p3 $ ./bpftool link show 1: iter prog 6 target_name bpf_map 2: iter prog 7 target_name bpf_prog 3: iter prog 12 target_name task main_thread_only 1 5: iter prog 23 target_name task_file main_thread_only 1 6: iter prog 28 target_name task_file main_thread_only 0 $ cat /sys/fs/bpf/p2 tgid gid fd file ... 1716 1716 255 ffffffffa2e95ec0 1756 1756 0 ffffffffa2e95ec0 1756 1756 1 ffffffffa2e95ec0 1756 1756 2 ffffffffa2e95ec0 1756 1756 3 ffffffffa2e20a80 1756 1756 4 ffffffffa2e19ba0 1756 1756 5 ffffffffa2e16460 1756 1756 6 ffffffffa2e16460 1756 1756 7 ffffffffa2e16460 1756 1756 8 ffffffffa2e16260 1761 1761 0 ffffffffa2e95ec0 ... $ ls /proc/1756/task/ 1756 1757 1758 1759 1760 In the above task_file iterator, the process with id 1756 has 5 threads and only the thread with pid = 1756 is processed by the bpf program. Signed-off-by: Yonghong Song --- .../bpftool/Documentation/bpftool-iter.rst | 17 +++++++++-- tools/bpf/bpftool/bash-completion/bpftool | 9 +++++- tools/bpf/bpftool/iter.c | 28 ++++++++++++++++--- tools/bpf/bpftool/link.c | 12 ++++++++ 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/tools/bpf/bpftool/Documentation/bpftool-iter.rst b/tools/bpf/bpftool/Documentation/bpftool-iter.rst index 070ffacb42b5..d9aac12c76da 100644 --- a/tools/bpf/bpftool/Documentation/bpftool-iter.rst +++ b/tools/bpf/bpftool/Documentation/bpftool-iter.rst @@ -17,15 +17,16 @@ SYNOPSIS ITER COMMANDS =================== -| **bpftool** **iter pin** *OBJ* *PATH* [**map** *MAP*] +| **bpftool** **iter pin** *OBJ* *PATH* [**map** *MAP* | **task** *TASK_OPT*] | **bpftool** **iter help** | | *OBJ* := /a/file/of/bpf_iter_target.o | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* } +| *TASK_OPT* := { **main_thread_only** } DESCRIPTION =========== - **bpftool iter pin** *OBJ* *PATH* [**map** *MAP*] + **bpftool iter pin** *OBJ* *PATH* [**map** *MAP* | **task** *TASK_OPT*] A bpf iterator combines a kernel iterating of particular kernel data (e.g., tasks, bpf_maps, etc.) and a bpf program called for each kernel data object @@ -44,6 +45,11 @@ DESCRIPTION with each map element, do checking, filtering, aggregation, etc. without copying data to user space. + The task or task_file bpf iterator can have an optional + parameter *TASK_OPT*. The current supported value is + **main_thread_only** which supports to iterate only main + threads of each process. + User can then *cat PATH* to see the bpf iterator output. **bpftool iter help** @@ -78,6 +84,13 @@ EXAMPLES Create a file-based bpf iterator from bpf_iter_hashmap.o and map with id 20, and pin it to /sys/fs/bpf/my_hashmap +**# bpftool iter pin bpf_iter_task.o /sys/fs/bpf/my_task task main_thread_only** + +:: + + Create a file-based bpf iterator from bpf_iter_task.o which iterates main + threads of processes only, and pin it to /sys/fs/bpf/my_hashmap + SEE ALSO ======== **bpf**\ (2), diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool index 7b68e3c0a5fb..84d538de71e1 100644 --- a/tools/bpf/bpftool/bash-completion/bpftool +++ b/tools/bpf/bpftool/bash-completion/bpftool @@ -613,6 +613,7 @@ _bpftool() esac ;; iter) + local TARGET_TYPE='map task' case $command in pin) case $prev in @@ -628,9 +629,15 @@ _bpftool() pinned) _filedir ;; - *) + task) + _bpftool_one_of_list 'main_thread_only' + ;; + map) _bpftool_one_of_list $MAP_TYPE ;; + *) + _bpftool_one_of_list $TARGET_TYPE + ;; esac return 0 ;; diff --git a/tools/bpf/bpftool/iter.c b/tools/bpf/bpftool/iter.c index 3b1aad7535dd..a4c789ea43f1 100644 --- a/tools/bpf/bpftool/iter.c +++ b/tools/bpf/bpftool/iter.c @@ -26,6 +26,7 @@ static int do_pin(int argc, char **argv) /* optional arguments */ if (argc) { + memset(&linfo, 0, sizeof(linfo)); if (is_prefix(*argv, "map")) { NEXT_ARG(); @@ -38,11 +39,29 @@ static int do_pin(int argc, char **argv) if (map_fd < 0) return -1; - memset(&linfo, 0, sizeof(linfo)); linfo.map.map_fd = map_fd; - iter_opts.link_info = &linfo; - iter_opts.link_info_len = sizeof(linfo); + } else if (is_prefix(*argv, "task")) { + NEXT_ARG(); + + if (!REQ_ARGS(1)) { + p_err("incorrect task spec"); + return -1; + } + + if (strcmp(*argv, "main_thread_only") != 0) { + p_err("incorrect task spec"); + return -1; + } + + linfo.task.main_thread_only = true; + } else { + p_err("expected no more arguments, 'map' or 'task', got: '%s'?", + *argv); + return -1; } + + iter_opts.link_info = &linfo; + iter_opts.link_info_len = sizeof(linfo); } obj = bpf_object__open(objfile); @@ -95,9 +114,10 @@ static int do_pin(int argc, char **argv) static int do_help(int argc, char **argv) { fprintf(stderr, - "Usage: %1$s %2$s pin OBJ PATH [map MAP]\n" + "Usage: %1$s %2$s pin OBJ PATH [map MAP | task TASK_OPT]\n" " %1$s %2$s help\n" " " HELP_SPEC_MAP "\n" + " TASK_OPT := { main_thread_only }\n" "", bin_name, "iter"); diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index e77e1525d20a..a159d5680c74 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -83,6 +83,12 @@ static bool is_iter_map_target(const char *target_name) strcmp(target_name, "bpf_sk_storage_map") == 0; } +static bool is_iter_task_target(const char *target_name) +{ + return strcmp(target_name, "task") == 0 || + strcmp(target_name, "task_file") == 0; +} + static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr) { const char *target_name = u64_to_ptr(info->iter.target_name); @@ -91,6 +97,9 @@ static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr) if (is_iter_map_target(target_name)) jsonw_uint_field(wtr, "map_id", info->iter.map.map_id); + else if (is_iter_task_target(target_name)) + jsonw_uint_field(wtr, "main_thread_only", + info->iter.task.main_thread_only); } static int get_prog_info(int prog_id, struct bpf_prog_info *info) @@ -202,6 +211,9 @@ static void show_iter_plain(struct bpf_link_info *info) if (is_iter_map_target(target_name)) printf("map_id %u ", info->iter.map.map_id); + else if (is_iter_task_target(target_name)) + printf("main_thread_only %u ", + info->iter.task.main_thread_only); } static int show_link_close_plain(int fd, struct bpf_link_info *info)