From patchwork Fri Dec 4 18:48:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 11952153 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=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,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 91CF8C4361B for ; Fri, 4 Dec 2020 18:53:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 34B3822CB1 for ; Fri, 4 Dec 2020 18:53:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730416AbgLDSw0 (ORCPT ); Fri, 4 Dec 2020 13:52:26 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:55930 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727997AbgLDSw0 (ORCPT ); Fri, 4 Dec 2020 13:52:26 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0B4Ie35p085149; Fri, 4 Dec 2020 18:50:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=qKvf4lzEo5tNiBSD+ta9N0URRH+/apH1S2WO9655a9k=; b=mhrID1qX18m6dww1exE6wPNTGZ74zSHcGO0t/+YT+rn9U/dRLYsOsXsJhtC53PztlUk1 YPM3WZVMFIHWGMRzLgb77YnvtF1uaViSucME3JudNON5lvgd850Kb4rReWCAdilSjm4h es/SygkHshlhUkGLMSgL2JAM5VA0z5U7PTBEoFfANPSVK/Op/+gqa8DQbb1hEDQucidG +hTNRe2QTy5Hkd31LEkrKGg+UND/cy+VHEjtkWtCPiSMUUaZK2SB3Ce9Bx0p1udD/tmY Jwdd7WmTiGSdqIN5eNg+eO4K9laOSEmTTrykweFq4sqQrRy70J/lNBovmuhqM1xzyk4o ug== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2130.oracle.com with ESMTP id 353dyr4pry-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 04 Dec 2020 18:50:57 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0B4IaIgY044683; Fri, 4 Dec 2020 18:48:56 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserp3020.oracle.com with ESMTP id 3540f3pntt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 04 Dec 2020 18:48:56 +0000 Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 0B4ImtZC006809; Fri, 4 Dec 2020 18:48:55 GMT Received: from localhost.uk.oracle.com (/10.175.205.186) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 04 Dec 2020 10:48:55 -0800 From: Alan Maguire To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org Cc: kafai@fb.com, yhs@fb.com, songliubraving@fb.com, john.fastabend@gmail.com, kpsingh@chromium.org, rostedt@goodmis.org, mingo@redhat.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, shuah@kernel.org, lmb@cloudflare.com, linux-kselftest@vger.kernel.org, Alan Maguire Subject: [PATCH v2 bpf-next 1/3] bpf: eliminate btf_module_mutex as RCU synchronization can be used Date: Fri, 4 Dec 2020 18:48:34 +0000 Message-Id: <1607107716-14135-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1607107716-14135-1-git-send-email-alan.maguire@oracle.com> References: <1607107716-14135-1-git-send-email-alan.maguire@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9825 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 malwarescore=0 phishscore=0 suspectscore=2 bulkscore=0 spamscore=0 adultscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012040106 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9825 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 bulkscore=0 clxscore=1015 mlxscore=0 spamscore=0 priorityscore=1501 mlxlogscore=999 suspectscore=2 lowpriorityscore=0 phishscore=0 adultscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012040106 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org btf_module_mutex is used when manipulating the BTF module list. However we will wish to look up this list from BPF program context, and such contexts can include interrupt state where we cannot sleep due to a mutex_lock(). RCU usage here conforms quite closely to the example in the system call auditing example in Documentation/RCU/listRCU.rst ; and as such we can eliminate the lock and use list_del_rcu()/call_rcu() on module removal, and list_add_rcu() for module addition. Signed-off-by: Alan Maguire --- kernel/bpf/btf.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 8d6bdb4..333f41c 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5758,13 +5758,13 @@ bool btf_id_set_contains(const struct btf_id_set *set, u32 id) #ifdef CONFIG_DEBUG_INFO_BTF_MODULES struct btf_module { struct list_head list; + struct rcu_head rcu; struct module *module; struct btf *btf; struct bin_attribute *sysfs_attr; }; static LIST_HEAD(btf_modules); -static DEFINE_MUTEX(btf_module_mutex); static ssize_t btf_module_read(struct file *file, struct kobject *kobj, @@ -5777,10 +5777,21 @@ struct btf_module { return len; } +static void btf_module_free(struct rcu_head *rcu) +{ + struct btf_module *btf_mod = container_of(rcu, struct btf_module, rcu); + + if (btf_mod->sysfs_attr) + sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr); + btf_put(btf_mod->btf); + kfree(btf_mod->sysfs_attr); + kfree(btf_mod); +} + static int btf_module_notify(struct notifier_block *nb, unsigned long op, void *module) { - struct btf_module *btf_mod, *tmp; + struct btf_module *btf_mod; struct module *mod = module; struct btf *btf; int err = 0; @@ -5811,11 +5822,9 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op, goto out; } - mutex_lock(&btf_module_mutex); btf_mod->module = module; btf_mod->btf = btf; - list_add(&btf_mod->list, &btf_modules); - mutex_unlock(&btf_module_mutex); + list_add_rcu(&btf_mod->list, &btf_modules); if (IS_ENABLED(CONFIG_SYSFS)) { struct bin_attribute *attr; @@ -5845,20 +5854,14 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op, break; case MODULE_STATE_GOING: - mutex_lock(&btf_module_mutex); - list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { + list_for_each_entry(btf_mod, &btf_modules, list) { if (btf_mod->module != module) continue; - list_del(&btf_mod->list); - if (btf_mod->sysfs_attr) - sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr); - btf_put(btf_mod->btf); - kfree(btf_mod->sysfs_attr); - kfree(btf_mod); + list_del_rcu(&btf_mod->list); + call_rcu(&btf_mod->rcu, btf_module_free); break; } - mutex_unlock(&btf_module_mutex); break; } out: From patchwork Fri Dec 4 18:48:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 11952149 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=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,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 6F76BC4361B for ; Fri, 4 Dec 2020 18:51:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3009022CA0 for ; Fri, 4 Dec 2020 18:51:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730209AbgLDSuc (ORCPT ); Fri, 4 Dec 2020 13:50:32 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:59844 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726152AbgLDSu3 (ORCPT ); Fri, 4 Dec 2020 13:50:29 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0B4Id5qq099390; Fri, 4 Dec 2020 18:49:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=/93N649h23mFPKdE9O8nGlSxlC3KmuSOrZrM93ncRgk=; b=lTLbUAcn004c7Aw/JvIbnaKTaJKaimdgYMxppp2j7O9jEILmNcgGS/ZI/W8ZbPsscpo7 +kXa1ZtjDKh+NW9vYwV89Jt8SxFTHO/wafKti0AUKiuK+PaWEMkqF8BeubI1b4wgwMKo lrvda/I1NTCXf+roG96Df38F7gdlMgL55d7PHRLAK1lXsUFfJQRktJ4Z6esC6sCeWgSx QTX3zAs1G10bXZS5DsSghQqdv6T6qkAfXviSgBKeydmO1Dn75f6WcvS8+gx3O9IHxwn7 rlpWo4ViGdAkIz1AHiGRoq2/vzKs0sH4HD30bsZCg5SYC60loF8UeAbXekIHiI1Et9Am Cg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by aserp2120.oracle.com with ESMTP id 353egm4ktq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 04 Dec 2020 18:49:02 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0B4IYROo082082; Fri, 4 Dec 2020 18:49:01 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userp3020.oracle.com with ESMTP id 3540ayfr5x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 04 Dec 2020 18:49:01 +0000 Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 0B4In02C006822; Fri, 4 Dec 2020 18:49:00 GMT Received: from localhost.uk.oracle.com (/10.175.205.186) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 04 Dec 2020 10:49:00 -0800 From: Alan Maguire To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org Cc: kafai@fb.com, yhs@fb.com, songliubraving@fb.com, john.fastabend@gmail.com, kpsingh@chromium.org, rostedt@goodmis.org, mingo@redhat.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, shuah@kernel.org, lmb@cloudflare.com, linux-kselftest@vger.kernel.org, Alan Maguire Subject: [PATCH v2 bpf-next 2/3] bpf: add module support to btf display helpers Date: Fri, 4 Dec 2020 18:48:35 +0000 Message-Id: <1607107716-14135-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1607107716-14135-1-git-send-email-alan.maguire@oracle.com> References: <1607107716-14135-1-git-send-email-alan.maguire@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9825 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 bulkscore=0 phishscore=0 mlxscore=0 adultscore=0 malwarescore=0 suspectscore=2 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012040106 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9825 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 bulkscore=0 suspectscore=2 phishscore=0 mlxlogscore=999 lowpriorityscore=0 malwarescore=0 priorityscore=1501 spamscore=0 impostorscore=0 clxscore=1015 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012040106 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org bpf_snprintf_btf and bpf_seq_printf_btf use a "struct btf_ptr *" argument that specifies type information about the type to be displayed. Augment this information to include an object id. If this id is 0, the assumption is that it refers to a core kernel type from vmlinux; otherwise the object id specifies the module the type is in, or if no such id is found in the module list, we fall back to vmlinux. Signed-off-by: Alan Maguire --- include/linux/btf.h | 12 ++++++++++++ include/uapi/linux/bpf.h | 13 +++++++------ kernel/bpf/btf.c | 18 +++++++++++++++++ kernel/trace/bpf_trace.c | 44 +++++++++++++++++++++++++++++++----------- tools/include/uapi/linux/bpf.h | 13 +++++++------ 5 files changed, 77 insertions(+), 23 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 4c200f5..688786a 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -214,6 +214,14 @@ static inline const struct btf_var_secinfo *btf_type_var_secinfo( const char *btf_name_by_offset(const struct btf *btf, u32 offset); struct btf *btf_parse_vmlinux(void); struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +struct btf *bpf_get_btf_module(__u32 obj_id); +#else +static inline struct btf *bpf_get_btf_module(__u32 obj_id) +{ + return ERR_PTR(-ENOTSUPP); +} +#endif #else static inline const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id) @@ -225,6 +233,10 @@ static inline const char *btf_name_by_offset(const struct btf *btf, { return NULL; } +static inline struct btf *bpf_get_btf_module(__u32 obj_id) +{ + return ERR_PTR(-ENOTSUPP); +} #endif #endif diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 1233f14..ccb75299 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -3641,7 +3641,9 @@ struct bpf_stack_build_id { * the pointer data is carried out to avoid kernel crashes during * operation. Smaller types can use string space on the stack; * larger programs can use map data to store the string - * representation. + * representation. Module-specific data structures can be + * displayed if the module BTF object id is supplied in the + * *ptr*->obj_id field. * * The string can be subsequently shared with userspace via * bpf_perf_event_output() or ring buffer interfaces. @@ -5115,15 +5117,14 @@ struct bpf_sk_lookup { /* * struct btf_ptr is used for typed pointer representation; the * type id is used to render the pointer data as the appropriate type - * via the bpf_snprintf_btf() helper described above. A flags field - - * potentially to specify additional details about the BTF pointer - * (rather than its mode of display) - is included for future use. - * Display flags - BTF_F_* - are passed to bpf_snprintf_btf separately. + * via the bpf_snprintf_btf() helper described above. The obj_id + * is used to specify an object id (such as a module); if unset + * a core vmlinux type id is assumed. */ struct btf_ptr { void *ptr; __u32 type_id; - __u32 flags; /* BTF ptr flags; unused at present. */ + __u32 obj_id; /* BTF object; vmlinux if 0 */ }; /* diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 333f41c..8ee691e 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5777,6 +5777,24 @@ struct btf_module { return len; } +struct btf *bpf_get_btf_module(__u32 obj_id) +{ + struct btf *btf = ERR_PTR(-ENOENT); + struct btf_module *btf_mod; + + rcu_read_lock(); + list_for_each_entry_rcu(btf_mod, &btf_modules, list) { + if (!btf_mod->btf || obj_id != btf_mod->btf->id) + continue; + + refcount_inc(&btf_mod->btf->refcnt); + btf = btf_mod->btf; + break; + } + rcu_read_unlock(); + return btf; +} + static void btf_module_free(struct rcu_head *rcu) { struct btf_module *btf_mod = container_of(rcu, struct btf_module, rcu); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 23a390a..66d4120 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -75,8 +75,8 @@ static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, - u64 flags, const struct btf **btf, - s32 *btf_id); + u64 flags, struct btf **btf, + bool *btf_is_vmlinux, s32 *btf_id); /** * trace_call_bpf - invoke BPF program @@ -786,15 +786,22 @@ struct bpf_seq_printf_buf { BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr, u32, btf_ptr_size, u64, flags) { - const struct btf *btf; + bool btf_is_vmlinux; + struct btf *btf; s32 btf_id; int ret; - ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); + ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, + &btf_is_vmlinux, &btf_id); if (ret) return ret; - return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags); + ret = btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags); + /* modules refcount their BTF */ + if (!btf_is_vmlinux) + btf_put(btf); + + return ret; } static const struct bpf_func_proto bpf_seq_printf_btf_proto = { @@ -1205,7 +1212,8 @@ static bool bpf_d_path_allowed(const struct bpf_prog *prog) BTF_F_PTR_RAW | BTF_F_ZERO) static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, - u64 flags, const struct btf **btf, + u64 flags, struct btf **btf, + bool *btf_is_vmlinux, s32 *btf_id) { const struct btf_type *t; @@ -1216,7 +1224,14 @@ static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, if (btf_ptr_size != sizeof(struct btf_ptr)) return -EINVAL; - *btf = bpf_get_btf_vmlinux(); + *btf_is_vmlinux = false; + + if (ptr->obj_id > 0) + *btf = bpf_get_btf_module(ptr->obj_id); + if (ptr->obj_id == 0 || IS_ERR(*btf)) { + *btf = bpf_get_btf_vmlinux(); + *btf_is_vmlinux = true; + } if (IS_ERR_OR_NULL(*btf)) return PTR_ERR(*btf); @@ -1237,16 +1252,23 @@ static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr, u32, btf_ptr_size, u64, flags) { - const struct btf *btf; + bool btf_is_vmlinux; + struct btf *btf; s32 btf_id; int ret; - ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); + ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, + &btf_is_vmlinux, &btf_id); if (ret) return ret; - return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size, - flags); + ret = btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size, + flags); + /* modules refcount their BTF */ + if (!btf_is_vmlinux) + btf_put(btf); + + return ret; } const struct bpf_func_proto bpf_snprintf_btf_proto = { diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 1233f14..ccb75299 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -3641,7 +3641,9 @@ struct bpf_stack_build_id { * the pointer data is carried out to avoid kernel crashes during * operation. Smaller types can use string space on the stack; * larger programs can use map data to store the string - * representation. + * representation. Module-specific data structures can be + * displayed if the module BTF object id is supplied in the + * *ptr*->obj_id field. * * The string can be subsequently shared with userspace via * bpf_perf_event_output() or ring buffer interfaces. @@ -5115,15 +5117,14 @@ struct bpf_sk_lookup { /* * struct btf_ptr is used for typed pointer representation; the * type id is used to render the pointer data as the appropriate type - * via the bpf_snprintf_btf() helper described above. A flags field - - * potentially to specify additional details about the BTF pointer - * (rather than its mode of display) - is included for future use. - * Display flags - BTF_F_* - are passed to bpf_snprintf_btf separately. + * via the bpf_snprintf_btf() helper described above. The obj_id + * is used to specify an object id (such as a module); if unset + * a core vmlinux type id is assumed. */ struct btf_ptr { void *ptr; __u32 type_id; - __u32 flags; /* BTF ptr flags; unused at present. */ + __u32 obj_id; /* BTF object; vmlinux if 0 */ }; /* From patchwork Fri Dec 4 18:48:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 11952151 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=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,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 27F4EC2BB3F for ; Fri, 4 Dec 2020 18:51:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DF28222CA0 for ; Fri, 4 Dec 2020 18:51:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730324AbgLDSuf (ORCPT ); Fri, 4 Dec 2020 13:50:35 -0500 Received: from aserp2130.oracle.com ([141.146.126.79]:46896 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730277AbgLDSue (ORCPT ); Fri, 4 Dec 2020 13:50:34 -0500 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0B4IdRT6080674; Fri, 4 Dec 2020 18:49:06 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=LodmyqwDjSlhRwAL2+FQ9VMhHLbsi9nNb2DqAz9Jwck=; b=QsnK8LlyaOdGMqWdxHVY4obElKLM6cakDDAQplT+ZM5ZBgnjoha/JODZwqEYsULNp1Td diMR/jGPXf71djv6WdV1P81KLaVU/yui8DFy9C7ZToU2IH+6OjvtrsPCY1rpQJFuZXDI guZGVy8GbhwufwcJL7wmw78EgiFCaFlPXqCTg+y5Dx34PcZjmuFqMehi6oT3/Ra9y8EG T7qP7JmgS7B8O5ajWqaReMNA1zfWcwJ9Hv3wknFqySaekYa4CAx2pEAHyBpqpLQRY6PG Z6jiDoDSFLu6d4bPsWHHUfNNHaBNQ3nSwcI4wBN1Hj6ol1rjy+6YFbI89FBN+6uA6dAn Gw== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by aserp2130.oracle.com with ESMTP id 353c2bcpvw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 04 Dec 2020 18:49:06 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0B4IaGEk044262; Fri, 4 Dec 2020 18:49:06 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserp3020.oracle.com with ESMTP id 3540f3pnxf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 04 Dec 2020 18:49:06 +0000 Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 0B4In5vA025494; Fri, 4 Dec 2020 18:49:05 GMT Received: from localhost.uk.oracle.com (/10.175.205.186) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 04 Dec 2020 10:49:04 -0800 From: Alan Maguire To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org Cc: kafai@fb.com, yhs@fb.com, songliubraving@fb.com, john.fastabend@gmail.com, kpsingh@chromium.org, rostedt@goodmis.org, mingo@redhat.com, haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, shuah@kernel.org, lmb@cloudflare.com, linux-kselftest@vger.kernel.org, Alan Maguire Subject: [PATCH v2 bpf-next 3/3] selftests/bpf: verify module-specific types can be shown via bpf_snprintf_btf Date: Fri, 4 Dec 2020 18:48:36 +0000 Message-Id: <1607107716-14135-4-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1607107716-14135-1-git-send-email-alan.maguire@oracle.com> References: <1607107716-14135-1-git-send-email-alan.maguire@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9825 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 malwarescore=0 phishscore=0 suspectscore=0 bulkscore=0 spamscore=0 adultscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012040106 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9825 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 lowpriorityscore=0 clxscore=1015 bulkscore=0 mlxlogscore=999 phishscore=0 malwarescore=0 spamscore=0 adultscore=0 mlxscore=0 priorityscore=1501 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012040106 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Verify that specifying a module object id in "struct btf_ptr *" along with a type id of a module-specific type will succeed. veth_stats_rx() is chosen because its function signature consists of a module-specific type "struct veth_stats" and a kernel-specific one "struct net_device". Currently the tests take the messy approach of determining object and type ids for the relevant module/function; __builtin_btf_type_id() supports object ids by returning a 64-bit value, but need to find a good way to determine if that support is present. Signed-off-by: Alan Maguire --- .../selftests/bpf/prog_tests/snprintf_btf_mod.c | 124 +++++++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 2 +- tools/testing/selftests/bpf/progs/btf_ptr.h | 2 +- tools/testing/selftests/bpf/progs/veth_stats_rx.c | 72 ++++++++++++ 4 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/snprintf_btf_mod.c create mode 100644 tools/testing/selftests/bpf/progs/veth_stats_rx.c diff --git a/tools/testing/selftests/bpf/prog_tests/snprintf_btf_mod.c b/tools/testing/selftests/bpf/prog_tests/snprintf_btf_mod.c new file mode 100644 index 0000000..89805d7 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/snprintf_btf_mod.c @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include "veth_stats_rx.skel.h" + +#define VETH_NAME "bpfveth0" + +/* Demonstrate that bpf_snprintf_btf succeeds for both module-specific + * and kernel-defined data structures; veth_stats_rx() is used as + * it has both module-specific and kernel-defined data as arguments. + * This test assumes that veth is built as a module and will skip if not. + */ +void test_snprintf_btf_mod(void) +{ + struct btf *vmlinux_btf = NULL, *veth_btf = NULL; + struct veth_stats_rx *skel = NULL; + struct veth_stats_rx__bss *bss; + int err, duration = 0; + __u32 id; + + err = system("ip link add name " VETH_NAME " type veth"); + if (CHECK(err, "system", "ip link add veth failed: %d\n", err)) + return; + + vmlinux_btf = btf__parse_raw("/sys/kernel/btf/vmlinux"); + err = libbpf_get_error(vmlinux_btf); + if (CHECK(err, "parse vmlinux BTF", "failed parsing vmlinux BTF: %d\n", + err)) + goto cleanup; + veth_btf = btf__parse_raw_split("/sys/kernel/btf/veth", vmlinux_btf); + err = libbpf_get_error(veth_btf); + if (err == -ENOENT) { + printf("%s:SKIP:no BTF info for veth\n", __func__); + test__skip(); + goto cleanup; + } + + if (CHECK(err, "parse veth BTF", "failed parsing veth BTF: %d\n", err)) + goto cleanup; + + skel = veth_stats_rx__open(); + if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) + goto cleanup; + + err = veth_stats_rx__load(skel); + if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err)) + goto cleanup; + + bss = skel->bss; + + /* This could all be replaced by __builtin_btf_type_id(); but need + * a way to determine if it supports object and type id. In the + * meantime, look up type id for veth_stats and object id for veth. + */ + bss->veth_stats_btf_id = btf__find_by_name(veth_btf, "veth_stats"); + + if (CHECK(bss->veth_stats_btf_id <= 0, "find 'struct veth_stats'", + "could not find 'struct veth_stats' in veth BTF: %d", + bss->veth_stats_btf_id)) + goto cleanup; + + bss->veth_obj_id = 0; + + for (id = 1; bpf_btf_get_next_id(id, &id) == 0; ) { + struct bpf_btf_info info; + __u32 len = sizeof(info); + char name[64]; + int fd; + + fd = bpf_btf_get_fd_by_id(id); + if (fd < 0) + continue; + + memset(&info, 0, sizeof(info)); + info.name_len = sizeof(name); + info.name = (__u64)name; + if (bpf_obj_get_info_by_fd(fd, &info, &len) || + strcmp((char *)info.name, "veth") != 0) + continue; + bss->veth_obj_id = info.id; + } + + if (CHECK(bss->veth_obj_id == 0, "get obj id for veth module", + "could not get veth module id")) + goto cleanup; + + err = veth_stats_rx__attach(skel); + if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) + goto cleanup; + + /* generate stats event, then delete; this ensures the program + * triggers prior to reading status. + */ + err = system("ethtool -S " VETH_NAME " > /dev/null"); + if (CHECK(err, "system", "ethtool -S failed: %d\n", err)) + goto cleanup; + + system("ip link delete " VETH_NAME); + + /* Make sure veth_stats_rx program was triggered and it set + * expected return values from bpf_trace_printk()s and all + * tests ran. + */ + if (CHECK(bss->ret <= 0, + "bpf_snprintf_btf: got return value", + "ret <= 0 %ld test %d\n", bss->ret, bss->ran_subtests)) + goto cleanup; + + if (CHECK(bss->ran_subtests == 0, "check if subtests ran", + "no subtests ran, did BPF program run?")) + goto cleanup; + + if (CHECK(bss->num_subtests != bss->ran_subtests, + "check all subtests ran", + "only ran %d of %d tests\n", bss->num_subtests, + bss->ran_subtests)) + goto cleanup; + +cleanup: + system("ip link delete " VETH_NAME ">/dev/null 2>&1"); + if (skel) + veth_stats_rx__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/bpf_iter.h b/tools/testing/selftests/bpf/progs/bpf_iter.h index 6a12554..d6d9838 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter.h +++ b/tools/testing/selftests/bpf/progs/bpf_iter.h @@ -119,7 +119,7 @@ struct bpf_iter__sockmap { struct btf_ptr { void *ptr; __u32 type_id; - __u32 flags; + __u32 obj_id; }; enum { diff --git a/tools/testing/selftests/bpf/progs/btf_ptr.h b/tools/testing/selftests/bpf/progs/btf_ptr.h index c3c9797..4f84034 100644 --- a/tools/testing/selftests/bpf/progs/btf_ptr.h +++ b/tools/testing/selftests/bpf/progs/btf_ptr.h @@ -16,7 +16,7 @@ struct btf_ptr { void *ptr; __u32 type_id; - __u32 flags; + __u32 obj_id; }; enum { diff --git a/tools/testing/selftests/bpf/progs/veth_stats_rx.c b/tools/testing/selftests/bpf/progs/veth_stats_rx.c new file mode 100644 index 0000000..f04fb55 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/veth_stats_rx.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020, Oracle and/or its affiliates. */ + +#include "btf_ptr.h" +#include +#include +#include + +#include + +long ret = 0; +int num_subtests = 0; +int ran_subtests = 0; +s32 veth_stats_btf_id = 0; +s32 veth_obj_id = 0; + +#define STRSIZE 2048 + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#endif + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, char[STRSIZE]); +} strdata SEC(".maps"); + +SEC("kprobe/veth_stats_rx") +int veth_stats_rx(struct pt_regs *ctx) +{ + static __u64 flags[] = { 0, BTF_F_COMPACT, BTF_F_ZERO, BTF_F_PTR_RAW, + BTF_F_NONAME, BTF_F_COMPACT | BTF_F_ZERO | + BTF_F_PTR_RAW | BTF_F_NONAME }; + static struct btf_ptr p = { }; + __u32 btf_ids[] = { 0, 0 }; + __u32 obj_ids[] = { 0, 0 }; + void *ptrs[] = { 0, 0 }; + __u32 key = 0; + int i, j; + char *str; + + btf_ids[0] = veth_stats_btf_id; + obj_ids[0] = veth_obj_id; + ptrs[0] = (void *)PT_REGS_PARM1_CORE(ctx); + + btf_ids[1] = bpf_core_type_id_kernel(struct net_device); + ptrs[1] = (void *)PT_REGS_PARM2_CORE(ctx); + + str = bpf_map_lookup_elem(&strdata, &key); + if (!str) + return 0; + + for (i = 0; i < ARRAY_SIZE(btf_ids); i++) { + p.type_id = btf_ids[i]; + p.obj_id = obj_ids[i]; + p.ptr = ptrs[i]; + for (j = 0; j < ARRAY_SIZE(flags); j++) { + ++num_subtests; + ret = bpf_snprintf_btf(str, STRSIZE, &p, sizeof(p), 0); + if (ret < 0) + bpf_printk("returned %d when writing id %d", + ret, p.type_id); + ++ran_subtests; + } + } + + return 0; +} + +char _license[] SEC("license") = "GPL";