From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 629D0C6FD19 for ; Fri, 10 Mar 2023 04:39:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229614AbjCJEjj (ORCPT ); Thu, 9 Mar 2023 23:39:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229628AbjCJEjh (ORCPT ); Thu, 9 Mar 2023 23:39:37 -0500 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 884DD10111E for ; Thu, 9 Mar 2023 20:39:36 -0800 (PST) Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 329LqwhD025226 for ; Thu, 9 Mar 2023 20:39:36 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=s2048-2021-q4; bh=bLaBpB3ICw2koLk1Zlaoyy+AIJQaF6X/LxEMLl6f3bg=; b=guF7jxBEw4IuMoT/HPS2UDjDYIRfq/Xo0QOJW2bsea60pC3FqxIqvK1p7vHztLgmhg3V NX7jXNKL41mR+x9Wd5hCiRwr63dutxolT2ah82wx9AqH1NZ74qww4jYZC0fqnJnpG0fd Re32sbTcK29wywAKtTe2+hePIUWYY0u1aOHlsZqCvoCmCjPgDUndpcPDaTTAilvnQ3uI NaNCzOAi1mA37QHoGTJcUaiZRmY/s7YAAdi2N4Y9xyrk04sGY7FXbwmtCSwUJa5K3DWM +CvZHWQDMxCRQ8rzdgXHrAQckYO1c/ebSx1g4knzg4EZ8O5l5Zl9zkHoABcbHi4BL/i4 zw== Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3p783m09j6-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 09 Mar 2023 20:39:36 -0800 Received: from twshared52565.14.frc2.facebook.com (2620:10d:c085:108::8) by mail.thefacebook.com (2620:10d:c085:11d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.17; Thu, 9 Mar 2023 20:39:35 -0800 Received: by devbig931.frc1.facebook.com (Postfix, from userid 460691) id BD5626F6C2B7; Thu, 9 Mar 2023 20:39:23 -0800 (PST) From: Kui-Feng Lee To: , , , , , , CC: Kui-Feng Lee Subject: [PATCH bpf-next v6 4/8] libbpf: Create a bpf_link in bpf_map__attach_struct_ops(). Date: Thu, 9 Mar 2023 20:38:09 -0800 Message-ID: <20230310043812.3087672-5-kuifeng@meta.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230310043812.3087672-1-kuifeng@meta.com> References: <20230310043812.3087672-1-kuifeng@meta.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: aLHvMuVshuEI0yCV24BH3VPKZYYSirt9 X-Proofpoint-ORIG-GUID: aLHvMuVshuEI0yCV24BH3VPKZYYSirt9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-10_02,2023-03-09_01,2023-02-09_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org bpf_map__attach_struct_ops() was creating a dummy bpf_link as a placeholder, but now it is constructing an authentic one by calling bpf_link_create() if the map has the BPF_F_LINK flag. You can flag a struct_ops map with BPF_F_LINK by calling bpf_map__set_map_flags(). Signed-off-by: Kui-Feng Lee --- tools/lib/bpf/libbpf.c | 90 +++++++++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 24 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index a557718401e4..6dbae7ffab48 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -116,6 +116,7 @@ static const char * const attach_type_name[] =3D { [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] =3D "sk_reuseport_select_or_migrat= e", [BPF_PERF_EVENT] =3D "perf_event", [BPF_TRACE_KPROBE_MULTI] =3D "trace_kprobe_multi", + [BPF_STRUCT_OPS] =3D "struct_ops", }; =20 static const char * const link_type_name[] =3D { @@ -7677,6 +7678,37 @@ static int bpf_object__resolve_externs(struct bpf_= object *obj, return 0; } =20 +static void bpf_map_prepare_vdata(const struct bpf_map *map) +{ + struct bpf_struct_ops *st_ops; + __u32 i; + + st_ops =3D map->st_ops; + for (i =3D 0; i < btf_vlen(st_ops->type); i++) { + struct bpf_program *prog =3D st_ops->progs[i]; + void *kern_data; + int prog_fd; + + if (!prog) + continue; + + prog_fd =3D bpf_program__fd(prog); + kern_data =3D st_ops->kern_vdata + st_ops->kern_func_off[i]; + *(unsigned long *)kern_data =3D prog_fd; + } +} + +static int bpf_object_prepare_struct_ops(struct bpf_object *obj) +{ + int i; + + for (i =3D 0; i < obj->nr_maps; i++) + if (bpf_map__is_struct_ops(&obj->maps[i])) + bpf_map_prepare_vdata(&obj->maps[i]); + + return 0; +} + static int bpf_object_load(struct bpf_object *obj, int extra_log_level, = const char *target_btf_path) { int err, i; @@ -7702,6 +7734,7 @@ static int bpf_object_load(struct bpf_object *obj, = int extra_log_level, const ch err =3D err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : targ= et_btf_path); err =3D err ? : bpf_object__load_progs(obj, extra_log_level); err =3D err ? : bpf_object_init_prog_arrays(obj); + err =3D err ? : bpf_object_prepare_struct_ops(obj); =20 if (obj->gen_loader) { /* reset FDs */ @@ -11566,22 +11599,30 @@ struct bpf_link *bpf_program__attach(const stru= ct bpf_program *prog) return link; } =20 +struct bpf_link_struct_ops { + struct bpf_link link; + int map_fd; +}; + static int bpf_link__detach_struct_ops(struct bpf_link *link) { + struct bpf_link_struct_ops *st_link; __u32 zero =3D 0; =20 - if (bpf_map_delete_elem(link->fd, &zero)) - return -errno; + st_link =3D container_of(link, struct bpf_link_struct_ops, link); =20 - return 0; + if (st_link->map_fd < 0) + /* w/o a real link */ + return bpf_map_delete_elem(link->fd, &zero); + + return close(link->fd); } =20 struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map) { - struct bpf_struct_ops *st_ops; - struct bpf_link *link; - __u32 i, zero =3D 0; - int err; + struct bpf_link_struct_ops *link; + __u32 zero =3D 0; + int err, fd; =20 if (!bpf_map__is_struct_ops(map) || map->fd =3D=3D -1) return libbpf_err_ptr(-EINVAL); @@ -11590,31 +11631,32 @@ struct bpf_link *bpf_map__attach_struct_ops(con= st struct bpf_map *map) if (!link) return libbpf_err_ptr(-EINVAL); =20 - st_ops =3D map->st_ops; - for (i =3D 0; i < btf_vlen(st_ops->type); i++) { - struct bpf_program *prog =3D st_ops->progs[i]; - void *kern_data; - int prog_fd; + /* kern_vdata should be prepared during the loading phase. */ + err =3D bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0)= ; + if (err) { + free(link); + return libbpf_err_ptr(err); + } =20 - if (!prog) - continue; + link->link.detach =3D bpf_link__detach_struct_ops; =20 - prog_fd =3D bpf_program__fd(prog); - kern_data =3D st_ops->kern_vdata + st_ops->kern_func_off[i]; - *(unsigned long *)kern_data =3D prog_fd; + if (!(map->def.map_flags & BPF_F_LINK)) { + /* w/o a real link */ + link->link.fd =3D map->fd; + link->map_fd =3D -1; + return &link->link; } =20 - err =3D bpf_map_update_elem(map->fd, &zero, st_ops->kern_vdata, 0); - if (err) { - err =3D -errno; + fd =3D bpf_link_create(map->fd, -1, BPF_STRUCT_OPS, NULL); + if (fd < 0) { free(link); - return libbpf_err_ptr(err); + return libbpf_err_ptr(fd); } =20 - link->detach =3D bpf_link__detach_struct_ops; - link->fd =3D map->fd; + link->link.fd =3D fd; + link->map_fd =3D map->fd; =20 - return link; + return &link->link; } =20 typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(struct perf_ev= ent_header *hdr, --=20 2.34.1