From patchwork Wed Feb 14 02:08:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13555888 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 939D546AB for ; Wed, 14 Feb 2024 02:08:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707876527; cv=none; b=WOyxXlaywFN/uDLkRb7RmX9fLNCo+bwJmAnuHS4Xnkz574s+qDoBXSQas0KZVAKexd70iLtPVj1W79zvWN07wEEK2UW4rj1o0gEG4gZfulpQVkVSOX3WHoxQ9JOZ0NhdJEFBAfsd1vv6rvY6bFBL08jISwSEDbL/tVsdQnL6m2k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707876527; c=relaxed/simple; bh=x3a5FB5lHt8ZFI599gt9isIizhdFOb6pHHipjU8yaIw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jX/Ju7xDUU7AJM/SXUgNTqcs1Acfw4RfCnxiBOM7ATnnn09IoIFuyVp9WuQi0oOt0Z01kPc3tfnUmMiV2TCCHXXuxkt32UMUqJMzd1UQVa1+uA1KOqKhL3KG65KdP4fC7SpDcBQ8Hn+JvkzlkET7vpLv8SX0Qp7FKcqPiezhRx8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LuURInx6; arc=none smtp.client-ip=209.85.219.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LuURInx6" Received: by mail-yb1-f172.google.com with SMTP id 3f1490d57ef6-dcc6fc978ddso322647276.0 for ; Tue, 13 Feb 2024 18:08:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707876524; x=1708481324; darn=vger.kernel.org; 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=PU1Q9FQBIf28oHG2S/fp5/S8tr3I7bEZh/t4Vw3EHlM=; b=LuURInx6TZyyA3Fth2ooN7goglhkjfh7mMAMaZv312h99ryRrqZveglRqbYLuhnHFK gMT5tvoZsCOOoDDnGNcs+HXqAE127cEzuKmNl0U5U7dbsddu5W3qDSmtUDmJ3agMUcpA AEt6iK4gjiSB50dT0QtZba2ZgWrxDmJLdSGwYuzzLAojWiywWjqkXaI64cwGEZYsgQrI P++NmXW5JTdsbDkD0zhIy72zUOS6aA9MgrTMvtCmC23tPLGuaKNtOKbJIVJAEBa8LPOR hUo5VssiFZqyyuNySHX02RpxczEi8PZ8ieeD0kS+FteEHqtdEOjZSMQXHa0TWLJ8RABR R9/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707876524; x=1708481324; 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=PU1Q9FQBIf28oHG2S/fp5/S8tr3I7bEZh/t4Vw3EHlM=; b=UtnEDfhHkw39QY9hMy8sGzA2NhlOZqNt8gS62TfyqDsJ2qosLX5Mxgr9w+APelkgZc WRT7/HzyJO+s9QBPCYiRW/5Rt165MENYndtFN8QAc7ahH79gm8nbZD23sbJIGEh09lsF rwazMLqSsHS1ERAFuGmkAvFMDOlWgHOZXhiZgECXuZt7M7Pjk0Si+Q6Ot2Woi85XQ2o1 rFQ31yCYTPkrZwa5xMUquoWvCKmUae1tCU9qORzk1VDU4dhusSk5+Mxmy4ExJzivXZFK djj1OLbSYlZuJrVo219fRp86va8nmxteH+G4RnMWmC8W0VKEnVgG7ZjuILiD+bZ09Pru jsLw== X-Gm-Message-State: AOJu0Yx5DWM1Ka1Da9SSj9jkr3PpMb/3wzMjVDLwUSpAct+9iBf/1JON yj6mJRtzVdMxeXZVB12VhubQ5gc+Xax1ywdo9yaA6ieVtTuXfe5B3g4dKtLU X-Google-Smtp-Source: AGHT+IFApyQx2bV6BmJ/a3/GEPm52YiS0iFncL8D45AP9Eu1S7KQlyh0JGyoM2vBa1K3P3bNONdZfg== X-Received: by 2002:a25:9f0f:0:b0:dcd:a6ec:7f57 with SMTP id n15-20020a259f0f000000b00dcda6ec7f57mr338303ybq.7.1707876523956; Tue, 13 Feb 2024 18:08:43 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWVnGtMZDQ4Tmz2kehbh+yJ4A7yDWKwEAH+CuCYTUrnpFvdlux9Ize5qJu+D4YXRxKuZGq47ynorczJWmZNvo/Eo66Bn+2Wt39wHKa7JHQnV2wqfqKbHoeLtJUgaJJcJEw2yWHubiGUtJ6Lm8e6xCw9gaPHde5g6o2FsWRW6S2GZnl6eSh8z7q7uv7g+KvChO/97qRnICZzSATjOqEAmDwBQLSUxdfk8I8U3vzTDozoilx1mK+gkA== Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:966f:7edc:e6e6:cd97]) by smtp.gmail.com with ESMTPSA id s17-20020a258311000000b00dc2310abe8bsm1894752ybk.38.2024.02.13.18.08.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 18:08:43 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [RFC bpf-next v2 1/3] libbpf: Create a shadow copy for each struct_ops map if necessary. Date: Tue, 13 Feb 2024 18:08:34 -0800 Message-Id: <20240214020836.1845354-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240214020836.1845354-1-thinker.li@gmail.com> References: <20240214020836.1845354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC From: Kui-Feng Lee If the user has passed a shadow info for a struct_ops map along with struct bpf_object_open_opts, a shadow copy will be created for the map and returned from bpf_map__initial_value(). The user can read and write shadow variables through the shadow copy, which is placed in the struct pointed by skel->struct_ops.FOO, where FOO is the map name. The value of a shadow variable will be used to update the value of the map when loading the map to the kernel. Signed-off-by: Kui-Feng Lee --- tools/lib/bpf/libbpf.c | 195 ++++++++++++++++++++++++++++++-- tools/lib/bpf/libbpf.h | 34 +++++- tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/libbpf_internal.h | 1 + 4 files changed, 220 insertions(+), 11 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 01f407591a92..ce9c4cdb2dc5 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -487,6 +487,14 @@ struct bpf_struct_ops { * from "data". */ void *kern_vdata; + /* Description of the layout that a shadow copy should look like. + */ + const struct bpf_struct_ops_map_info *shadow_info; + /* A shadow copy of the struct_ops data created according to the + * layout described by shadow_info. + */ + void *shadow_data; + __u32 shadow_data_size; __u32 type_id; }; @@ -1027,7 +1035,7 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) struct module_btf *mod_btf; void *data, *kern_data; const char *tname; - int err; + int err, j; st_ops = map->st_ops; type = st_ops->type; @@ -1083,9 +1091,18 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) } moff = member->offset / 8; - kern_moff = kern_member->offset / 8; - mdata = data + moff; + if (st_ops->shadow_data) { + for (j = 0; j < st_ops->shadow_info->cnt; j++) { + if (strcmp(mname, st_ops->shadow_info->members[j].name)) + continue; + moff = st_ops->shadow_info->members[j].offset; + mdata = st_ops->shadow_data + moff; + break; + } + } + + kern_moff = kern_member->offset / 8; kern_mdata = kern_data + kern_moff; mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); @@ -1102,6 +1119,9 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) if (btf_is_ptr(mtype)) { struct bpf_program *prog; + if (st_ops->shadow_data) + st_ops->progs[i] = + *(struct bpf_program **)mdata; prog = st_ops->progs[i]; if (!prog) continue; @@ -1172,8 +1192,108 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) return 0; } +static int init_struct_ops_shadow(struct bpf_map *map, + const struct btf_type *t) +{ + struct btf *btf = map->obj->btf; + struct bpf_struct_ops *st_ops = map->st_ops; + const struct btf_member *m; + const struct btf_type *mt; + const struct bpf_struct_ops_member_info *info; + const char *name; + char *data; + int i, j, err; + + data = calloc(1, st_ops->shadow_info->data_size); + if (!data) + return -ENOMEM; + + for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { + name = btf__name_by_offset(btf, m->name_off); + if (!name) { + pr_warn("struct_ops init_shadow %s: member %d has no name\n", + map->name, i); + err = -EINVAL; + goto err_out; + } + for (j = 0, info = st_ops->shadow_info->members; + j < st_ops->shadow_info->cnt; + j++, info++) { + if (strcmp(name, info->name) == 0) + break; + } + if (j == st_ops->shadow_info->cnt) + info = NULL; + mt = skip_mods_and_typedefs(btf, m->type, NULL); + + switch (btf_kind(mt)) { + case BTF_KIND_INT: + case BTF_KIND_FLOAT: + case BTF_KIND_ENUM: + case BTF_KIND_ENUM64: + if (!info) { + pr_warn("struct_ops init_shadow %s: member %s not found in map info\n", + map->name, name); + err = -EINVAL; + goto err_out; + } + if (info->size != mt->size) { + pr_warn("struct_ops init_shadow %s: member %s size mismatch: %u != %u\n", + map->name, name, info->size, mt->size); + err = -EINVAL; + goto err_out; + } + memcpy(data + info->offset, st_ops->data + m->offset / 8, mt->size); + break; + + case BTF_KIND_PTR: + if (!resolve_func_ptr(btf, m->type, NULL)) { + if (!info) + break; + pr_warn("struct_ops init_shadow %s: member %s is not a func ptr\n", + map->name, name); + err = -ENOTSUP; + goto err_out; + } + if (!info) { + pr_warn("struct_ops init_shadow %s: member %s not found in map info\n", + map->name, name); + err = -EINVAL; + goto err_out; + } + if (info->size != sizeof(void *)) { + pr_warn("struct_ops init_shadow %s: member %s size mismatch: %u != %lu\n", + map->name, name, info->size, sizeof(void *)); + err = -EINVAL; + goto err_out; + } + *((struct bpf_program **)(data + info->offset)) = + st_ops->progs[i]; + break; + + default: + if (info) { + pr_warn("struct_ops init_shadow %s: member %s not supported type\n", + map->name, name); + err = -ENOTSUP; + goto err_out; + } + break; + } + } + + st_ops->shadow_data = data; + + return 0; + +err_out: + free(data); + return err; +} + static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, - int shndx, Elf_Data *data, __u32 map_flags) + int shndx, Elf_Data *data, __u32 map_flags, + const struct bpf_struct_ops_shadow_info *shadow) { const struct btf_type *type, *datasec; const struct btf_var_secinfo *vsi; @@ -1182,7 +1302,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, __s32 type_id, datasec_id; const struct btf *btf; struct bpf_map *map; - __u32 i; + __u32 i, j; if (shndx == -1) return 0; @@ -1260,6 +1380,16 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, st_ops->type = type; st_ops->type_id = type_id; + if (shadow) { + for (j = 0; j < shadow->cnt; j++) { + if (strcmp(shadow->maps[j].name, var_name)) + continue; + st_ops->shadow_info = &shadow->maps[j]; + break; + } + + } + pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n", tname, type_id, var_name, vsi->offset); } @@ -1267,16 +1397,19 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, return 0; } -static int bpf_object_init_struct_ops(struct bpf_object *obj) +static int bpf_object_init_struct_ops(struct bpf_object *obj, + const struct bpf_struct_ops_shadow_info *shadow) { int err; err = init_struct_ops_maps(obj, STRUCT_OPS_SEC, obj->efile.st_ops_shndx, - obj->efile.st_ops_data, 0); + obj->efile.st_ops_data, 0, shadow); err = err ?: init_struct_ops_maps(obj, STRUCT_OPS_LINK_SEC, obj->efile.st_ops_link_shndx, obj->efile.st_ops_link_data, - BPF_F_LINK); + BPF_F_LINK, + shadow); + return err; } @@ -2145,7 +2278,7 @@ skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id) return t; } -static const struct btf_type * +const struct btf_type * resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id) { const struct btf_type *t; @@ -2736,17 +2869,19 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, static int bpf_object__init_maps(struct bpf_object *obj, const struct bpf_object_open_opts *opts) { + const struct bpf_struct_ops_shadow_info *shadow_info; const char *pin_root_path; bool strict; int err = 0; strict = !OPTS_GET(opts, relaxed_maps, false); pin_root_path = OPTS_GET(opts, pin_root_path, NULL); + shadow_info = OPTS_GET(opts, struct_ops_shadow, NULL); err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path); err = err ?: bpf_object__init_global_data_maps(obj); err = err ?: bpf_object__init_kconfig_map(obj); - err = err ?: bpf_object_init_struct_ops(obj); + err = err ?: bpf_object_init_struct_ops(obj, shadow_info); return err; } @@ -7528,6 +7663,33 @@ static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object return 0; } +/* Create a shadow copy for each struct_ops map if it has shadow info. + * + * The shadow copy should be created after bpf_object__collect_relos() + * since st_ops->progs is initialized in that function. + */ +static int bpf_object__init_shadow(struct bpf_object *obj) +{ + struct bpf_map *map; + int err; + + bpf_object__for_each_map(map, obj) { + if (!bpf_map__is_struct_ops(map)) + continue; + + if (!map->st_ops->shadow_info) + continue; + err = init_struct_ops_shadow(map, map->st_ops->type); + if (err) { + pr_warn("map '%s': failed to init shadow: %d\n", + map->name, err); + return err; + } + } + + return 0; +} + static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz, const struct bpf_object_open_opts *opts) { @@ -7624,6 +7786,7 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, err = err ? : bpf_object__init_maps(obj, opts); err = err ? : bpf_object_init_progs(obj, opts); err = err ? : bpf_object__collect_relos(obj); + err = err ? : bpf_object__init_shadow(obj); if (err) goto out; @@ -8588,6 +8751,7 @@ static void bpf_map__destroy(struct bpf_map *map) } if (map->st_ops) { + zfree(&map->st_ops->shadow_data); zfree(&map->st_ops->data); zfree(&map->st_ops->progs); zfree(&map->st_ops->kern_func_off); @@ -9877,6 +10041,12 @@ int bpf_map__set_initial_value(struct bpf_map *map, void *bpf_map__initial_value(struct bpf_map *map, size_t *psize) { + if (bpf_map__is_struct_ops(map)) { + if (psize) + *psize = map->st_ops->shadow_data_size; + return map->st_ops->shadow_data; + } + if (!map->mmaped) return NULL; *psize = map->def.value_size; @@ -13462,3 +13632,8 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s) free(s->progs); free(s); } + +__u32 bpf_map__struct_ops_type(const struct bpf_map *map) +{ + return map->st_ops->type_id; +} diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 5723cbbfcc41..b435cafefe7a 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -109,6 +109,27 @@ LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn); /* Hide internal to user */ struct bpf_object; +/* Description of a member in the struct_ops type for a map. */ +struct bpf_struct_ops_member_info { + const char *name; + __u32 offset; + __u32 size; +}; + +/* Description of the layout of a shadow copy for a struct_ops map. */ +struct bpf_struct_ops_map_info { + /* The name of the struct_ops map */ + const char *name; + const struct bpf_struct_ops_member_info *members; + __u32 cnt; + __u32 data_size; +}; + +struct bpf_struct_ops_shadow_info { + const struct bpf_struct_ops_map_info *maps; + __u32 cnt; +}; + struct bpf_object_open_opts { /* size of this struct, for forward/backward compatibility */ size_t sz; @@ -197,9 +218,18 @@ struct bpf_object_open_opts { */ const char *bpf_token_path; + /* A list of shadow info for every struct_ops map. A shadow info + * provides the information used by libbpf to map the offsets of + * struct members of a struct_ops type from BTF to the offsets of + * the corresponding members in the shadow copy in the user + * space. It ensures that the shadow copy provided by the libbpf + * can be accessed by the user space program correctly. + */ + const struct bpf_struct_ops_shadow_info *struct_ops_shadow; + size_t :0; }; -#define bpf_object_open_opts__last_field bpf_token_path +#define bpf_object_open_opts__last_field struct_ops_shadow /** * @brief **bpf_object__open()** creates a bpf_object by opening @@ -839,6 +869,8 @@ struct bpf_map; LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map); LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map); +LIBBPF_API __u32 bpf_map__struct_ops_type(const struct bpf_map *map); + struct bpf_iter_attach_opts { size_t sz; /* size of this struct for forward/backward compatibility */ union bpf_iter_link_info *link_info; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 86804fd90dd1..e0efc85114df 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -413,4 +413,5 @@ LIBBPF_1.4.0 { bpf_token_create; btf__new_split; btf_ext__raw_data; + bpf_map__struct_ops_type; } LIBBPF_1.3.0; diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index ad936ac5e639..aec6d57fe5d1 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -234,6 +234,7 @@ struct btf_type; struct btf_type *btf_type_by_id(const struct btf *btf, __u32 type_id); const char *btf_kind_str(const struct btf_type *t); const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id); +const struct btf_type *resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id); static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t) { From patchwork Wed Feb 14 02:08:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13555889 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9CB96138 for ; Wed, 14 Feb 2024 02:08:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707876528; cv=none; b=TqS8jBik4aMVZHvHdV1kxV6Lq5/Dku3EUjz8JE+Prn1Y4EUof9dlJwOWnULx6g7FvbgQL/hD1J3JHyUVvBaodUbYAz09G1my8YGp7ZpSe8SxPkkIwq27eCqtVAvgb48DuCqg75Z2MsW8RY0Vf73NcK7/5JbokrQDlsRwdHpKwkI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707876528; c=relaxed/simple; bh=cwhTtlpyBvYRzhLElp2zfDAiWmOy3W1GfAhUJ9o0W0s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GjmbQJdE+tsH3cLh5fNWVz3bBpxUdEx0yVandgIo5aUTXzi2Sdnb4vu+mWBuIhjN4MPYmaG+vPQUuLS3ETrCPjItP6e+dAwI6CvGwzQ61egrZ3X8VmxxPcXwX7yOSfh/ZtvqYSXduhpGjpmqBVqfMgkZmyoaT7HoLIw6PVnCXIE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Hc3DQy4z; arc=none smtp.client-ip=209.85.219.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Hc3DQy4z" Received: by mail-yb1-f171.google.com with SMTP id 3f1490d57ef6-dcbc6a6808fso2273792276.2 for ; Tue, 13 Feb 2024 18:08:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707876525; x=1708481325; darn=vger.kernel.org; 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=ySEb+RhEnn7fjRukPpR9W+Dg4RoCgjYlqvoMU8NyNy8=; b=Hc3DQy4z5hrD/oC+Dxp3faac19/s0yXM1PobWKuR+PXfBwv09xmIkjZjomsxnXeE9M zQKoToYibdbpA7wHB0LEiQr/URpc2UbOj+2QJbfhRnWHRVDg91x0OGERgLI/xReY7si3 ubdFWItzk8jUOYojPO32uCXGbIUXMX3THWyi7/vTGzxutwRv63b84uqBPJpOzrDVRpHY 8cX7CHNhsJBMI572Jg89YKONnmZoZTEJdA/0s4uMY4amuCnKturot2RedxbgtWCsZ35s mX8nRu41Lu/eCqpd8Ykc+//0zbFJ1rgtzBr8JWd2IWF53LNjnLXeoETuUkmGFAZLB2eS 6k0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707876525; x=1708481325; 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=ySEb+RhEnn7fjRukPpR9W+Dg4RoCgjYlqvoMU8NyNy8=; b=jEUHQTnqF/pAaKJgydGWwqQ8FhAtK99hdWYVyj62DG5nJxDYEHf+0sSmUgwl4CkdGT Sk8PFJvs0eJ5Z2JvctwstCbRkVZes2tOn9B2QWqrs6t53ti02Omon1Uh390mRfPE3edW K05e+gUvzr9HMZ5sUSnLW+7RdGwvuhu1UcJBP+8AQDlZ9abTTRmuj+jP3fsmmO8ex/Jv gIr1QJnJXIj725AKwJu3PwmfVYGBd3MItmdVHNYnKOXTaQFD/vhW+s+taGY0jtuG2hEp FbBkMZM+AWm4Li6+S/NAAGtgZJ3xaV6Ibo5wXFpo4wDC3awKz1uX4xaIuxhUoX9lMtFr h/kg== X-Gm-Message-State: AOJu0Yyf2LdlRIL+SO/rIr2EriU9RVO3kT0N28oRrIRmA5btHUb/PwCN GZV+axRcUx1SGopPnyQ3ycDyKQAzJOdjG6IdVvo9zbY9yPNj7+Vjbc0VHEjV X-Google-Smtp-Source: AGHT+IG1JB+KCod1LOMOYNDUCf/GIvHUTrIN8EWEUZAPV7Ej22tdgeF5Z47bKgp8Y1sieDamzMaTmA== X-Received: by 2002:a05:6902:2585:b0:dcc:57ff:fb70 with SMTP id du5-20020a056902258500b00dcc57fffb70mr979735ybb.60.1707876525239; Tue, 13 Feb 2024 18:08:45 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCX/MP+N9Kp/mol27G0QI2Eoz122Nsa0S5aOe6nVKTjI97X/FEgihqlAVuOy1uqi3YOo/zg9bbSLSdWdGHdDR9VKoXFw6uUAhyzv0mza94r4jRZ5jYxgZtESyo4oAjsfPwVCf8giqF8HxgGzbB1KwOB72K0ADcKDWfz2gOYJRUM2o457uNTfZXk0xYp4Fd9hmqZoibwzz93SkRGO9EcjC6vv/KRmJMpY5c4+Lt2AK1/paXUggT1/2g== Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:966f:7edc:e6e6:cd97]) by smtp.gmail.com with ESMTPSA id s17-20020a258311000000b00dc2310abe8bsm1894752ybk.38.2024.02.13.18.08.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 18:08:44 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [RFC bpf-next v2 2/3] bpftool: generated shadow variables for struct_ops maps. Date: Tue, 13 Feb 2024 18:08:35 -0800 Message-Id: <20240214020836.1845354-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240214020836.1845354-1-thinker.li@gmail.com> References: <20240214020836.1845354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC From: Kui-Feng Lee Declares and defines a pointer to a shadow copy for each struct_ops map. Create shadow info to describe the layout of shadow copies of every struct_ops map. The shadow info will be passed to libbpf when calling *__open() of the skeleton. And, initialize the pointers to shadow copies created by the libbpf. Signed-off-by: Kui-Feng Lee --- tools/bpf/bpftool/gen.c | 358 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 350 insertions(+), 8 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index a9334c57e859..fb5d01dcfe17 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -909,6 +909,281 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li } } +/* A callback (cb) will be called for each member that can have a shadow of + * a map. + * + * For now, it supports only scalar types and function pointers. + */ +static int walk_st_ops_shadow_vars(struct btf *btf, + const char *ident, + const struct bpf_map *map, + int (*cb)(const struct btf *btf, + const char *ident, + const struct btf_member *m, + __u32 member_type_id, + bool func_ptr)) +{ + const struct btf_type *map_type, *member_type; + const struct btf_member *m; + __u32 map_type_id, member_type_id; + int i, err; + + map_type_id = bpf_map__struct_ops_type(map); + if (map_type_id == 0) + return -EINVAL; + map_type = btf__type_by_id(btf, map_type_id); + if (!map_type) + return -EINVAL; + + for (i = 0, m = btf_members(map_type); + i < btf_vlen(map_type); + i++, m++) { + member_type = skip_mods_and_typedefs(btf, m->type, + &member_type_id); + if (!member_type) + return -EINVAL; + + switch (btf_kind(member_type)) { + case BTF_KIND_INT: + case BTF_KIND_FLOAT: + case BTF_KIND_ENUM: + case BTF_KIND_ENUM64: + /* scalar type */ + err = cb(btf, ident, m, member_type_id, false); + if (err) + return err; + break; + + case BTF_KIND_PTR: + if (resolve_func_ptr(btf, m->type, NULL)) { + /* Function pointer */ + err = cb(btf, ident, m, member_type_id, true); + if (err) + return err; + } + break; + + default: + break; + } + } + + return 0; +} + +static int gen_st_ops_sahdow_var(const struct btf *btf, + const char *ident, + const struct btf_member *m, + __u32 member_type_id, + bool func_ptr) +{ + DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts, + .indent_level = 3, + .strip_mods = false, + ); + struct btf_dump *d = NULL; + int err = 0; + + if (func_ptr) { + printf("\t\t\tconst struct bpf_program *%s;\n", + btf__name_by_offset(btf, m->name_off)); + } else { + d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL); + if (!d) { + err = -errno; + goto out; + } + + printf("\t\t\t"); + opts.field_name = btf__name_by_offset(btf, m->name_off); + err = btf_dump__emit_type_decl(d, member_type_id, &opts); + if (err) + goto out; + printf(";\n"); + } + +out: + btf_dump__free(d); + + return err; +} + +/* Add a pointer of a shadow copy to the skeleton for a struct_ops map. + * + * A shadow copy of a struct_ops map is used to store the sahdow variables. + * Shadow variables are a mirror of member fields in a struct_ops map + * defined in the BPF program. They serve as a way to access and change the + * values in the map. For example, in struct bpf_testmod_ops, it defines + * three fields: test_1, test_2, and data. And, it defines an instance as + * testmod_1 in the program. Then, the skeleton will have three shadow + * variables: test_1, test_2, and data in skel->st_ops.testmod_1. + * + * Now, it doesn't support pointer type fields except function pointers. + * For non-function pointer fields, the shadow variables will be in the + * same type as the original fields, but all modifiers (const, + * volatile,...) are removed. + * + * For function pointer fields, the shadow variables will be in the "struct + * bpf_program *" type. + */ +static int gen_st_ops_shadow_copy(struct btf *btf, const char *ident, + const struct bpf_map *map) +{ + int err; + + printf("\t\tstruct {\n"); + + err = walk_st_ops_shadow_vars(btf, ident, map, gen_st_ops_sahdow_var); + if (err) + return err; + + printf("\t\t} *%s;\n", ident); + + return 0; +} + +static int gen_st_ops_member_info(const struct btf *btf, + const char *ident, + const struct btf_member *m, + __u32 member_type_id, + bool func_ptr) +{ + const char *member_name = btf__name_by_offset(btf, m->name_off); + + codegen("\ + \n\ + { \n\ + .name = \"%2$s\", \n\ + .offset = (__u32)(uintptr_t)&((typeof(struct_ops->%1$s))0)->%2$s,\n\ + .size = sizeof(((typeof(struct_ops->%1$s))0)->%2$s),\n\ + }, \n\ + ", ident, member_name); + + return 0; +} + +static int gen_st_ops_member_infos(struct btf *btf, const char *ident, + const struct bpf_map *map) +{ + int err; + + printf("\tstatic struct bpf_struct_ops_member_info member_info_%s[] = {\n", + ident); + + err = walk_st_ops_shadow_vars(btf, ident, map, + gen_st_ops_member_info); + if (err) + return err; + + printf("\t};\n"); + + return 0; +} + +static void gen_st_ops_map_info(const char *ident) +{ + codegen("\ + \n\ + { \n\ + .name = \"%1$s\", \n\ + .members = member_info_%1$s, \n\ + .cnt = ARRAY_SIZE(member_info_%1$s), \n\ + .data_size = sizeof(*struct_ops->%1$s), \n\ + }, \n\ + ", ident); +} + +/* Generate information of shadow variables for every struct_ops map. + * + * The shadow information of map includes the name, the offset, and the + * size of each supported member field in the struct_ops map to describe + * the layout of a shadow copy. The shadow info of struct_ops maps will be + * passed to libbpf to create a shadow copy for each struct_ops map. + * + * For example, in struct_ops_module.c, it defines "testmod_1" in struct + * bpf_testmod_ops, which has three fields: test_1, test_2, and data. Then, + * the shadow information will be: + * + * static struct bpf_struct_ops_member_info member_info_testmod_1[] = { + * { + * .name = "test_1", + * .offset = ....., + * .size = ....., + * }, + * { + * .name = "test_2", + * .offset = ....., + * .size = ....., + * }, + * { + * .name = "data", + * .offset = ....., + * .size = ....., + * }, + * }; + * static struct bpf_struct_ops_map_info map_info[] = { + * { + * .name = "testmod_1", + * .members = member_info_testmod_1, + * .cnt = ARRAY_SIZE(member_info_testmod_1), + * .data_size = sizeof(struct_ops->testmod_1), + * }, + * }; + * static struct bpf_struct_ops_shadow_info shadow_info = { + * .maps = map_info, + * .cnt = ARRAY_SIZE(map_info), + * }; + * + * testmod_1_shadow_info() will return the pointer of "sahdow_info" defined + * above. + */ +static int gen_st_ops_shadow_info(struct bpf_object *obj, const char *obj_name) +{ + struct btf *btf = bpf_object__btf(obj); + struct bpf_map *map; + + codegen("\ + \n\ + static inline struct bpf_struct_ops_shadow_info * \n\ + %1$s__shadow_info() \n\ + { \n\ + typeof(((struct %1$s *)0)->struct_ops) *struct_ops = NULL;\n\ + ", obj_name); + bpf_object__for_each_map(map, obj) { + const char *ident = bpf_map__name(map); + int err; + + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) + continue; + + err = gen_st_ops_member_infos(btf, ident, map); + if (err) + return err; + } + printf("\tstatic struct bpf_struct_ops_map_info map_info[] = {\n"); + bpf_object__for_each_map(map, obj) { + const char *ident = bpf_map__name(map); + + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) + continue; + + gen_st_ops_map_info(ident); + } + codegen("\ + \n\ + }; \n\ + static struct bpf_struct_ops_shadow_info shadow_info = {\n\ + .maps = map_info, \n\ + .cnt = ARRAY_SIZE(map_info), \n\ + }; \n\ + \n\ + return &shadow_info; \n\ + } \n\ + \n"); + + return 0; +} + static int do_skeleton(int argc, char **argv) { char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")]; @@ -923,6 +1198,7 @@ static int do_skeleton(int argc, char **argv) struct bpf_map *map; struct btf *btf; struct stat st; + int st_ops_cnt = 0; if (!REQ_ARGS(1)) { usage(); @@ -1048,8 +1324,33 @@ static int do_skeleton(int argc, char **argv) printf("\t\tstruct bpf_map_desc %s;\n", ident); else printf("\t\tstruct bpf_map *%s;\n", ident); + if (bpf_map__type(map) == BPF_MAP_TYPE_STRUCT_OPS) + st_ops_cnt++; } printf("\t} maps;\n"); + if (st_ops_cnt) { + /* Generate the pointers to shadow copies of + * struct_ops maps. + */ + btf = bpf_object__btf(obj); + if (!btf) { + err = -1; + p_err("need btf type information for %s", obj_name); + goto out; + } + + printf("\tstruct {\n"); + bpf_object__for_each_map(map, obj) { + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) + continue; + err = gen_st_ops_shadow_copy(btf, ident, map); + if (err) + goto out; + } + printf("\t} struct_ops;\n"); + } } if (prog_cnt) { @@ -1075,12 +1376,9 @@ static int do_skeleton(int argc, char **argv) printf("\t} links;\n"); } - btf = bpf_object__btf(obj); - if (btf) { - err = codegen_datasecs(obj, obj_name); - if (err) - goto out; - } + err = codegen_datasecs(obj, obj_name); + if (err) + goto out; if (use_loader) { err = gen_trace(obj, obj_name, header_guard); goto out; @@ -1099,7 +1397,17 @@ static int do_skeleton(int argc, char **argv) static inline const void *elf_bytes(size_t *sz); \n\ #endif /* __cplusplus */ \n\ }; \n\ - \n\ + \n\ + ", obj_name); + + if (st_ops_cnt) { + err = gen_st_ops_shadow_info(obj, obj_name); + if (err) + goto out; + } + + codegen("\ + \n\ static void \n\ %1$s__destroy(struct %1$s *obj) \n\ { \n\ @@ -1133,6 +1441,24 @@ static int do_skeleton(int argc, char **argv) if (err) \n\ goto err_out; \n\ \n\ + ", obj_name); + if (st_ops_cnt) { + /* Initialize the pointers of struct_ops shadow copies */ + bpf_object__for_each_map(map, obj) { + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) + continue; + codegen("\ + \n\ + obj->struct_ops.%1$s = \n\ + bpf_map__initial_value(obj->maps.%1$s, NULL);\n\ + \n\ + ", ident); + } + } + codegen("\ + \n\ return obj; \n\ err_out: \n\ %1$s__destroy(obj); \n\ @@ -1143,7 +1469,23 @@ static int do_skeleton(int argc, char **argv) static inline struct %1$s * \n\ %1$s__open(void) \n\ { \n\ - return %1$s__open_opts(NULL); \n\ + ", obj_name); + if (st_ops_cnt) + codegen("\ + \n\ + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);\n\ + \n\ + opts.struct_ops_shadow = %1$s__shadow_info();\n\ + \n\ + return %1$s__open_opts(&opts); \n\ + ", obj_name); + else + codegen("\ + \n\ + return %1$s__open_opts(NULL); \n\ + ", obj_name); + codegen("\ + \n\ } \n\ \n\ static inline int \n\ From patchwork Wed Feb 14 02:08:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13555890 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF0BB79E4 for ; Wed, 14 Feb 2024 02:08:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707876529; cv=none; b=BEYR8amrzpTIzmnIfQDtcI/nFLkirnbnXSJhRxHxL0unywijJSvzCOksk6C2EnrFdrRj2fCjBR593wDD7XRQgB43EPiTRR8p/Nq1MXP/4V48QnYWSK8Ojcj1CXwnMMNSyKRSqPbfPdHcj25Ory44AAtoy6X7O0CKJx6kBi29qwI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707876529; c=relaxed/simple; bh=rAijJx3F0NAnIP754tBz6sHGDAXUutn7JktKIRBgJvo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qCQ2n+S/62Z6yTQNLQ7fhtrxwJBzAsFG2ZGADoG8eRNY+E2I8xSkOFqyik5BW+aTMdg6SiODQNBrNYYEVHQNvnjspcI59UEj3l9vvvSM8SdNSgIm5ofHSqCJQN7Q7VdYKjRbNvE3LTb/r4EKsTT94sv/qxJNJE/edS2ErUYyJJM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FNNwl34O; arc=none smtp.client-ip=209.85.219.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FNNwl34O" Received: by mail-yb1-f178.google.com with SMTP id 3f1490d57ef6-db4364ecd6aso4366890276.2 for ; Tue, 13 Feb 2024 18:08:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707876526; x=1708481326; darn=vger.kernel.org; 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=hjN4u8nYemxSER4RxMIvQTe+Q2rce+eGYsiG4eAZVsA=; b=FNNwl34OESSG9xYohFTVIa6GfVb06LW+Yo9mNHairhh8CMAcWqS/C/nHX3evx2uXlc EaYe5TwcLm9ogvR7hEr2rJvASo9dkcMEQUbypfyY6GDTr4cpFXcDSw0+Zib1z4XwOLC6 wZEVi7ls0ENylDV+TbK/iue3ryOo+jgmMmq9/rqZvTV8kpxOPDEpR92eUoKT/QIOjccq TyGahKVlyjB4irjzJHemMpwZCNqnt9x2sZLGkgge8IfEh732DUf7BqrOoqBdAw+VorFa rZTVcAnCyi0F2yQ0k5KdDlZJvlmEfAwAoE+8NNQZSaqfSZDTYXdyuC8MD5U5vwBq865t FvCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707876526; x=1708481326; 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=hjN4u8nYemxSER4RxMIvQTe+Q2rce+eGYsiG4eAZVsA=; b=fag2AP/DmKGpc+hPquJtIka6EsEJ1z1T00dkGNZnIz+wIMFvSZsRSUvhP/7+mAg3tz g9SkE50LysCGCKYYqzLl2+yM2yMbqmgdmLjCken4XaYbSNE2nUxvV9sqc0jh72YcZfNh 3IoBhM0T+tUEznczdzMvDfWz/GZNcZYwY2lVmcglahVk6ry1Mr6Wj4fbWNRMZIRPqftr 4x5LwoQwREszaNJg3hWgo95CETAOoCjJMNJg/96FeEoSe9VNsOGAS+G+aKF2TwzBWajC z8D0T/NCH6WxTGHLH2K2CavXlKHhuBqR7crNuvnbK5NsuHy1eLRmCXbT6gCqOM9qNzni NARw== X-Gm-Message-State: AOJu0YxlFoj185oGIc3kXbL7B2SkUCW23PF24fStL6v6GBVvJQGObLXw CeaXQ059Bd89+9k2ptRjF6yGbhGQ7tMSLHNV/TRcOrBFxfiAre1kepxwrplK X-Google-Smtp-Source: AGHT+IHvTmaHxhL1QH4tLx478bVVp1NhvjQKyeXBxuuwhnMn5MkflaFZixae96felph3ouN1woIpOw== X-Received: by 2002:a25:830f:0:b0:dc6:ff32:aae2 with SMTP id s15-20020a25830f000000b00dc6ff32aae2mr740019ybk.63.1707876526415; Tue, 13 Feb 2024 18:08:46 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWul3cXbALI3eSgizH1yAmVHrzADpv+DRVvWbei0CXb3ArOd2reYl1UHGOzv7bjD6crRcO4aIr2IN57wF7KLSN9zJltejXsQ2cdGONrHWi4qyX/ziWrhS4vDz4KsW2DwxIj97lArOP7mWAAma3h8ZdvlxdLujYupQxcLapgZIemV0on3/8aXNEEF7TIuYb/73zMsdxHvimFQRU5ZBY0F9ogsiqpXWcfNNXuQTe4kWwQH9IGdqNqrQ== Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:966f:7edc:e6e6:cd97]) by smtp.gmail.com with ESMTPSA id s17-20020a258311000000b00dc2310abe8bsm1894752ybk.38.2024.02.13.18.08.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 18:08:46 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [RFC bpf-next v2 3/3] selftests/bpf: Test if shadow variables work. Date: Tue, 13 Feb 2024 18:08:36 -0800 Message-Id: <20240214020836.1845354-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240214020836.1845354-1-thinker.li@gmail.com> References: <20240214020836.1845354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC From: Kui-Feng Lee Change the value of fields, including scalar types and function pointers, and check if the struct_ops map works as expected. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 6 +++++- .../selftests/bpf/bpf_testmod/bpf_testmod.h | 1 + .../bpf/prog_tests/test_struct_ops_module.c | 16 ++++++++++++---- .../selftests/bpf/progs/struct_ops_module.c | 8 ++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index 66787e99ba1b..96fb0f44a390 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -539,6 +539,10 @@ static int bpf_testmod_ops_init_member(const struct btf_type *t, const struct btf_member *member, void *kdata, const void *udata) { + if (member->offset == offsetof(struct bpf_testmod_ops, data) * 8) { + ((struct bpf_testmod_ops *)kdata)->data = ((struct bpf_testmod_ops *)udata)->data; + return 1; + } return 0; } @@ -559,7 +563,7 @@ static int bpf_dummy_reg(void *kdata) * initialized, so we need to check for NULL. */ if (ops->test_2) - ops->test_2(4, 3); + ops->test_2(4, ops->data); return 0; } diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h index c3b0cf788f9f..428efd65cafd 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h @@ -35,6 +35,7 @@ struct bpf_testmod_ops { void (*test_2)(int a, int b); /* Used to test nullable arguments. */ int (*test_maybe_null)(int dummy, struct task_struct *task); + int data; }; #endif /* _BPF_TESTMOD_H */ diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c index 8d833f0c7580..68d91b769ca0 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c @@ -32,17 +32,20 @@ static void check_map_info(struct bpf_map_info *info) static void test_struct_ops_load(void) { - DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); struct struct_ops_module *skel; struct bpf_map_info info = {}; struct bpf_link *link; int err; u32 len; - skel = struct_ops_module__open_opts(&opts); + skel = struct_ops_module__open(); if (!ASSERT_OK_PTR(skel, "struct_ops_module_open")) return; + skel->struct_ops.testmod_1->data = 13; + skel->struct_ops.testmod_1->test_2 = skel->progs.test_3; + bpf_program__set_autoload(skel->progs.test_2, false); + err = struct_ops_module__load(skel); if (!ASSERT_OK(err, "struct_ops_module_load")) goto cleanup; @@ -56,8 +59,13 @@ static void test_struct_ops_load(void) link = bpf_map__attach_struct_ops(skel->maps.testmod_1); ASSERT_OK_PTR(link, "attach_test_mod_1"); - /* test_2() will be called from bpf_dummy_reg() in bpf_testmod.c */ - ASSERT_EQ(skel->bss->test_2_result, 7, "test_2_result"); + /* test_3() will be called from bpf_dummy_reg() in bpf_testmod.c + * + * In bpf_testmod.c it will pass 4 and 13 (the value of data) to + * .test_2. So, the value of test_2_result should be 20 (4 + 13 + + * 3). + */ + ASSERT_EQ(skel->bss->test_2_result, 20, "test_2_result"); bpf_link__destroy(link); diff --git a/tools/testing/selftests/bpf/progs/struct_ops_module.c b/tools/testing/selftests/bpf/progs/struct_ops_module.c index b78746b3cef3..25952fa09348 100644 --- a/tools/testing/selftests/bpf/progs/struct_ops_module.c +++ b/tools/testing/selftests/bpf/progs/struct_ops_module.c @@ -21,9 +21,17 @@ void BPF_PROG(test_2, int a, int b) test_2_result = a + b; } +SEC("struct_ops/test_3") +int BPF_PROG(test_3, int a, int b) +{ + test_2_result = a + b + 3; + return a + b + 3; +} + SEC(".struct_ops.link") struct bpf_testmod_ops testmod_1 = { .test_1 = (void *)test_1, .test_2 = (void *)test_2, + .data = 0x1, };