Merge branch 'create-shadow-types-for-struct_ops-maps-in-skeletons'

Kui-Feng Lee says:

====================
Create shadow types for struct_ops maps in skeletons

This patchset allows skeleton users to change the values of the fields
in struct_ops maps at runtime. It will create a shadow type pointer in
a skeleton for each struct_ops map, allowing users to access the
values of fields through these pointers. For instance, if there is an
integer field named "FOO" in a struct_ops map called "testmap", you
can access the value of "FOO" in this way.

    skel->struct_ops.testmap->FOO = 13;

With this feature, the users can pass flags or other data along with
the map from the user space to the kernel without creating separate
struct_ops map for different values in BPF.

== Shadow Type ==

The shadow type of a struct_ops map is a variant of the original
struct type of the map. The code generator translates each field in
the original struct type to a field in the shadow type. The type of a
field in the shadow type may not be the same as the corresponding
field in the original struct type. For example, modifiers like
volatile, const, etc., are removed from the fields in a shadow
type. Function pointers are translated to pointers of struct
bpf_program.

Currently, only scalar types and function pointers are
supported. Fields belonging to structs, unions, non-function pointers,
arrays, or other types are not supported. For those unsupported
fields, they are converted to arrays of characters to preserve their
space within the original struct type.

The padding between consecutive fields is handled by padding fields
(__padding_*). This helps to maintain the memory layout consistent
with the original struct_type.

Here is an example of shadow types.
The origin struct type of a struct_ops map is

    struct bpf_testmod_ops {
    	int (*test_1)(void);
    	void (*test_2)(int a, int b);
    	/* Used to test nullable arguments. */
    	int (*test_maybe_null)(int dummy, struct task_struct *task);

    	/* The following fields are used to test shadow copies. */
    	char onebyte;
    	struct {
    		int a;
    		int b;
    	} unsupported;
    	int data;
    };

The struct_ops map, named testmod_1, of this type will be translated
to a pointer in the shadow type.

    struct {
    	struct my_skel__testmod_1__bpf_testmod_ops {
    		const struct bpf_program *test_1;
    		const struct bpf_program *test_2;
    		const struct bpf_program *test_maybe_null;
    		char onebyte;
    		char __padding_4[3];
    		char __unsupported_4[8];
    		int data;
    	} *testmod_1;
    } struct_ops;

== Convert st_ops->data to Shadow Type ==

libbpf converts st_ops->data to the format of the shadow type for each
struct_ops map. This means that the bytes where function pointers are
located are converted to the values of the pointers of struct
bpf_program. The fields of other types are kept as they were.

Libbpf will synchronize the pointers of struct bpf_program with
st_ops->progs[] so that users can change function pointers
(bpf_program) before loading the map.
---
Changes from v5:

 - Generate names for shadow types.

 - Check btf and the number of struct_ops maps in gen_st_ops_shadow()
   and gen_st_ops_shadow_init() instead of do_skeleton() and
   do_subskeleton().

 - Name unsupported fields in the pattern __unsupported_*.

 - Have a padding field for a unsupported fields as well if necessary.

 - Implement resolve_func_ptr() in gen.c instead of reusing the one in
   libbpf. (Remove the part 1 in v4.)

 - Fix stylistic issues.

Changes from v4:

 - Convert function pointers to the pointers to struct bpf_program in
   bpf_object__collect_st_ops_relos().

Changes from v3:

 - Add comment to avoid people from removing resolve_func_ptr() from
   libbpf_internal.h

 - Fix commit logs and comments.

 - Add an example about using the pointers of shadow types
   for struct_ops maps to bpftool-gen.8.

v5: https://lore.kernel.org/all/20240227010432.714127-1-thinker.li@gmail.com/
v4: https://lore.kernel.org/all/20240222222624.1163754-1-thinker.li@gmail.com/
v3: https://lore.kernel.org/all/20240221012329.1387275-1-thinker.li@gmail.com/
v2: https://lore.kernel.org/all/20240214020836.1845354-1-thinker.li@gmail.com/
v1: https://lore.kernel.org/all/20240124224130.859921-1-thinker.li@gmail.com/

Kui-Feng Lee (5):
  libbpf: set btf_value_type_id of struct bpf_map for struct_ops.
  libbpf: Convert st_ops->data to shadow type.
  bpftool: generated shadow variables for struct_ops maps.
  bpftool: Add an example for struct_ops map and shadow type.
  selftests/bpf: Test if shadow types work correctly.

 .../bpf/bpftool/Documentation/bpftool-gen.rst |  58 ++++-
 tools/bpf/bpftool/gen.c                       | 237 +++++++++++++++++-
 tools/lib/bpf/libbpf.c                        |  50 +++-
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  11 +-
 .../selftests/bpf/bpf_testmod/bpf_testmod.h   |   8 +
 .../bpf/prog_tests/test_struct_ops_module.c   |  19 +-
 .../selftests/bpf/progs/struct_ops_module.c   |   8 +
 7 files changed, 377 insertions(+), 14 deletions(-)
====================

Link: https://lore.kernel.org/r/20240229064523.2091270-1-thinker.li@gmail.com
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
This commit is contained in:
Andrii Nakryiko 2024-02-29 13:54:06 -08:00
commit 0270d69121
7 changed files with 372 additions and 14 deletions

View File

@ -257,18 +257,48 @@ EXAMPLES
return 0;
}
This is example BPF application with two BPF programs and a mix of BPF maps
and global variables. Source code is split across two source code files.
**$ cat example3.bpf.c**
::
#include <linux/ptrace.h>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
/* This header file is provided by the bpf_testmod module. */
#include "bpf_testmod.h"
int test_2_result = 0;
/* bpf_Testmod.ko calls this function, passing a "4"
* and testmod_map->data.
*/
SEC("struct_ops/test_2")
void BPF_PROG(test_2, int a, int b)
{
test_2_result = a + b;
}
SEC(".struct_ops")
struct bpf_testmod_ops testmod_map = {
.test_2 = (void *)test_2,
.data = 0x1,
};
This is example BPF application with three BPF programs and a mix of BPF
maps and global variables. Source code is split across three source code
files.
**$ clang --target=bpf -g example1.bpf.c -o example1.bpf.o**
**$ clang --target=bpf -g example2.bpf.c -o example2.bpf.o**
**$ bpftool gen object example.bpf.o example1.bpf.o example2.bpf.o**
**$ clang --target=bpf -g example3.bpf.c -o example3.bpf.o**
This set of commands compiles *example1.bpf.c* and *example2.bpf.c*
individually and then statically links respective object files into the final
BPF ELF object file *example.bpf.o*.
**$ bpftool gen object example.bpf.o example1.bpf.o example2.bpf.o example3.bpf.o**
This set of commands compiles *example1.bpf.c*, *example2.bpf.c* and
*example3.bpf.c* individually and then statically links respective object
files into the final BPF ELF object file *example.bpf.o*.
**$ bpftool gen skeleton example.bpf.o name example | tee example.skel.h**
@ -291,7 +321,15 @@ BPF ELF object file *example.bpf.o*.
struct bpf_map *data;
struct bpf_map *bss;
struct bpf_map *my_map;
struct bpf_map *testmod_map;
} maps;
struct {
struct example__testmod_map__bpf_testmod_ops {
const struct bpf_program *test_1;
const struct bpf_program *test_2;
int data;
} *testmod_map;
} struct_ops;
struct {
struct bpf_program *handle_sys_enter;
struct bpf_program *handle_sys_exit;
@ -304,6 +342,7 @@ BPF ELF object file *example.bpf.o*.
struct {
int x;
} data;
int test_2_result;
} *bss;
struct example__data {
_Bool global_flag;
@ -342,10 +381,16 @@ BPF ELF object file *example.bpf.o*.
skel->rodata->param1 = 128;
/* Change the value through the pointer of shadow type */
skel->struct_ops.testmod_map->data = 13;
err = example__load(skel);
if (err)
goto cleanup;
/* The result of the function test_2() */
printf("test_2_result: %d\n", skel->bss->test_2_result);
err = example__attach(skel);
if (err)
goto cleanup;
@ -372,6 +417,7 @@ BPF ELF object file *example.bpf.o*.
::
test_2_result: 17
my_map name: my_map
sys_enter prog FD: 8
my_static_var: 7

View File

@ -55,6 +55,20 @@ static bool str_has_suffix(const char *str, const char *suffix)
return true;
}
static const struct btf_type *
resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
{
const struct btf_type *t;
t = skip_mods_and_typedefs(btf, id, NULL);
if (!btf_is_ptr(t))
return NULL;
t = skip_mods_and_typedefs(btf, t->type, res_id);
return btf_is_func_proto(t) ? t : NULL;
}
static void get_obj_name(char *name, const char *file)
{
char file_copy[PATH_MAX];
@ -909,6 +923,207 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li
}
}
static int walk_st_ops_shadow_vars(struct btf *btf, const char *ident,
const struct btf_type *map_type, __u32 map_type_id)
{
LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts, .indent_level = 3);
const struct btf_type *member_type;
__u32 offset, next_offset = 0;
const struct btf_member *m;
struct btf_dump *d = NULL;
const char *member_name;
__u32 member_type_id;
int i, err = 0, n;
int size;
d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL);
if (!d)
return -errno;
n = btf_vlen(map_type);
for (i = 0, m = btf_members(map_type); i < n; i++, m++) {
member_type = skip_mods_and_typedefs(btf, m->type, &member_type_id);
member_name = btf__name_by_offset(btf, m->name_off);
offset = m->offset / 8;
if (next_offset < offset)
printf("\t\t\tchar __padding_%d[%d];\n", i, offset - next_offset);
switch (btf_kind(member_type)) {
case BTF_KIND_INT:
case BTF_KIND_FLOAT:
case BTF_KIND_ENUM:
case BTF_KIND_ENUM64:
/* scalar type */
printf("\t\t\t");
opts.field_name = member_name;
err = btf_dump__emit_type_decl(d, member_type_id, &opts);
if (err) {
p_err("Failed to emit type declaration for %s: %d", member_name, err);
goto out;
}
printf(";\n");
size = btf__resolve_size(btf, member_type_id);
if (size < 0) {
p_err("Failed to resolve size of %s: %d\n", member_name, size);
err = size;
goto out;
}
next_offset = offset + size;
break;
case BTF_KIND_PTR:
if (resolve_func_ptr(btf, m->type, NULL)) {
/* Function pointer */
printf("\t\t\tstruct bpf_program *%s;\n", member_name);
next_offset = offset + sizeof(void *);
break;
}
/* All pointer types are unsupported except for
* function pointers.
*/
fallthrough;
default:
/* Unsupported types
*
* Types other than scalar types and function
* pointers are currently not supported in order to
* prevent conflicts in the generated code caused
* by multiple definitions. For instance, if the
* struct type FOO is used in a struct_ops map,
* bpftool has to generate definitions for FOO,
* which may result in conflicts if FOO is defined
* in different skeleton files.
*/
size = btf__resolve_size(btf, member_type_id);
if (size < 0) {
p_err("Failed to resolve size of %s: %d\n", member_name, size);
err = size;
goto out;
}
printf("\t\t\tchar __unsupported_%d[%d];\n", i, size);
next_offset = offset + size;
break;
}
}
/* Cannot fail since it must be a struct type */
size = btf__resolve_size(btf, map_type_id);
if (next_offset < (__u32)size)
printf("\t\t\tchar __padding_end[%d];\n", size - next_offset);
out:
btf_dump__free(d);
return err;
}
/* Generate the pointer of the shadow type for a struct_ops map.
*
* This function adds a pointer of the shadow type for a struct_ops map.
* The members of a struct_ops map can be exported through a pointer to a
* shadow type. The user can access these members through the pointer.
*
* A shadow type includes not all members, only members of some types.
* They are scalar types and function pointers. The function pointers are
* translated to the pointer of the struct bpf_program. The scalar types
* are translated to the original type without any modifiers.
*
* Unsupported types will be translated to a char array to occupy the same
* space as the original field, being renamed as __unsupported_*. The user
* should treat these fields as opaque data.
*/
static int gen_st_ops_shadow_type(const char *obj_name, struct btf *btf, const char *ident,
const struct bpf_map *map)
{
const struct btf_type *map_type;
const char *type_name;
__u32 map_type_id;
int err;
map_type_id = bpf_map__btf_value_type_id(map);
if (map_type_id == 0)
return -EINVAL;
map_type = btf__type_by_id(btf, map_type_id);
if (!map_type)
return -EINVAL;
type_name = btf__name_by_offset(btf, map_type->name_off);
printf("\t\tstruct %s__%s__%s {\n", obj_name, ident, type_name);
err = walk_st_ops_shadow_vars(btf, ident, map_type, map_type_id);
if (err)
return err;
printf("\t\t} *%s;\n", ident);
return 0;
}
static int gen_st_ops_shadow(const char *obj_name, struct btf *btf, struct bpf_object *obj)
{
int err, st_ops_cnt = 0;
struct bpf_map *map;
char ident[256];
if (!btf)
return 0;
/* Generate the pointers to shadow types of
* struct_ops maps.
*/
bpf_object__for_each_map(map, obj) {
if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
continue;
if (!get_map_ident(map, ident, sizeof(ident)))
continue;
if (st_ops_cnt == 0) /* first struct_ops map */
printf("\tstruct {\n");
st_ops_cnt++;
err = gen_st_ops_shadow_type(obj_name, btf, ident, map);
if (err)
return err;
}
if (st_ops_cnt)
printf("\t} struct_ops;\n");
return 0;
}
/* Generate the code to initialize the pointers of shadow types. */
static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj)
{
struct bpf_map *map;
char ident[256];
if (!btf)
return;
/* Initialize the pointers to_ops shadow types of
* struct_ops maps.
*/
bpf_object__for_each_map(map, obj) {
if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
continue;
if (!get_map_ident(map, ident, sizeof(ident)))
continue;
codegen("\
\n\
obj->struct_ops.%1$s = bpf_map__initial_value(obj->maps.%1$s, NULL);\n\
\n\
", ident);
}
}
static int do_skeleton(int argc, char **argv)
{
char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
@ -1052,6 +1267,11 @@ static int do_skeleton(int argc, char **argv)
printf("\t} maps;\n");
}
btf = bpf_object__btf(obj);
err = gen_st_ops_shadow(obj_name, btf, obj);
if (err)
goto out;
if (prog_cnt) {
printf("\tstruct {\n");
bpf_object__for_each_program(prog, obj) {
@ -1075,7 +1295,6 @@ 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)
@ -1133,6 +1352,12 @@ static int do_skeleton(int argc, char **argv)
if (err) \n\
goto err_out; \n\
\n\
", obj_name);
gen_st_ops_shadow_init(btf, obj);
codegen("\
\n\
return obj; \n\
err_out: \n\
%1$s__destroy(obj); \n\
@ -1442,6 +1667,10 @@ static int do_subskeleton(int argc, char **argv)
printf("\t} maps;\n");
}
err = gen_st_ops_shadow(obj_name, btf, obj);
if (err)
goto out;
if (prog_cnt) {
printf("\tstruct {\n");
bpf_object__for_each_program(prog, obj) {
@ -1553,6 +1782,12 @@ static int do_subskeleton(int argc, char **argv)
if (err) \n\
goto err; \n\
\n\
");
gen_st_ops_shadow_init(btf, obj);
codegen("\
\n\
return obj; \n\
err: \n\
%1$s__destroy(obj); \n\

View File

@ -1014,6 +1014,19 @@ static bool bpf_map__is_struct_ops(const struct bpf_map *map)
return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
}
static bool is_valid_st_ops_program(struct bpf_object *obj,
const struct bpf_program *prog)
{
int i;
for (i = 0; i < obj->nr_programs; i++) {
if (&obj->programs[i] == prog)
return prog->type == BPF_PROG_TYPE_STRUCT_OPS;
}
return false;
}
/* Init the map's fields that depend on kern_btf */
static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
{
@ -1102,9 +1115,16 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
if (btf_is_ptr(mtype)) {
struct bpf_program *prog;
prog = st_ops->progs[i];
/* Update the value from the shadow type */
prog = *(void **)mdata;
st_ops->progs[i] = prog;
if (!prog)
continue;
if (!is_valid_st_ops_program(obj, prog)) {
pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n",
map->name, mname);
return -ENOTSUP;
}
kern_mtype = skip_mods_and_typedefs(kern_btf,
kern_mtype->type,
@ -1229,6 +1249,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
map->name = strdup(var_name);
if (!map->name)
return -ENOMEM;
map->btf_value_type_id = type_id;
map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
map->def.key_size = sizeof(int);
@ -4857,6 +4878,10 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
create_attr.btf_value_type_id = 0;
map->btf_key_type_id = 0;
map->btf_value_type_id = 0;
break;
case BPF_MAP_TYPE_STRUCT_OPS:
create_attr.btf_value_type_id = 0;
break;
default:
break;
}
@ -9305,7 +9330,9 @@ static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
return NULL;
}
/* Collect the reloc from ELF and populate the st_ops->progs[] */
/* Collect the reloc from ELF, populate the st_ops->progs[], and update
* st_ops->data for shadow type.
*/
static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
Elf64_Shdr *shdr, Elf_Data *data)
{
@ -9419,6 +9446,14 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
}
st_ops->progs[member_idx] = prog;
/* st_ops->data will be exposed to users, being returned by
* bpf_map__initial_value() as a pointer to the shadow
* type. All function pointers in the original struct type
* should be converted to a pointer to struct bpf_program
* in the shadow type.
*/
*((struct bpf_program **)(st_ops->data + moff)) = prog;
}
return 0;
@ -9877,6 +9912,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->def.value_size;
return map->st_ops->data;
}
if (!map->mmaped)
return NULL;
*psize = map->def.value_size;

View File

@ -539,6 +539,15 @@ 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) {
/* For data fields, this function has to copy it and return
* 1 to indicate that the data has been handled by the
* struct_ops type, or the verifier will reject the map if
* the value of the data field is not zero.
*/
((struct bpf_testmod_ops *)kdata)->data = ((struct bpf_testmod_ops *)udata)->data;
return 1;
}
return 0;
}
@ -559,7 +568,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;
}

View File

@ -35,6 +35,14 @@ 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);
/* The following fields are used to test shadow copies. */
char onebyte;
struct {
int a;
int b;
} unsupported;
int data;
};
#endif /* _BPF_TESTMOD_H */

View File

@ -32,17 +32,23 @@ cleanup:
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;
/* Since test_2() is not being used, it should be disabled from
* auto-loading, or it will fail to load.
*/
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 +62,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, "check_shadow_variables");
bpf_link__destroy(link);

View File

@ -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,
};