Skip to contentSkip to conversation

bpf: Light skeleton for the kernel.

PATCH Superseded Alexei Starovoitov 3 participants 19 messages Feb 8, 2022 – Feb 9, 2022

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover bpf: Light skeleton for the kernel.

From: Alexei Starovoitov <[email protected]>

The libbpf performs a set of complex operations to load BPF programs. With "loader program" and "CO-RE in the kernel" the loading job of libbpf was diminished. The light skeleton became lean enough to perform program loading and map creation tasks without libbpf.
It's now possible to tweak it further to make light skeleton usable out of user space and out of kernel module.
This allows bpf_preload.ko to drop user-mode-driver usage,
drop host compiler dependency, allow cross compilation and simplify the code. It's a building block toward safe and portable kernel modules.

v1->v2:
- removed redundant anon struct and added comments (Andrii's reivew)
- added Yonghong's ack
- fixed build warning when JIT is off

Alexei Starovoitov (5):
bpf: Extend sys_bpf commands for bpf_syscall programs.
libbpf: Prepare light skeleton for the kernel.
bpftool: Generalize light skeleton generation.
bpf: Update iterators.lskel.h.
bpf: Convert bpf_preload.ko to use light skeleton.

 kernel/bpf/inode.c                            |  39 +---
 kernel/bpf/preload/Kconfig                    |   9 +-
 kernel/bpf/preload/Makefile                   |  14 +-
 kernel/bpf/preload/bpf_preload.h              |   8 +-
 kernel/bpf/preload/bpf_preload_kern.c         | 119 +++++------
 kernel/bpf/preload/bpf_preload_umd_blob.S     |   7 -
 .../preload/iterators/bpf_preload_common.h    |  13 --
 kernel/bpf/preload/iterators/iterators.c      | 108 ----------
 .../bpf/preload/iterators/iterators.lskel.h   |  28 +--
 kernel/bpf/syscall.c                          |  40 +++-
 tools/bpf/bpftool/gen.c                       |  45 ++--
 tools/lib/bpf/skel_internal.h                 | 193 ++++++++++++++++--
 12 files changed, 319 insertions(+), 304 deletions(-)
 delete mode 100644 kernel/bpf/preload/bpf_preload_umd_blob.S
 delete mode 100644 kernel/bpf/preload/iterators/bpf_preload_common.h
 delete mode 100644 kernel/bpf/preload/iterators/iterators.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/5 bpf: Extend sys_bpf commands for bpf_syscall programs.

From: Alexei Starovoitov <[email protected]>

bpf_sycall programs can be used directly by the kernel modules to load programs and create maps via kernel skeleton.
. Export bpf_sys_bpf syscall wrapper to be used in kernel skeleton. . Export bpf_map_get to be used in kernel skeleton.
. Allow prog_run cmd for bpf_syscall programs with recursion check. . Enable link_create and raw_tp_open cmds.

---
 kernel/bpf/syscall.c | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)
kernel/bpf/syscall.c +34 -4
index 72ce1edde950..49f88b30662a 100644
@@ -985,6 +985,7 @@ struct bpf_map *bpf_map_get(u32 ufd)
 
 	return map;
 }
+EXPORT_SYMBOL(bpf_map_get);
 
 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
 {
@@ -4756,23 +4757,52 @@ static bool syscall_prog_is_valid_access(int off, int size,
 	return true;
 }
 
-BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
+BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
 {
+	struct bpf_prog * __maybe_unused prog;
+
 	switch (cmd) {
 	case BPF_MAP_CREATE:
 	case BPF_MAP_UPDATE_ELEM:
 	case BPF_MAP_FREEZE:
 	case BPF_PROG_LOAD:
 	case BPF_BTF_LOAD:
+	case BPF_LINK_CREATE:
+	case BPF_RAW_TRACEPOINT_OPEN:
 		break;
-	/* case BPF_PROG_TEST_RUN:
-	 * is not part of this list to prevent recursive test_run
-	 */
+#ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
+	case BPF_PROG_TEST_RUN:
+		if (attr->test.data_in || attr->test.data_out ||
+		    attr->test.ctx_out || attr->test.duration ||
+		    attr->test.repeat || attr->test.flags)
+			return -EINVAL;
+
+		prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
+		if (IS_ERR(prog))
+			return PTR_ERR(prog);
+
+		if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
+		    attr->test.ctx_size_in > U16_MAX) {
+			bpf_prog_put(prog);
+			return -EINVAL;
+		}
+
+		if (!__bpf_prog_enter_sleepable(prog)) {
+			/* recursion detected */
+			bpf_prog_put(prog);
+			return -EBUSY;
+		}
+		attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
+		__bpf_prog_exit_sleepable(prog, 0 /* bpf_prog_run does runtime stats */);
+		bpf_prog_put(prog);
+		return 0;
+#endif
 	default:
 		return -EINVAL;
 	}
 	return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
 }
+EXPORT_SYMBOL(bpf_sys_bpf);
 
 static const struct bpf_func_proto bpf_sys_bpf_proto = {
 	.func		= bpf_sys_bpf,
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/5 libbpf: Prepare light skeleton for the kernel.

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Prepare light skeleton to be used in the kernel module and in the user space. The look and feel of lskel.h is mostly the same with the difference that for user space the skel->rodata is the same pointer before and after skel_load operation, while in the kernel the skel->rodata after skel_open and the skel->rodata after skel_load are different pointers.
Typical usage of skeleton remains the same for kernel and user space:
skel = my_bpf__open();
skel->rodata->my_global_var = init_val;
err = my_bpf__load(skel);
err = my_bpf__attach(skel);
// access skel->rodata->my_global_var;
// access skel->bss->another_var;

---
 tools/lib/bpf/skel_internal.h | 193 +++++++++++++++++++++++++++++++---
 1 file changed, 176 insertions(+), 17 deletions(-)
tools/lib/bpf/skel_internal.h +176 -17
index dcd3336512d4..d16544666341 100644
@@ -3,9 +3,19 @@
 #ifndef __SKEL_INTERNAL_H
 #define __SKEL_INTERNAL_H
 
+#ifdef __KERNEL__
+#include <linux/fdtable.h>
+#include <linux/mm.h>
+#include <linux/mman.h>
+#include <linux/slab.h>
+#include <linux/bpf.h>
+#else
 #include <unistd.h>
 #include <sys/syscall.h>
 #include <sys/mman.h>
+#include <stdlib.h>
+#include "bpf.h"
+#endif
 
 #ifndef __NR_bpf
 # if defined(__mips__) && defined(_ABIO32)
@@ -25,17 +35,11 @@
  * requested during loader program generation.
  */
 struct bpf_map_desc {
-	union {
-		/* input for the loader prog */
-		struct {
-			__aligned_u64 initial_value;
-			__u32 max_entries;
-		};
-		/* output of the loader prog */
-		struct {
-			int map_fd;
-		};
-	};
+	/* output of the loader prog */
+	int map_fd;
+	/* input for the loader prog */
+	__u32 max_entries;
+	__aligned_u64 initial_value;
 };
 struct bpf_prog_desc {
 	int prog_fd;
@@ -57,12 +61,159 @@ struct bpf_load_and_run_opts {
 	const char *errstr;
 };
 
+long bpf_sys_bpf(__u32 cmd, void *attr, __u32 attr_size);
+
 static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
 			  unsigned int size)
 {
+#ifdef __KERNEL__
+	return bpf_sys_bpf(cmd, attr, size);
+#else
 	return syscall(__NR_bpf, cmd, attr, size);
+#endif
+}
+
+#ifdef __KERNEL__
+static inline int close(int fd)
+{
+	return close_fd(fd);
+}
+
+static inline void *skel_alloc(size_t size)
+{
+	return kcalloc(1, size, GFP_KERNEL);
+}
+
+static inline void skel_free(const void *p)
+{
+	kfree(p);
+}
+
+/* skel->bss/rodata maps are populated in three steps.
+ *
+ * For kernel use:
+ * skel_prep_map_data() allocates kernel memory that kernel module can directly access.
+ * skel_prep_init_value() allocates a region in user space process and copies
+ * potentially modified initial map value into it.
+ * The loader program will perform copy_from_user() from maps.rodata.initial_value.
+ * skel_finalize_map_data() sets skel->rodata to point to actual value in a bpf map and
+ * does maps.rodata.initial_value = ~0ULL to signal skel_free_map_data() that kvfree
+ * is not nessary.
+ *
+ * For user space:
+ * skel_prep_map_data() mmaps anon memory into skel->rodata that can be accessed directly.
+ * skel_prep_init_value() copies rodata pointer into map.rodata.initial_value.
+ * The loader program will perform copy_from_user() from maps.rodata.initial_value.
+ * skel_finalize_map_data() remaps bpf array map value from the kernel memory into
+ * skel->rodata address.
+ *
+ * The "bpftool gen skeleton -L" command generates lskel.h that is suitable for
+ * both kernel and user space. The generated loader program does
+ * copy_from_user() from intial_value. Therefore the vm_mmap+copy_to_user step
+ * is need when lskel is used from the kernel module.
+ */
+static inline void skel_free_map_data(void *p, __u64 addr, size_t sz)
+{
+	if (addr && addr != ~0ULL)
+		vm_munmap(addr, sz);
+	if (addr != ~0ULL)
+		kvfree(p);
+	/* When addr == ~0ULL the 'p' points to
+	 * ((struct bpf_array *)map)->value. See skel_finalize_map_data.
+	 */
+}
+
+static inline void *skel_prep_map_data(const void *val, size_t mmap_sz, size_t val_sz)
+{
+	void *addr;
+
+	addr = kvmalloc(val_sz, GFP_KERNEL);
+	if (!addr)
+		return NULL;
+	memcpy(addr, val, val_sz);
+	return addr;
+}
+
+static inline __u64 skel_prep_init_value(void **addr, size_t mmap_sz, size_t val_sz)
+{
+	__u64 ret = 0;
+	void *uaddr;
+
+	uaddr = (void *) vm_mmap(NULL, 0, mmap_sz, PROT_READ | PROT_WRITE,
+				 MAP_SHARED | MAP_ANONYMOUS, 0);
+	if (IS_ERR(uaddr))
+		goto out;
+	if (copy_to_user(uaddr, *addr, val_sz)) {
+		vm_munmap((long) uaddr, mmap_sz);
+		goto out;
+	}
+	ret = (__u64) (long) uaddr;
+out:
+	kvfree(*addr);
+	*addr = NULL;
+	return ret;
 }
 
+static inline void *skel_finalize_map_data(__u64 *addr, size_t mmap_sz, int flags, int fd)
+{
+	struct bpf_map *map;
+	void *ptr = NULL;
+
+	vm_munmap(*addr, mmap_sz);
+	*addr = ~0ULL;
+
+	map = bpf_map_get(fd);
+	if (IS_ERR(map))
+		return NULL;
+	if (map->map_type != BPF_MAP_TYPE_ARRAY)
+		goto out;
+	ptr = ((struct bpf_array *)map)->value;
+	/* the ptr stays valid, since FD is not closed */
+out:
+	bpf_map_put(map);
+	return ptr;
+}
+
+#else
+
+static inline void *skel_alloc(size_t size)
+{
+	return calloc(1, size);
+}
+
+static inline void skel_free(void *p)
+{
+	free(p);
+}
+
+static inline void skel_free_map_data(void *p, __u64 addr, size_t sz)
+{
+	munmap(p, sz);
+}
+
+static inline void *skel_prep_map_data(const void *val, size_t mmap_sz, size_t val_sz)
+{
+	void *addr;
+
+	addr = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
+		    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	if (addr == (void *) -1)
+		return NULL;
+	memcpy(addr, val, val_sz);
+	return addr;
+}
+
+static inline __u64 skel_prep_init_value(void **addr, size_t mmap_sz, size_t val_sz)
+{
+	return (__u64) (long) *addr;
+}
+
+static inline void *skel_finalize_map_data(__u64 *addr, size_t mmap_sz, int flags, int fd)
+{
+	return mmap((void *)*addr, mmap_sz, flags, MAP_SHARED | MAP_FIXED, fd, 0);
+}
+#endif
+
 static inline int skel_closenz(int fd)
 {
 	if (fd > 0)
@@ -136,22 +287,28 @@ static inline int skel_link_create(int prog_fd, int target_fd,
 	return skel_sys_bpf(BPF_LINK_CREATE, &attr, attr_sz);
 }
 
+#ifdef __KERNEL__
+#define set_err
+#else
+#define set_err err = -errno
+#endif
+
 static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
 {
 	int map_fd = -1, prog_fd = -1, key = 0, err;
 	union bpf_attr attr;
 
-	map_fd = skel_map_create(BPF_MAP_TYPE_ARRAY, "__loader.map", 4, opts->data_sz, 1);
+	err = map_fd = skel_map_create(BPF_MAP_TYPE_ARRAY, "__loader.map", 4, opts->data_sz, 1);
 	if (map_fd < 0) {
 		opts->errstr = "failed to create loader map";
-		err = -errno;
+		set_err;
 		goto out;
 	}
 
 	err = skel_map_update_elem(map_fd, &key, opts->data, 0);
 	if (err < 0) {
 		opts->errstr = "failed to update loader map";
-		err = -errno;
+		set_err;
 		goto out;
 	}
 
@@ -166,10 +323,10 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
 	attr.log_size = opts->ctx->log_size;
 	attr.log_buf = opts->ctx->log_buf;
 	attr.prog_flags = BPF_F_SLEEPABLE;
-	prog_fd = skel_sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
+	err = prog_fd = skel_sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
 	if (prog_fd < 0) {
 		opts->errstr = "failed to load loader prog";
-		err = -errno;
+		set_err;
 		goto out;
 	}
 
@@ -181,10 +338,12 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
 	if (err < 0 || (int)attr.test.retval < 0) {
 		opts->errstr = "failed to execute loader prog";
 		if (err < 0) {
-			err = -errno;
+			set_err;
 		} else {
 			err = (int)attr.test.retval;
+#ifndef __KERNEL__
 			errno = -err;
+#endif
 		}
 		goto out;
 	}
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH v2 bpf-next 2/5] libbpf: Prepare light skeleton for the kernel.

Quoted 167 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Prepare light skeleton to be used in the kernel module and in the user space. The look and feel of lskel.h is mostly the same with the difference that for user space the skel->rodata is the same pointer before and after skel_load operation, while in the kernel the skel->rodata after skel_open and the skel->rodata after skel_load are different pointers.
Typical usage of skeleton remains the same for kernel and user space:
skel = my_bpf__open();
skel->rodata->my_global_var = init_val;
err = my_bpf__load(skel);
err = my_bpf__attach(skel);
// access skel->rodata->my_global_var;
// access skel->bss->another_var;

---
  tools/lib/bpf/skel_internal.h | 193 +++++++++++++++++++++++++++++++---
  1 file changed, 176 insertions(+), 17 deletions(-)
tools/lib/bpf/skel_internal.h +105 -11
index dcd3336512d4..d16544666341 100644
@@ -3,9 +3,19 @@
  #ifndef __SKEL_INTERNAL_H
  #define __SKEL_INTERNAL_H
 
+#ifdef __KERNEL__
+#include <linux/fdtable.h>
+#include <linux/mm.h>
+#include <linux/mman.h>
+#include <linux/slab.h>
+#include <linux/bpf.h>
+#else
  #include <unistd.h>
  #include <sys/syscall.h>
  #include <sys/mman.h>
+#include <stdlib.h>
+#include "bpf.h"
+#endif
 
  #ifndef __NR_bpf
  # if defined(__mips__) && defined(_ABIO32)
@@ -25,17 +35,11 @@
   * requested during loader program generation.
   */
  struct bpf_map_desc {
-	union {
-		/* input for the loader prog */
-		struct {
-			__aligned_u64 initial_value;
-			__u32 max_entries;
-		};
-		/* output of the loader prog */
-		struct {
-			int map_fd;
-		};
-	};
+	/* output of the loader prog */
+	int map_fd;
+	/* input for the loader prog */
+	__u32 max_entries;
+	__aligned_u64 initial_value;
  };
  struct bpf_prog_desc {
  	int prog_fd;
@@ -57,12 +61,159 @@ struct bpf_load_and_run_opts {
  	const char *errstr;
  };
 
+long bpf_sys_bpf(__u32 cmd, void *attr, __u32 attr_size);
+
  static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  			  unsigned int size)
  {
+#ifdef __KERNEL__
+	return bpf_sys_bpf(cmd, attr, size);
+#else
  	return syscall(__NR_bpf, cmd, attr, size);
+#endif
+}
+
+#ifdef __KERNEL__
+static inline int close(int fd)
+{
+	return close_fd(fd);
+}
+
+static inline void *skel_alloc(size_t size)
+{
+	return kcalloc(1, size, GFP_KERNEL);
+}
+
+static inline void skel_free(const void *p)
+{
+	kfree(p);
+}
+
+/* skel->bss/rodata maps are populated in three steps.
+ *
+ * For kernel use:
+ * skel_prep_map_data() allocates kernel memory that kernel module can directly access.
+ * skel_prep_init_value() allocates a region in user space process and copies
+ * potentially modified initial map value into it.
+ * The loader program will perform copy_from_user() from maps.rodata.initial_value.
+ * skel_finalize_map_data() sets skel->rodata to point to actual value in a bpf map and
+ * does maps.rodata.initial_value = ~0ULL to signal skel_free_map_data() that kvfree
+ * is not nessary.
+ *
+ * For user space:
+ * skel_prep_map_data() mmaps anon memory into skel->rodata that can be accessed directly.
+ * skel_prep_init_value() copies rodata pointer into map.rodata.initial_value.
+ * The loader program will perform copy_from_user() from maps.rodata.initial_value.
+ * skel_finalize_map_data() remaps bpf array map value from the kernel memory into
+ * skel->rodata address.
+ *
+ * The "bpftool gen skeleton -L" command generates lskel.h that is suitable for
+ * both kernel and user space. The generated loader program does
+ * copy_from_user() from intial_value. Therefore the vm_mmap+copy_to_user step
+ * is need when lskel is used from the kernel module.
+ */
+static inline void skel_free_map_data(void *p, __u64 addr, size_t sz)
+{
+	if (addr && addr != ~0ULL)
+		vm_munmap(addr, sz);
+	if (addr != ~0ULL)
+		kvfree(p);
+	/* When addr == ~0ULL the 'p' points to
+	 * ((struct bpf_array *)map)->value. See skel_finalize_map_data.
+	 */
+}
+
+static inline void *skel_prep_map_data(const void *val, size_t mmap_sz, size_t val_sz)
+{
+	void *addr;
+
+	addr = kvmalloc(val_sz, GFP_KERNEL);
+	if (!addr)
+		return NULL;
+	memcpy(addr, val, val_sz);
+	return addr;
+}
+
+static inline __u64 skel_prep_init_value(void **addr, size_t mmap_sz, size_t val_sz)
+{
+	__u64 ret = 0;
+	void *uaddr;
+
+	uaddr = (void *) vm_mmap(NULL, 0, mmap_sz, PROT_READ | PROT_WRITE,
+				 MAP_SHARED | MAP_ANONYMOUS, 0);
+	if (IS_ERR(uaddr))
+		goto out;
+	if (copy_to_user(uaddr, *addr, val_sz)) {
+		vm_munmap((long) uaddr, mmap_sz);
+		goto out;
+	}
+	ret = (__u64) (long) uaddr;
+out:
+	kvfree(*addr);
+	*addr = NULL;
+	return ret;
  }
 
+static inline void *skel_finalize_map_data(__u64 *addr, size_t mmap_sz, int flags, int fd)
+{
+	struct bpf_map *map;

tools/lib/bpf/skel_internal.h struct bpf_load_and_run_opts { · line 169

Quoted 10 lines
tools/lib/bpf/skel_internal.h +10 -0
+	void *ptr = NULL;
+
+	vm_munmap(*addr, mmap_sz);
+	*addr = ~0ULL;
+
+	map = bpf_map_get(fd);
+	if (IS_ERR(map))
+		return NULL;
+	if (map->map_type != BPF_MAP_TYPE_ARRAY)
+		goto out;

Should we do more map validation here, e.g., max_entries = 1 and also checking value_size?

Quoted 7 lines
tools/lib/bpf/skel_internal.h +7 -0
+	ptr = ((struct bpf_array *)map)->value;
+	/* the ptr stays valid, since FD is not closed */
+out:
+	bpf_map_put(map);
+	return ptr;
+}
+

[...]

#4 lore raw

Re: [PATCH v2 bpf-next 2/5] libbpf: Prepare light skeleton for the kernel.

Quoted 171 lines · Yonghong Song
Quoted 168 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Prepare light skeleton to be used in the kernel module and in the user space. The look and feel of lskel.h is mostly the same with the difference that for user space the skel->rodata is the same pointer before and after skel_load operation, while in the kernel the skel->rodata after skel_open and the skel->rodata after skel_load are different pointers.
Typical usage of skeleton remains the same for kernel and user space:
skel = my_bpf__open();
skel->rodata->my_global_var = init_val;
err = my_bpf__load(skel);
err = my_bpf__attach(skel);
// access skel->rodata->my_global_var;
// access skel->bss->another_var;

---
  tools/lib/bpf/skel_internal.h | 193 +++++++++++++++++++++++++++++++---
  1 file changed, 176 insertions(+), 17 deletions(-)
tools/lib/bpf/skel_internal.h +110 -11
index dcd3336512d4..d16544666341 100644
@@ -3,9 +3,19 @@
  #ifndef __SKEL_INTERNAL_H
  #define __SKEL_INTERNAL_H
+#ifdef __KERNEL__
+#include <linux/fdtable.h>
+#include <linux/mm.h>
+#include <linux/mman.h>
+#include <linux/slab.h>
+#include <linux/bpf.h>
+#else
  #include <unistd.h>
  #include <sys/syscall.h>
  #include <sys/mman.h>
+#include <stdlib.h>
+#include "bpf.h"
+#endif
  #ifndef __NR_bpf
  # if defined(__mips__) && defined(_ABIO32)
@@ -25,17 +35,11 @@
   * requested during loader program generation.
   */
  struct bpf_map_desc {
-	union {
-		/* input for the loader prog */
-		struct {
-			__aligned_u64 initial_value;
-			__u32 max_entries;
-		};
-		/* output of the loader prog */
-		struct {
-			int map_fd;
-		};
-	};
+	/* output of the loader prog */
+	int map_fd;
+	/* input for the loader prog */
+	__u32 max_entries;
+	__aligned_u64 initial_value;
  };
  struct bpf_prog_desc {
  	int prog_fd;
@@ -57,12 +61,159 @@ struct bpf_load_and_run_opts {
  	const char *errstr;
  };
+long bpf_sys_bpf(__u32 cmd, void *attr, __u32 attr_size);
+
  static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  			  unsigned int size)
  {
+#ifdef __KERNEL__
+	return bpf_sys_bpf(cmd, attr, size);
+#else
  	return syscall(__NR_bpf, cmd, attr, size);
+#endif
+}
+
+#ifdef __KERNEL__
+static inline int close(int fd)
+{
+	return close_fd(fd);
+}
+
+static inline void *skel_alloc(size_t size)
+{
+	return kcalloc(1, size, GFP_KERNEL);
+}
+
+static inline void skel_free(const void *p)
+{
+	kfree(p);
+}
+
+/* skel->bss/rodata maps are populated in three steps.
+ *
+ * For kernel use:
+ * skel_prep_map_data() allocates kernel memory that kernel module can directly access.
+ * skel_prep_init_value() allocates a region in user space process and copies
+ * potentially modified initial map value into it.
+ * The loader program will perform copy_from_user() from maps.rodata.initial_value.
+ * skel_finalize_map_data() sets skel->rodata to point to actual value in a bpf map and
+ * does maps.rodata.initial_value = ~0ULL to signal skel_free_map_data() that kvfree
+ * is not nessary.
+ *
+ * For user space:
+ * skel_prep_map_data() mmaps anon memory into skel->rodata that can be accessed directly.
+ * skel_prep_init_value() copies rodata pointer into map.rodata.initial_value.
+ * The loader program will perform copy_from_user() from maps.rodata.initial_value.
+ * skel_finalize_map_data() remaps bpf array map value from the kernel memory into
+ * skel->rodata address.
+ *
+ * The "bpftool gen skeleton -L" command generates lskel.h that is suitable for
+ * both kernel and user space. The generated loader program does
+ * copy_from_user() from intial_value. Therefore the vm_mmap+copy_to_user step
+ * is need when lskel is used from the kernel module.
+ */
+static inline void skel_free_map_data(void *p, __u64 addr, size_t sz)
+{
+	if (addr && addr != ~0ULL)
+		vm_munmap(addr, sz);
+	if (addr != ~0ULL)
+		kvfree(p);
+	/* When addr == ~0ULL the 'p' points to
+	 * ((struct bpf_array *)map)->value. See skel_finalize_map_data.
+	 */
+}
+
+static inline void *skel_prep_map_data(const void *val, size_t mmap_sz, size_t val_sz)
+{
+	void *addr;
+
+	addr = kvmalloc(val_sz, GFP_KERNEL);
+	if (!addr)
+		return NULL;
+	memcpy(addr, val, val_sz);
+	return addr;
+}
+
+static inline __u64 skel_prep_init_value(void **addr, size_t mmap_sz, size_t val_sz)
+{
+	__u64 ret = 0;
+	void *uaddr;
+
+	uaddr = (void *) vm_mmap(NULL, 0, mmap_sz, PROT_READ | PROT_WRITE,
+				 MAP_SHARED | MAP_ANONYMOUS, 0);
+	if (IS_ERR(uaddr))
+		goto out;
+	if (copy_to_user(uaddr, *addr, val_sz)) {
+		vm_munmap((long) uaddr, mmap_sz);
+		goto out;
+	}
+	ret = (__u64) (long) uaddr;
+out:
+	kvfree(*addr);
+	*addr = NULL;
+	return ret;
  }
+static inline void *skel_finalize_map_data(__u64 *addr, size_t mmap_sz, int flags, int fd)
+{
+	struct bpf_map *map;
+	void *ptr = NULL;
+
+	vm_munmap(*addr, mmap_sz);
+	*addr = ~0ULL;
+
Quoted 8 lines

tools/lib/bpf/skel_internal.h struct bpf_load_and_run_opts { · line 169

Quoted 5 lines
tools/lib/bpf/skel_internal.h +5 -0
+	map = bpf_map_get(fd);
+	if (IS_ERR(map))
+		return NULL;
+	if (map->map_type != BPF_MAP_TYPE_ARRAY)
+		goto out;

Should we do more map validation here, e.g., max_entries = 1 and also checking value_size?

The map_type check is a sanity check.
It should be valid by construction of loader prog.
The map is also mmap-able and when signed progs will come to life it will be frozen and signature checked.
rodata map should be readonly too, but ((struct bpf_array *)map)->value direct access assumes that the kernel module won't mess with the values. imo map_type check is enough. More checks feels like overkill.

Acked-by
#5 lore raw

Re: [PATCH v2 bpf-next 2/5] libbpf: Prepare light skeleton for the kernel.

Quoted 181 lines · Alexei Starovoitov
Quoted 171 lines · Yonghong Song
Quoted 168 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Prepare light skeleton to be used in the kernel module and in the user space. The look and feel of lskel.h is mostly the same with the difference that for user space the skel->rodata is the same pointer before and after skel_load operation, while in the kernel the skel->rodata after skel_open and the skel->rodata after skel_load are different pointers.
Typical usage of skeleton remains the same for kernel and user space:
skel = my_bpf__open();
skel->rodata->my_global_var = init_val;
err = my_bpf__load(skel);
err = my_bpf__attach(skel);
// access skel->rodata->my_global_var;
// access skel->bss->another_var;

---
   tools/lib/bpf/skel_internal.h | 193 +++++++++++++++++++++++++++++++---
   1 file changed, 176 insertions(+), 17 deletions(-)
tools/lib/bpf/skel_internal.h +110 -11
index dcd3336512d4..d16544666341 100644
@@ -3,9 +3,19 @@
   #ifndef __SKEL_INTERNAL_H
   #define __SKEL_INTERNAL_H
+#ifdef __KERNEL__
+#include <linux/fdtable.h>
+#include <linux/mm.h>
+#include <linux/mman.h>
+#include <linux/slab.h>
+#include <linux/bpf.h>
+#else
   #include <unistd.h>
   #include <sys/syscall.h>
   #include <sys/mman.h>
+#include <stdlib.h>
+#include "bpf.h"
+#endif
   #ifndef __NR_bpf
   # if defined(__mips__) && defined(_ABIO32)
@@ -25,17 +35,11 @@
    * requested during loader program generation.
    */
   struct bpf_map_desc {
-	union {
-		/* input for the loader prog */
-		struct {
-			__aligned_u64 initial_value;
-			__u32 max_entries;
-		};
-		/* output of the loader prog */
-		struct {
-			int map_fd;
-		};
-	};
+	/* output of the loader prog */
+	int map_fd;
+	/* input for the loader prog */
+	__u32 max_entries;
+	__aligned_u64 initial_value;
   };
   struct bpf_prog_desc {
   	int prog_fd;
@@ -57,12 +61,159 @@ struct bpf_load_and_run_opts {
   	const char *errstr;
   };
+long bpf_sys_bpf(__u32 cmd, void *attr, __u32 attr_size);
+
   static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
   			  unsigned int size)
   {
+#ifdef __KERNEL__
+	return bpf_sys_bpf(cmd, attr, size);
+#else
   	return syscall(__NR_bpf, cmd, attr, size);
+#endif
+}
+
+#ifdef __KERNEL__
+static inline int close(int fd)
+{
+	return close_fd(fd);
+}
+
+static inline void *skel_alloc(size_t size)
+{
+	return kcalloc(1, size, GFP_KERNEL);
+}
+
+static inline void skel_free(const void *p)
+{
+	kfree(p);
+}
+
+/* skel->bss/rodata maps are populated in three steps.
+ *
+ * For kernel use:
+ * skel_prep_map_data() allocates kernel memory that kernel module can directly access.
+ * skel_prep_init_value() allocates a region in user space process and copies
+ * potentially modified initial map value into it.
+ * The loader program will perform copy_from_user() from maps.rodata.initial_value.
+ * skel_finalize_map_data() sets skel->rodata to point to actual value in a bpf map and
+ * does maps.rodata.initial_value = ~0ULL to signal skel_free_map_data() that kvfree
+ * is not nessary.
+ *
+ * For user space:
+ * skel_prep_map_data() mmaps anon memory into skel->rodata that can be accessed directly.
+ * skel_prep_init_value() copies rodata pointer into map.rodata.initial_value.
+ * The loader program will perform copy_from_user() from maps.rodata.initial_value.
+ * skel_finalize_map_data() remaps bpf array map value from the kernel memory into
+ * skel->rodata address.
+ *
+ * The "bpftool gen skeleton -L" command generates lskel.h that is suitable for
+ * both kernel and user space. The generated loader program does
+ * copy_from_user() from intial_value. Therefore the vm_mmap+copy_to_user step
+ * is need when lskel is used from the kernel module.
+ */
+static inline void skel_free_map_data(void *p, __u64 addr, size_t sz)
+{
+	if (addr && addr != ~0ULL)
+		vm_munmap(addr, sz);
+	if (addr != ~0ULL)
+		kvfree(p);
+	/* When addr == ~0ULL the 'p' points to
+	 * ((struct bpf_array *)map)->value. See skel_finalize_map_data.
+	 */
+}
+
+static inline void *skel_prep_map_data(const void *val, size_t mmap_sz, size_t val_sz)
+{
+	void *addr;
+
+	addr = kvmalloc(val_sz, GFP_KERNEL);
+	if (!addr)
+		return NULL;
+	memcpy(addr, val, val_sz);
+	return addr;
+}
+
+static inline __u64 skel_prep_init_value(void **addr, size_t mmap_sz, size_t val_sz)
+{
+	__u64 ret = 0;
+	void *uaddr;
+
+	uaddr = (void *) vm_mmap(NULL, 0, mmap_sz, PROT_READ | PROT_WRITE,
+				 MAP_SHARED | MAP_ANONYMOUS, 0);
+	if (IS_ERR(uaddr))
+		goto out;
+	if (copy_to_user(uaddr, *addr, val_sz)) {
+		vm_munmap((long) uaddr, mmap_sz);
+		goto out;
+	}
+	ret = (__u64) (long) uaddr;
+out:
+	kvfree(*addr);
+	*addr = NULL;
+	return ret;
   }
+static inline void *skel_finalize_map_data(__u64 *addr, size_t mmap_sz, int flags, int fd)
+{
+	struct bpf_map *map;
+	void *ptr = NULL;
+
+	vm_munmap(*addr, mmap_sz);
+	*addr = ~0ULL;
+
Quoted 8 lines

tools/lib/bpf/skel_internal.h struct bpf_load_and_run_opts { · line 169

Quoted 5 lines
tools/lib/bpf/skel_internal.h +5 -0
+	map = bpf_map_get(fd);
+	if (IS_ERR(map))
+		return NULL;
+	if (map->map_type != BPF_MAP_TYPE_ARRAY)
+		goto out;

Should we do more map validation here, e.g., max_entries = 1 and also checking value_size?

Quoted 1 line

The map_type check is a sanity check.

Yes, I am aware of this as a sanity check. I am just wondering if we do this check whether we should do other checks as well.

Quoted 6 lines

It should be valid by construction of loader prog.
The map is also mmap-able and when signed progs will come to life it will be frozen and signature checked.
rodata map should be readonly too, but ((struct bpf_array *)map)->value direct access assumes that the kernel module won't mess with the values. imo map_type check is enough. More checks feels like overkill.

I am okay with this. Maybe add a comment right before bpf_map_get() to explain map with fd is created by loader prog so map should be a valid array map with max_entries 1, the IS_ERR and map_type checks are just sanity checks.

With this,

#6 lore raw

PATCH v2 3/5 bpftool: Generalize light skeleton generation.

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Generealize light skeleton by hiding mmap details in skel_internal.h In this form generated lskel.h is usable both by user space and by the kernel.

---
 tools/bpf/bpftool/gen.c | 45 ++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 18 deletions(-)
tools/bpf/bpftool/gen.c +27 -18
index eacfc6a2060d..903abbf077ce 100644
@@ -472,7 +472,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
 			continue;
 		if (bpf_map__is_internal(map) &&
 		    (bpf_map__map_flags(map) & BPF_F_MMAPABLE))
-			printf("\tmunmap(skel->%1$s, %2$zd);\n",
+			printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
 			       ident, bpf_map_mmap_sz(map));
 		codegen("\
 			\n\
@@ -481,7 +481,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
 	}
 	codegen("\
 		\n\
-			free(skel);					    \n\
+			skel_free(skel);				    \n\
 		}							    \n\
 		",
 		obj_name);
@@ -525,7 +525,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 		{							    \n\
 			struct %1$s *skel;				    \n\
 									    \n\
-			skel = calloc(sizeof(*skel), 1);		    \n\
+			skel = skel_alloc(sizeof(*skel));		    \n\
 			if (!skel)					    \n\
 				goto cleanup;				    \n\
 			skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\
@@ -544,18 +544,12 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 
 		codegen("\
 			\n\
-				skel->%1$s =					 \n\
-					mmap(NULL, %2$zd, PROT_READ | PROT_WRITE,\n\
-					     MAP_SHARED | MAP_ANONYMOUS, -1, 0); \n\
-				if (skel->%1$s == (void *) -1)			 \n\
-					goto cleanup;				 \n\
-				memcpy(skel->%1$s, (void *)\"\\			 \n\
-			", ident, bpf_map_mmap_sz(map));
+				skel->%1$s = skel_prep_map_data((void *)\"\\	 \n\
+			", ident);
 		mmap_data = bpf_map__initial_value(map, &mmap_size);
 		print_hex(mmap_data, mmap_size);
-		printf("\", %2$zd);\n"
-		       "\tskel->maps.%1$s.initial_value = (__u64)(long)skel->%1$s;\n",
-		       ident, mmap_size);
+		printf("\", %1$zd, %2$zd);\n",
+		       bpf_map_mmap_sz(map), mmap_size);
 	}
 	codegen("\
 		\n\
@@ -592,6 +586,24 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 	codegen("\
 		\n\
 		\";							    \n\
+		");
+	bpf_object__for_each_map(map, obj) {
+		size_t mmap_size = 0;
+
+		if (!get_map_ident(map, ident, sizeof(ident)))
+			continue;
+
+		if (!bpf_map__is_internal(map) ||
+		    !(bpf_map__map_flags(map) & BPF_F_MMAPABLE))
+			continue;
+
+		bpf_map__initial_value(map, &mmap_size);
+		printf("\tskel->maps.%1$s.initial_value ="
+		       " skel_prep_init_value((void **)&skel->%1$s, %2$zd, %3$zd);\n",
+		       ident, bpf_map_mmap_sz(map), mmap_size);
+	}
+	codegen("\
+		\n\
 			err = bpf_load_and_run(&opts);			    \n\
 			if (err < 0)					    \n\
 				return err;				    \n\
@@ -611,9 +623,8 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 		else
 			mmap_flags = "PROT_READ | PROT_WRITE";
 
-		printf("\tskel->%1$s =\n"
-		       "\t\tmmap(skel->%1$s, %2$zd, %3$s, MAP_SHARED | MAP_FIXED,\n"
-		       "\t\t\tskel->maps.%1$s.map_fd, 0);\n",
+		printf("\tskel->%1$s = skel_finalize_map_data(&skel->maps.%1$s.initial_value,\n"
+		       "\t\t\t%2$zd, %3$s, skel->maps.%1$s.map_fd);\n",
 		       ident, bpf_map_mmap_sz(map), mmap_flags);
 	}
 	codegen("\
@@ -751,8 +762,6 @@ static int do_skeleton(int argc, char **argv)
 		#ifndef %2$s						    \n\
 		#define %2$s						    \n\
 									    \n\
-		#include <stdlib.h>					    \n\
-		#include <bpf/bpf.h>					    \n\
 		#include <bpf/skel_internal.h>				    \n\
 									    \n\
 		struct %1$s {						    \n\
Signature · 1 line
2.30.2
#7 lore raw

Re: [PATCH v2 bpf-next 3/5] bpftool: Generalize light skeleton generation.

Quoted 102 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Generealize light skeleton by hiding mmap details in skel_internal.h In this form generated lskel.h is usable both by user space and by the kernel.

---
  tools/bpf/bpftool/gen.c | 45 ++++++++++++++++++++++++-----------------
  1 file changed, 27 insertions(+), 18 deletions(-)
tools/bpf/bpftool/gen.c +27 -16
index eacfc6a2060d..903abbf077ce 100644
@@ -472,7 +472,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
  			continue;
  		if (bpf_map__is_internal(map) &&
  		    (bpf_map__map_flags(map) & BPF_F_MMAPABLE))
-			printf("\tmunmap(skel->%1$s, %2$zd);\n",
+			printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
  			       ident, bpf_map_mmap_sz(map));
  		codegen("\
  			\n\
@@ -481,7 +481,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
  	}
  	codegen("\
  		\n\
-			free(skel);					    \n\
+			skel_free(skel);				    \n\
  		}							    \n\
  		",
  		obj_name);
@@ -525,7 +525,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
  		{							    \n\
  			struct %1$s *skel;				    \n\
  									    \n\
-			skel = calloc(sizeof(*skel), 1);		    \n\
+			skel = skel_alloc(sizeof(*skel));		    \n\
  			if (!skel)					    \n\
  				goto cleanup;				    \n\
  			skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\
@@ -544,18 +544,12 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 
  		codegen("\
  			\n\
-				skel->%1$s =					 \n\
-					mmap(NULL, %2$zd, PROT_READ | PROT_WRITE,\n\
-					     MAP_SHARED | MAP_ANONYMOUS, -1, 0); \n\
-				if (skel->%1$s == (void *) -1)			 \n\
-					goto cleanup;				 \n\
-				memcpy(skel->%1$s, (void *)\"\\			 \n\
-			", ident, bpf_map_mmap_sz(map));
+				skel->%1$s = skel_prep_map_data((void *)\"\\	 \n\
+			", ident);
  		mmap_data = bpf_map__initial_value(map, &mmap_size);
  		print_hex(mmap_data, mmap_size);
-		printf("\", %2$zd);\n"
-		       "\tskel->maps.%1$s.initial_value = (__u64)(long)skel->%1$s;\n",
-		       ident, mmap_size);
+		printf("\", %1$zd, %2$zd);\n",
+		       bpf_map_mmap_sz(map), mmap_size);
  	}
  	codegen("\
  		\n\
@@ -592,6 +586,24 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
  	codegen("\
  		\n\
  		\";							    \n\
+		");
+	bpf_object__for_each_map(map, obj) {
+		size_t mmap_size = 0;
+
+		if (!get_map_ident(map, ident, sizeof(ident)))
+			continue;
+
+		if (!bpf_map__is_internal(map) ||
+		    !(bpf_map__map_flags(map) & BPF_F_MMAPABLE))
+			continue;
+
+		bpf_map__initial_value(map, &mmap_size);
+		printf("\tskel->maps.%1$s.initial_value ="
+		       " skel_prep_init_value((void **)&skel->%1$s, %2$zd, %3$zd);\n",
+		       ident, bpf_map_mmap_sz(map), mmap_size);
+	}
+	codegen("\
+		\n\
  			err = bpf_load_and_run(&opts);			    \n\
  			if (err < 0)					    \n\
  				return err;				    \n\
@@ -611,9 +623,8 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
  		else
  			mmap_flags = "PROT_READ | PROT_WRITE";
 
-		printf("\tskel->%1$s =\n"
-		       "\t\tmmap(skel->%1$s, %2$zd, %3$s, MAP_SHARED | MAP_FIXED,\n"
-		       "\t\t\tskel->maps.%1$s.map_fd, 0);\n",
+		printf("\tskel->%1$s = skel_finalize_map_data(&skel->maps.%1$s.initial_value,\n"
+		       "\t\t\t%2$zd, %3$s, skel->maps.%1$s.map_fd);\n",
  		       ident, bpf_map_mmap_sz(map), mmap_flags);
  	}
  	codegen("\

tools/bpf/bpftool/gen.c static int do_skeleton(int argc, char **argv) · line 755

Quoted 5 lines
tools/bpf/bpftool/gen.c +0 -2
@@ -751,8 +762,6 @@ static int do_skeleton(int argc, char **argv)
  		#ifndef %2$s						    \n\
  		#define %2$s						    \n\
  									    \n\
-		#include <stdlib.h>					    \n\
-		#include <bpf/bpf.h>					    \n\

I noticed that in patch2, the "bpf.h" is used instead of <bpf/bpf.h>. Any particular reason for this or it is a bug fix?

Quoted 3 lines
tools/bpf/bpftool/gen.c +0 -0
  		#include <bpf/skel_internal.h>				    \n\
  									    \n\
  		struct %1$s {						    \n\
#8 lore raw

Re: [PATCH v2 bpf-next 3/5] bpftool: Generalize light skeleton generation.

Quoted 103 lines · Yonghong Song
Quoted 100 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Generealize light skeleton by hiding mmap details in skel_internal.h In this form generated lskel.h is usable both by user space and by the kernel.

---
  tools/bpf/bpftool/gen.c | 45 ++++++++++++++++++++++++-----------------
  1 file changed, 27 insertions(+), 18 deletions(-)
tools/bpf/bpftool/gen.c +27 -16
index eacfc6a2060d..903abbf077ce 100644
@@ -472,7 +472,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
  			continue;
  		if (bpf_map__is_internal(map) &&
  		    (bpf_map__map_flags(map) & BPF_F_MMAPABLE))
-			printf("\tmunmap(skel->%1$s, %2$zd);\n",
+			printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
  			       ident, bpf_map_mmap_sz(map));
  		codegen("\
  			\n\
@@ -481,7 +481,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
  	}
  	codegen("\
  		\n\
-			free(skel);					    \n\
+			skel_free(skel);				    \n\
  		}							    \n\
  		",
  		obj_name);
@@ -525,7 +525,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
  		{							    \n\
  			struct %1$s *skel;				    \n\
  									    \n\
-			skel = calloc(sizeof(*skel), 1);		    \n\
+			skel = skel_alloc(sizeof(*skel));		    \n\
  			if (!skel)					    \n\
  				goto cleanup;				    \n\
  			skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\
@@ -544,18 +544,12 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
  		codegen("\
  			\n\
-				skel->%1$s =					 \n\
-					mmap(NULL, %2$zd, PROT_READ | PROT_WRITE,\n\
-					     MAP_SHARED | MAP_ANONYMOUS, -1, 0); \n\
-				if (skel->%1$s == (void *) -1)			 \n\
-					goto cleanup;				 \n\
-				memcpy(skel->%1$s, (void *)\"\\			 \n\
-			", ident, bpf_map_mmap_sz(map));
+				skel->%1$s = skel_prep_map_data((void *)\"\\	 \n\
+			", ident);
  		mmap_data = bpf_map__initial_value(map, &mmap_size);
  		print_hex(mmap_data, mmap_size);
-		printf("\", %2$zd);\n"
-		       "\tskel->maps.%1$s.initial_value = (__u64)(long)skel->%1$s;\n",
-		       ident, mmap_size);
+		printf("\", %1$zd, %2$zd);\n",
+		       bpf_map_mmap_sz(map), mmap_size);
  	}
  	codegen("\
  		\n\
@@ -592,6 +586,24 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
  	codegen("\
  		\n\
  		\";							    \n\
+		");
+	bpf_object__for_each_map(map, obj) {
+		size_t mmap_size = 0;
+
+		if (!get_map_ident(map, ident, sizeof(ident)))
+			continue;
+
+		if (!bpf_map__is_internal(map) ||
+		    !(bpf_map__map_flags(map) & BPF_F_MMAPABLE))
+			continue;
+
+		bpf_map__initial_value(map, &mmap_size);
+		printf("\tskel->maps.%1$s.initial_value ="
+		       " skel_prep_init_value((void **)&skel->%1$s, %2$zd, %3$zd);\n",
+		       ident, bpf_map_mmap_sz(map), mmap_size);
+	}
+	codegen("\
+		\n\
  			err = bpf_load_and_run(&opts);			    \n\
  			if (err < 0)					    \n\
  				return err;				    \n\
@@ -611,9 +623,8 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
  		else
  			mmap_flags = "PROT_READ | PROT_WRITE";
-		printf("\tskel->%1$s =\n"
-		       "\t\tmmap(skel->%1$s, %2$zd, %3$s, MAP_SHARED | MAP_FIXED,\n"
-		       "\t\t\tskel->maps.%1$s.map_fd, 0);\n",
+		printf("\tskel->%1$s = skel_finalize_map_data(&skel->maps.%1$s.initial_value,\n"
+		       "\t\t\t%2$zd, %3$s, skel->maps.%1$s.map_fd);\n",
  		       ident, bpf_map_mmap_sz(map), mmap_flags);
  	}
  	codegen("\
@@ -751,8 +762,6 @@ static int do_skeleton(int argc, char **argv)
Quoted 8 lines

tools/bpf/bpftool/gen.c static int do_skeleton(int argc, char **argv) · line 755

Quoted 5 lines
tools/bpf/bpftool/gen.c +0 -2
  		#ifndef %2$s						    \n\
  		#define %2$s						    \n\
  									    \n\
-		#include <stdlib.h>					    \n\
-		#include <bpf/bpf.h>					    \n\

I noticed that in patch2, the "bpf.h" is used instead of <bpf/bpf.h>. Any particular reason for this or it is a bug fix?

skel_internal.h didn't include bpf.h directly.
gen_loader.c needs it. It does:
#include "skel_internal.h"
because gen_loader.c is part of libbpf.
libbpf sources cannot do #include <bpf/...>

If skel_internal.h did
#include <bpf/bpf.h>
there would be a build error:
In file included from gen_loader.c:15:
skel_internal.h:17:10: fatal error: bpf/bpf.h: No such file or directory
#include <bpf/bpf.h>

Hence #include "bpf.h" in skel_internal.h
So it works for libbpf's gen_loader.c and for generated lskel.h too.

Acked-by
#9 lore raw

Re: [PATCH v2 bpf-next 3/5] bpftool: Generalize light skeleton generation.

Quoted 126 lines · Alexei Starovoitov
Quoted 103 lines · Yonghong Song
Quoted 100 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Generealize light skeleton by hiding mmap details in skel_internal.h In this form generated lskel.h is usable both by user space and by the kernel.

---
   tools/bpf/bpftool/gen.c | 45 ++++++++++++++++++++++++-----------------
   1 file changed, 27 insertions(+), 18 deletions(-)
tools/bpf/bpftool/gen.c +27 -16
index eacfc6a2060d..903abbf077ce 100644
@@ -472,7 +472,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
   			continue;
   		if (bpf_map__is_internal(map) &&
   		    (bpf_map__map_flags(map) & BPF_F_MMAPABLE))
-			printf("\tmunmap(skel->%1$s, %2$zd);\n",
+			printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
   			       ident, bpf_map_mmap_sz(map));
   		codegen("\
   			\n\
@@ -481,7 +481,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
   	}
   	codegen("\
   		\n\
-			free(skel);					    \n\
+			skel_free(skel);				    \n\
   		}							    \n\
   		",
   		obj_name);
@@ -525,7 +525,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
   		{							    \n\
   			struct %1$s *skel;				    \n\
   									    \n\
-			skel = calloc(sizeof(*skel), 1);		    \n\
+			skel = skel_alloc(sizeof(*skel));		    \n\
   			if (!skel)					    \n\
   				goto cleanup;				    \n\
   			skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\
@@ -544,18 +544,12 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
   		codegen("\
   			\n\
-				skel->%1$s =					 \n\
-					mmap(NULL, %2$zd, PROT_READ | PROT_WRITE,\n\
-					     MAP_SHARED | MAP_ANONYMOUS, -1, 0); \n\
-				if (skel->%1$s == (void *) -1)			 \n\
-					goto cleanup;				 \n\
-				memcpy(skel->%1$s, (void *)\"\\			 \n\
-			", ident, bpf_map_mmap_sz(map));
+				skel->%1$s = skel_prep_map_data((void *)\"\\	 \n\
+			", ident);
   		mmap_data = bpf_map__initial_value(map, &mmap_size);
   		print_hex(mmap_data, mmap_size);
-		printf("\", %2$zd);\n"
-		       "\tskel->maps.%1$s.initial_value = (__u64)(long)skel->%1$s;\n",
-		       ident, mmap_size);
+		printf("\", %1$zd, %2$zd);\n",
+		       bpf_map_mmap_sz(map), mmap_size);
   	}
   	codegen("\
   		\n\
@@ -592,6 +586,24 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
   	codegen("\
   		\n\
   		\";							    \n\
+		");
+	bpf_object__for_each_map(map, obj) {
+		size_t mmap_size = 0;
+
+		if (!get_map_ident(map, ident, sizeof(ident)))
+			continue;
+
+		if (!bpf_map__is_internal(map) ||
+		    !(bpf_map__map_flags(map) & BPF_F_MMAPABLE))
+			continue;
+
+		bpf_map__initial_value(map, &mmap_size);
+		printf("\tskel->maps.%1$s.initial_value ="
+		       " skel_prep_init_value((void **)&skel->%1$s, %2$zd, %3$zd);\n",
+		       ident, bpf_map_mmap_sz(map), mmap_size);
+	}
+	codegen("\
+		\n\
   			err = bpf_load_and_run(&opts);			    \n\
   			if (err < 0)					    \n\
   				return err;				    \n\
@@ -611,9 +623,8 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
   		else
   			mmap_flags = "PROT_READ | PROT_WRITE";
-		printf("\tskel->%1$s =\n"
-		       "\t\tmmap(skel->%1$s, %2$zd, %3$s, MAP_SHARED | MAP_FIXED,\n"
-		       "\t\t\tskel->maps.%1$s.map_fd, 0);\n",
+		printf("\tskel->%1$s = skel_finalize_map_data(&skel->maps.%1$s.initial_value,\n"
+		       "\t\t\t%2$zd, %3$s, skel->maps.%1$s.map_fd);\n",
   		       ident, bpf_map_mmap_sz(map), mmap_flags);
   	}
   	codegen("\
@@ -751,8 +762,6 @@ static int do_skeleton(int argc, char **argv)
Quoted 8 lines

tools/bpf/bpftool/gen.c static int do_skeleton(int argc, char **argv) · line 755

Quoted 5 lines
tools/bpf/bpftool/gen.c +0 -2
   		#ifndef %2$s						    \n\
   		#define %2$s						    \n\
   									    \n\
-		#include <stdlib.h>					    \n\
-		#include <bpf/bpf.h>					    \n\

I noticed that in patch2, the "bpf.h" is used instead of <bpf/bpf.h>. Any particular reason for this or it is a bug fix?

skel_internal.h didn't include bpf.h directly.
gen_loader.c needs it. It does:
#include "skel_internal.h"
because gen_loader.c is part of libbpf.
libbpf sources cannot do #include <bpf/...>

If skel_internal.h did
#include <bpf/bpf.h>
there would be a build error:
In file included from gen_loader.c:15:
skel_internal.h:17:10: fatal error: bpf/bpf.h: No such file or directory
#include <bpf/bpf.h>

Quoted 2 lines

Hence #include "bpf.h" in skel_internal.h
So it works for libbpf's gen_loader.c and for generated lskel.h too.

Okay, now I understand, previously <bpf/bpf.h> is in *.lskel.h file, which has nothing to do with gen_loader.c, and now the bpf.h header needs to be in skel_internal.h which will impact gen_loader.c compilation.

#10 lore raw

PATCH v2 4/5 bpf: Update iterators.lskel.h.

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Light skeleton and skel_internal.h have changed.
Update iterators.lskel.h.

---
 .../bpf/preload/iterators/iterators.lskel.h   | 28 +++++++------------
 1 file changed, 10 insertions(+), 18 deletions(-)
kernel/bpf/preload/iterators/iterators.lskel.h +10 -18
index d90562d672d2..3e45237f59f4 100644
@@ -3,8 +3,6 @@
 #ifndef __ITERATORS_BPF_SKEL_H__
 #define __ITERATORS_BPF_SKEL_H__
 
-#include <stdlib.h>
-#include <bpf/bpf.h>
 #include <bpf/skel_internal.h>
 
 struct iterators_bpf {
@@ -70,31 +68,25 @@ iterators_bpf__destroy(struct iterators_bpf *skel)
 	iterators_bpf__detach(skel);
 	skel_closenz(skel->progs.dump_bpf_map.prog_fd);
 	skel_closenz(skel->progs.dump_bpf_prog.prog_fd);
-	munmap(skel->rodata, 4096);
+	skel_free_map_data(skel->rodata, skel->maps.rodata.initial_value, 4096);
 	skel_closenz(skel->maps.rodata.map_fd);
-	free(skel);
+	skel_free(skel);
 }
 static inline struct iterators_bpf *
 iterators_bpf__open(void)
 {
 	struct iterators_bpf *skel;
 
-	skel = calloc(sizeof(*skel), 1);
+	skel = skel_alloc(sizeof(*skel));
 	if (!skel)
 		goto cleanup;
 	skel->ctx.sz = (void *)&skel->links - (void *)skel;
-	skel->rodata =
-		mmap(NULL, 4096, PROT_READ | PROT_WRITE,
-		     MAP_SHARED | MAP_ANONYMOUS, -1, 0);
-	if (skel->rodata == (void *) -1)
-		goto cleanup;
-	memcpy(skel->rodata, (void *)"\
+	skel->rodata = skel_prep_map_data((void *)"\
 \x20\x20\x69\x64\x20\x6e\x61\x6d\x65\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\
 \x20\x20\x20\x6d\x61\x78\x5f\x65\x6e\x74\x72\x69\x65\x73\x0a\0\x25\x34\x75\x20\
 \x25\x2d\x31\x36\x73\x25\x36\x64\x0a\0\x20\x20\x69\x64\x20\x6e\x61\x6d\x65\x20\
 \x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x61\x74\x74\x61\x63\x68\x65\
-\x64\x0a\0\x25\x34\x75\x20\x25\x2d\x31\x36\x73\x20\x25\x73\x20\x25\x73\x0a\0", 98);
-	skel->maps.rodata.initial_value = (__u64)(long)skel->rodata;
+\x64\x0a\0\x25\x34\x75\x20\x25\x2d\x31\x36\x73\x20\x25\x73\x20\x25\x73\x0a\0", 4096, 98);
 	return skel;
 cleanup:
 	iterators_bpf__destroy(skel);
@@ -343,11 +335,11 @@ iterators_bpf__load(struct iterators_bpf *skel)
 \0\0\x18\x62\0\0\0\0\0\0\0\0\0\0\x30\x0e\0\0\xb7\x03\0\0\x1c\0\0\0\x85\0\0\0\
 \xa6\0\0\0\xbf\x07\0\0\0\0\0\0\xc5\x07\xd4\xff\0\0\0\0\x63\x7a\x78\xff\0\0\0\0\
 \x61\xa0\x78\xff\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\x80\x0e\0\0\x63\x01\0\0\0\
-\0\0\0\x61\x60\x20\0\0\0\0\0\x15\0\x03\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\
+\0\0\0\x61\x60\x1c\0\0\0\0\0\x15\0\x03\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\
 \x5c\x0e\0\0\x63\x01\0\0\0\0\0\0\xb7\x01\0\0\0\0\0\0\x18\x62\0\0\0\0\0\0\0\0\0\
 \0\x50\x0e\0\0\xb7\x03\0\0\x48\0\0\0\x85\0\0\0\xa6\0\0\0\xbf\x07\0\0\0\0\0\0\
 \xc5\x07\xc3\xff\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x63\x71\0\0\0\0\0\
-\0\x79\x63\x18\0\0\0\0\0\x15\x03\x04\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\x98\
+\0\x79\x63\x20\0\0\0\0\0\x15\x03\x04\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\x98\
 \x0e\0\0\xb7\x02\0\0\x62\0\0\0\x85\0\0\0\x94\0\0\0\x18\x62\0\0\0\0\0\0\0\0\0\0\
 \0\0\0\0\x61\x20\0\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\x08\x0f\0\0\x63\x01\0\
 \0\0\0\0\0\x18\x60\0\0\0\0\0\0\0\0\0\0\0\x0f\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\
@@ -401,12 +393,12 @@ iterators_bpf__load(struct iterators_bpf *skel)
 \x28\0\0\0\0\0\x61\xa0\x84\xff\0\0\0\0\x63\x06\x2c\0\0\0\0\0\x18\x61\0\0\0\0\0\
 \0\0\0\0\0\0\0\0\0\x61\x10\0\0\0\0\0\0\x63\x06\x18\0\0\0\0\0\xb7\0\0\0\0\0\0\0\
 \x95\0\0\0\0\0\0\0";
+	skel->maps.rodata.initial_value = skel_prep_init_value((void **)&skel->rodata, 4096, 98);
 	err = bpf_load_and_run(&opts);
 	if (err < 0)
 		return err;
-	skel->rodata =
-		mmap(skel->rodata, 4096, PROT_READ, MAP_SHARED | MAP_FIXED,
-			skel->maps.rodata.map_fd, 0);
+	skel->rodata = skel_finalize_map_data(&skel->maps.rodata.initial_value,
+			4096, PROT_READ, skel->maps.rodata.map_fd);
 	return 0;
 }
 
Signature · 1 line
2.30.2
#12 lore raw

Re: [PATCH v2 bpf-next 4/5] bpf: Update iterators.lskel.h.

In reply to Alexei Starovoitov
Quoted 39 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Light skeleton and skel_internal.h have changed.
Update iterators.lskel.h.

---
 .../bpf/preload/iterators/iterators.lskel.h   | 28 +++++++------------
 1 file changed, 10 insertions(+), 18 deletions(-)
kernel/bpf/preload/iterators/iterators.lskel.h +2 -4
index d90562d672d2..3e45237f59f4 100644
@@ -3,8 +3,6 @@
 #ifndef __ITERATORS_BPF_SKEL_H__
 #define __ITERATORS_BPF_SKEL_H__
 
-#include <stdlib.h>
-#include <bpf/bpf.h>
 #include <bpf/skel_internal.h>
 
 struct iterators_bpf {
@@ -70,31 +68,25 @@ iterators_bpf__destroy(struct iterators_bpf *skel)
        iterators_bpf__detach(skel);
        skel_closenz(skel->progs.dump_bpf_map.prog_fd);
        skel_closenz(skel->progs.dump_bpf_prog.prog_fd);
-       munmap(skel->rodata, 4096);
+       skel_free_map_data(skel->rodata, skel->maps.rodata.initial_value, 4096);
        skel_closenz(skel->maps.rodata.map_fd);
-       free(skel);
+       skel_free(skel);
 }
 static inline struct iterators_bpf *
 iterators_bpf__open(void)
 {
        struct iterators_bpf *skel;
 

kernel/bpf/preload/iterators/iterators.lskel.h iterators_bpf__destroy(struct iterators_bpf *skel) · line 90

Quoted 10 lines
kernel/bpf/preload/iterators/iterators.lskel.h +1 -6
-       skel = calloc(sizeof(*skel), 1);
+       skel = skel_alloc(sizeof(*skel));
        if (!skel)
                goto cleanup;
        skel->ctx.sz = (void *)&skel->links - (void *)skel;
-       skel->rodata =
-               mmap(NULL, 4096, PROT_READ | PROT_WRITE,
-                    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
-       if (skel->rodata == (void *) -1)
-               goto cleanup;

previously if mmap() failed you'd go to cleanup, but now skel->rodata will remain NULL. Are you concerned about this?

Quoted 29 lines
kernel/bpf/preload/iterators/iterators.lskel.h +4 -5
-       memcpy(skel->rodata, (void *)"\
+       skel->rodata = skel_prep_map_data((void *)"\
 \x20\x20\x69\x64\x20\x6e\x61\x6d\x65\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\
 \x20\x20\x20\x6d\x61\x78\x5f\x65\x6e\x74\x72\x69\x65\x73\x0a\0\x25\x34\x75\x20\
 \x25\x2d\x31\x36\x73\x25\x36\x64\x0a\0\x20\x20\x69\x64\x20\x6e\x61\x6d\x65\x20\
 \x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x61\x74\x74\x61\x63\x68\x65\
-\x64\x0a\0\x25\x34\x75\x20\x25\x2d\x31\x36\x73\x20\x25\x73\x20\x25\x73\x0a\0", 98);
-       skel->maps.rodata.initial_value = (__u64)(long)skel->rodata;
+\x64\x0a\0\x25\x34\x75\x20\x25\x2d\x31\x36\x73\x20\x25\x73\x20\x25\x73\x0a\0", 4096, 98);
        return skel;
 cleanup:
        iterators_bpf__destroy(skel);
@@ -343,11 +335,11 @@ iterators_bpf__load(struct iterators_bpf *skel)
 \0\0\x18\x62\0\0\0\0\0\0\0\0\0\0\x30\x0e\0\0\xb7\x03\0\0\x1c\0\0\0\x85\0\0\0\
 \xa6\0\0\0\xbf\x07\0\0\0\0\0\0\xc5\x07\xd4\xff\0\0\0\0\x63\x7a\x78\xff\0\0\0\0\
 \x61\xa0\x78\xff\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\x80\x0e\0\0\x63\x01\0\0\0\
-\0\0\0\x61\x60\x20\0\0\0\0\0\x15\0\x03\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\
+\0\0\0\x61\x60\x1c\0\0\0\0\0\x15\0\x03\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\
 \x5c\x0e\0\0\x63\x01\0\0\0\0\0\0\xb7\x01\0\0\0\0\0\0\x18\x62\0\0\0\0\0\0\0\0\0\
 \0\x50\x0e\0\0\xb7\x03\0\0\x48\0\0\0\x85\0\0\0\xa6\0\0\0\xbf\x07\0\0\0\0\0\0\
 \xc5\x07\xc3\xff\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x63\x71\0\0\0\0\0\
-\0\x79\x63\x18\0\0\0\0\0\x15\x03\x04\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\x98\
+\0\x79\x63\x20\0\0\0\0\0\x15\x03\x04\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\x98\
 \x0e\0\0\xb7\x02\0\0\x62\0\0\0\x85\0\0\0\x94\0\0\0\x18\x62\0\0\0\0\0\0\0\0\0\0\
 \0\0\0\0\x61\x20\0\0\0\0\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\x08\x0f\0\0\x63\x01\0\
 \0\0\0\0\0\x18\x60\0\0\0\0\0\0\0\0\0\0\0\x0f\0\0\x18\x61\0\0\0\0\0\0\0\0\0\0\
@@ -401,12 +393,12 @@ iterators_bpf__load(struct iterators_bpf *skel)
 \x28\0\0\0\0\0\x61\xa0\x84\xff\0\0\0\0\x63\x06\x2c\0\0\0\0\0\x18\x61\0\0\0\0\0\
 \0\0\0\0\0\0\0\0\0\x61\x10\0\0\0\0\0\0\x63\x06\x18\0\0\0\0\0\xb7\0\0\0\0\0\0\0\

kernel/bpf/preload/iterators/iterators.lskel.h iterators_bpf__load(struct iterators_bpf *skel) · line 401

Quoted 10 lines
kernel/bpf/preload/iterators/iterators.lskel.h +3 -3
 \x95\0\0\0\0\0\0\0";
+       skel->maps.rodata.initial_value = skel_prep_init_value((void **)&skel->rodata, 4096, 98);
        err = bpf_load_and_run(&opts);
        if (err < 0)
                return err;
-       skel->rodata =
-               mmap(skel->rodata, 4096, PROT_READ, MAP_SHARED | MAP_FIXED,
-                       skel->maps.rodata.map_fd, 0);
+       skel->rodata = skel_finalize_map_data(&skel->maps.rodata.initial_value,
+                       4096, PROT_READ, skel->maps.rodata.map_fd);

here seems like both before and now, on error, nothing happens. For kernel mode it matches skeleton behavior (rodata will be NULL), but for user-space code you'll have (void *)-1, which is probably not great.

Quoted 8 lines
kernel/bpf/preload/iterators/iterators.lskel.h +0 -1
        return 0;
 }
 
 
 
--

2.30.2

#13 lore raw

Re: [PATCH v2 bpf-next 4/5] bpf: Update iterators.lskel.h.

Quoted 10 lines · Andrii Nakryiko

kernel/bpf/preload/iterators/iterators.lskel.h iterators_bpf__load(struct iterators_bpf *skel) · line 401

Quoted 5 lines
kernel/bpf/preload/iterators/iterators.lskel.h +2 -3
-       skel->rodata =
-               mmap(skel->rodata, 4096, PROT_READ, MAP_SHARED | MAP_FIXED,
-                       skel->maps.rodata.map_fd, 0);
+       skel->rodata = skel_finalize_map_data(&skel->maps.rodata.initial_value,
+                       4096, PROT_READ, skel->maps.rodata.map_fd);

here seems like both before and now, on error, nothing happens. For kernel mode it matches skeleton behavior (rodata will be NULL), but for user-space code you'll have (void *)-1, which is probably not great.

Yeah, not a regression, but let's add the checks while at it.

#14 lore raw

PATCH v2 5/5 bpf: Convert bpf_preload.ko to use light skeleton.

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

The main change is a move of the single line
#include "iterators.lskel.h"
from iterators/iterators.c to bpf_preload_kern.c.
Which means that generated light skeleton can be used from user space or user mode driver like iterators.c or from the kernel module.
The direct use of light skeleton from the kernel module simplifies the code, since UMD is no longer necessary. The libbpf.a required user space and UMD. The CO-RE in the kernel and generated "loader bpf program" used by the light skeleton are capable to perform complex loading operations traditionally provided by libbpf. In addition UMD approach was launching UMD process every time bpffs has to be mounted. With light skeleton in the kernel the bpf_preload kernel module loads bpf iterators once and pins them multiple times into different bpffs mounts.

Note the light skeleton cannot be used during early boot or out of kthread since light skeleton needs a valid mm. This limitation could be lifted in the future.

---
 kernel/bpf/inode.c                            |  39 ++----
 kernel/bpf/preload/Kconfig                    |   9 +-
 kernel/bpf/preload/Makefile                   |  14 +--
 kernel/bpf/preload/bpf_preload.h              |   8 +-
 kernel/bpf/preload/bpf_preload_kern.c         | 119 ++++++++----------
 kernel/bpf/preload/bpf_preload_umd_blob.S     |   7 --
 .../preload/iterators/bpf_preload_common.h    |  13 --
 kernel/bpf/preload/iterators/iterators.c      | 108 ----------------
 kernel/bpf/syscall.c                          |   2 +
 9 files changed, 72 insertions(+), 247 deletions(-)
 delete mode 100644 kernel/bpf/preload/bpf_preload_umd_blob.S
 delete mode 100644 kernel/bpf/preload/iterators/bpf_preload_common.h
 delete mode 100644 kernel/bpf/preload/iterators/iterators.c
kernel/bpf/inode.c +10 -29
index 5a8d9f7467bf..4f841e16779e 100644
@@ -710,11 +710,10 @@ static DEFINE_MUTEX(bpf_preload_lock);
 static int populate_bpffs(struct dentry *parent)
 {
 	struct bpf_preload_info objs[BPF_PRELOAD_LINKS] = {};
-	struct bpf_link *links[BPF_PRELOAD_LINKS] = {};
 	int err = 0, i;
 
 	/* grab the mutex to make sure the kernel interactions with bpf_preload
-	 * UMD are serialized
+	 * are serialized
 	 */
 	mutex_lock(&bpf_preload_lock);
 
@@ -722,40 +721,22 @@ static int populate_bpffs(struct dentry *parent)
 	if (!bpf_preload_mod_get())
 		goto out;
 
-	if (!bpf_preload_ops->info.tgid) {
-		/* preload() will start UMD that will load BPF iterator programs */
-		err = bpf_preload_ops->preload(objs);
-		if (err)
+	err = bpf_preload_ops->preload(objs);
+	if (err)
+		goto out_put;
+	for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
+		bpf_link_inc(objs[i].link);
+		err = bpf_iter_link_pin_kernel(parent,
+					       objs[i].link_name, objs[i].link);
+		if (err) {
+			bpf_link_put(objs[i].link);
 			goto out_put;
-		for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
-			links[i] = bpf_link_by_id(objs[i].link_id);
-			if (IS_ERR(links[i])) {
-				err = PTR_ERR(links[i]);
-				goto out_put;
-			}
 		}
-		for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
-			err = bpf_iter_link_pin_kernel(parent,
-						       objs[i].link_name, links[i]);
-			if (err)
-				goto out_put;
-			/* do not unlink successfully pinned links even
-			 * if later link fails to pin
-			 */
-			links[i] = NULL;
-		}
-		/* finish() will tell UMD process to exit */
-		err = bpf_preload_ops->finish();
-		if (err)
-			goto out_put;
 	}
 out_put:
 	bpf_preload_mod_put();
 out:
 	mutex_unlock(&bpf_preload_lock);
-	for (i = 0; i < BPF_PRELOAD_LINKS && err; i++)
-		if (!IS_ERR_OR_NULL(links[i]))
-			bpf_link_put(links[i]);
 	return err;
 }
 
kernel/bpf/preload/Kconfig +5 -4
index 26bced262473..9de6cfa5dbb1 100644
@@ -18,10 +18,11 @@ menuconfig BPF_PRELOAD
 
 if BPF_PRELOAD
 config BPF_PRELOAD_UMD
-	tristate "bpf_preload kernel module with user mode driver"
-	depends on CC_CAN_LINK
-	depends on m || CC_CAN_LINK_STATIC
+	tristate "bpf_preload kernel module"
+	# light skeleton cannot run out of kthread without mm
+	depends on m
 	default m
 	help
-	  This builds bpf_preload kernel module with embedded user mode driver.
+	  This builds bpf_preload kernel module with embedded BPF programs for
+	  introspection in bpffs.
 endif
kernel/bpf/preload/Makefile +2 -12
index baf47d9c7557..167534e3b0b4 100644
@@ -3,16 +3,6 @@
 LIBBPF_SRCS = $(srctree)/tools/lib/bpf/
 LIBBPF_INCLUDE = $(LIBBPF_SRCS)/..
 
-userccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi \
-	-I $(LIBBPF_INCLUDE) -Wno-unused-result
-
-userprogs := bpf_preload_umd
-
-bpf_preload_umd-objs := iterators/iterators.o
-
-$(obj)/bpf_preload_umd:
-
-$(obj)/bpf_preload_umd_blob.o: $(obj)/bpf_preload_umd
-
 obj-$(CONFIG_BPF_PRELOAD_UMD) += bpf_preload.o
-bpf_preload-objs += bpf_preload_kern.o bpf_preload_umd_blob.o
+CFLAGS_bpf_preload_kern.o += -I $(LIBBPF_INCLUDE)
+bpf_preload-objs += bpf_preload_kern.o
kernel/bpf/preload/bpf_preload.h +4 -4
index 2f9932276f2e..f065c91213a0 100644
@@ -2,13 +2,13 @@
 #ifndef _BPF_PRELOAD_H
 #define _BPF_PRELOAD_H
 
-#include <linux/usermode_driver.h>
-#include "iterators/bpf_preload_common.h"
+struct bpf_preload_info {
+	char link_name[16];
+	struct bpf_link *link;
+};
 
 struct bpf_preload_ops {
-        struct umd_info info;
 	int (*preload)(struct bpf_preload_info *);
-	int (*finish)(void);
 	struct module *owner;
 };
 extern struct bpf_preload_ops *bpf_preload_ops;
kernel/bpf/preload/bpf_preload_kern.c +49 -70

150 lines not on this page · raw message

kernel/bpf/preload/bpf_preload_umd_blob.S deleted +0 -7
deleted file mode 100644
index f1f40223b5c3..000000000000
@@ -1,7 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-	.section .init.rodata, "a"
-	.global bpf_preload_umd_start
-bpf_preload_umd_start:
-	.incbin "kernel/bpf/preload/bpf_preload_umd"
-	.global bpf_preload_umd_end
-bpf_preload_umd_end:
kernel/bpf/preload/iterators/bpf_preload_common.h deleted +0 -13
deleted file mode 100644
index 8464d1a48c05..000000000000
@@ -1,13 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-#ifndef _BPF_PRELOAD_COMMON_H
-#define _BPF_PRELOAD_COMMON_H
-
-#define BPF_PRELOAD_START 0x5555
-#define BPF_PRELOAD_END 0xAAAA
-
-struct bpf_preload_info {
-	char link_name[16];
-	int link_id;
-};
-
-#endif
kernel/bpf/preload/iterators/iterators.c deleted +0 -108

108 lines not on this page · raw message

kernel/bpf/syscall.c +2 -0
index 49f88b30662a..35646db3d950 100644
@@ -2490,6 +2490,7 @@ void bpf_link_put(struct bpf_link *link)
 		bpf_link_free(link);
 	}
 }
+EXPORT_SYMBOL(bpf_link_put);
 
 static int bpf_link_release(struct inode *inode, struct file *filp)
 {
@@ -2632,6 +2633,7 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd)
 
 	return link;
 }
+EXPORT_SYMBOL(bpf_link_get_from_fd);
 
 struct bpf_tracing_link {
 	struct bpf_link link;
Signature · 1 line
2.30.2
Acked-by on v2 5/5
#15 lore raw

Re: [PATCH v2 bpf-next 5/5] bpf: Convert bpf_preload.ko to use light skeleton.

Quoted 16 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

The main change is a move of the single line
#include "iterators.lskel.h"
from iterators/iterators.c to bpf_preload_kern.c.
Which means that generated light skeleton can be used from user space or user mode driver like iterators.c or from the kernel module.
The direct use of light skeleton from the kernel module simplifies the code, since UMD is no longer necessary. The libbpf.a required user space and UMD. The CO-RE in the kernel and generated "loader bpf program" used by the light skeleton are capable to perform complex loading operations traditionally provided by libbpf. In addition UMD approach was launching UMD process every time bpffs has to be mounted. With light skeleton in the kernel the bpf_preload kernel module loads bpf iterators once and pins them multiple times into different bpffs mounts.

Quoted 5 lines

Note the light skeleton cannot be used during early boot or out of kthread since light skeleton needs a valid mm. This limitation could be lifted in the future.

Indeed, this is much more simpler which uses the same set of lskel API functions. One minor nit below.

Quoted 199 lines
---
  kernel/bpf/inode.c                            |  39 ++----
  kernel/bpf/preload/Kconfig                    |   9 +-
  kernel/bpf/preload/Makefile                   |  14 +--
  kernel/bpf/preload/bpf_preload.h              |   8 +-
  kernel/bpf/preload/bpf_preload_kern.c         | 119 ++++++++----------
  kernel/bpf/preload/bpf_preload_umd_blob.S     |   7 --
  .../preload/iterators/bpf_preload_common.h    |  13 --
  kernel/bpf/preload/iterators/iterators.c      | 108 ----------------
  kernel/bpf/syscall.c                          |   2 +
  9 files changed, 72 insertions(+), 247 deletions(-)

delete mode 100644 kernel/bpf/preload/bpf_preload_umd_blob.S delete mode 100644 kernel/bpf/preload/iterators/bpf_preload_common.h delete mode 100644 kernel/bpf/preload/iterators/iterators.c

kernel/bpf/inode.c +10 -29
index 5a8d9f7467bf..4f841e16779e 100644
@@ -710,11 +710,10 @@ static DEFINE_MUTEX(bpf_preload_lock);
  static int populate_bpffs(struct dentry *parent)
  {
  	struct bpf_preload_info objs[BPF_PRELOAD_LINKS] = {};
-	struct bpf_link *links[BPF_PRELOAD_LINKS] = {};
  	int err = 0, i;
 
  	/* grab the mutex to make sure the kernel interactions with bpf_preload
-	 * UMD are serialized
+	 * are serialized
  	 */
  	mutex_lock(&bpf_preload_lock);
 
@@ -722,40 +721,22 @@ static int populate_bpffs(struct dentry *parent)
  	if (!bpf_preload_mod_get())
  		goto out;
 
-	if (!bpf_preload_ops->info.tgid) {
-		/* preload() will start UMD that will load BPF iterator programs */
-		err = bpf_preload_ops->preload(objs);
-		if (err)
+	err = bpf_preload_ops->preload(objs);
+	if (err)
+		goto out_put;
+	for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
+		bpf_link_inc(objs[i].link);
+		err = bpf_iter_link_pin_kernel(parent,
+					       objs[i].link_name, objs[i].link);
+		if (err) {
+			bpf_link_put(objs[i].link);
  			goto out_put;
-		for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
-			links[i] = bpf_link_by_id(objs[i].link_id);
-			if (IS_ERR(links[i])) {
-				err = PTR_ERR(links[i]);
-				goto out_put;
-			}
  		}
-		for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
-			err = bpf_iter_link_pin_kernel(parent,
-						       objs[i].link_name, links[i]);
-			if (err)
-				goto out_put;
-			/* do not unlink successfully pinned links even
-			 * if later link fails to pin
-			 */
-			links[i] = NULL;
-		}
-		/* finish() will tell UMD process to exit */
-		err = bpf_preload_ops->finish();
-		if (err)
-			goto out_put;
  	}
  out_put:
  	bpf_preload_mod_put();
  out:
  	mutex_unlock(&bpf_preload_lock);
-	for (i = 0; i < BPF_PRELOAD_LINKS && err; i++)
-		if (!IS_ERR_OR_NULL(links[i]))
-			bpf_link_put(links[i]);
  	return err;
  }
 
kernel/bpf/preload/Kconfig +5 -4
index 26bced262473..9de6cfa5dbb1 100644
@@ -18,10 +18,11 @@ menuconfig BPF_PRELOAD
 
  if BPF_PRELOAD
  config BPF_PRELOAD_UMD
-	tristate "bpf_preload kernel module with user mode driver"
-	depends on CC_CAN_LINK
-	depends on m || CC_CAN_LINK_STATIC
+	tristate "bpf_preload kernel module"
+	# light skeleton cannot run out of kthread without mm
+	depends on m
  	default m
  	help
-	  This builds bpf_preload kernel module with embedded user mode driver.
+	  This builds bpf_preload kernel module with embedded BPF programs for
+	  introspection in bpffs.
  endif
kernel/bpf/preload/Makefile +2 -12
index baf47d9c7557..167534e3b0b4 100644
@@ -3,16 +3,6 @@
  LIBBPF_SRCS = $(srctree)/tools/lib/bpf/
  LIBBPF_INCLUDE = $(LIBBPF_SRCS)/..
 
-userccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi \
-	-I $(LIBBPF_INCLUDE) -Wno-unused-result
-
-userprogs := bpf_preload_umd
-
-bpf_preload_umd-objs := iterators/iterators.o
-
-$(obj)/bpf_preload_umd:
-
-$(obj)/bpf_preload_umd_blob.o: $(obj)/bpf_preload_umd
-
  obj-$(CONFIG_BPF_PRELOAD_UMD) += bpf_preload.o
-bpf_preload-objs += bpf_preload_kern.o bpf_preload_umd_blob.o
+CFLAGS_bpf_preload_kern.o += -I $(LIBBPF_INCLUDE)
+bpf_preload-objs += bpf_preload_kern.o
kernel/bpf/preload/bpf_preload.h +4 -4
index 2f9932276f2e..f065c91213a0 100644
@@ -2,13 +2,13 @@
  #ifndef _BPF_PRELOAD_H
  #define _BPF_PRELOAD_H
 
-#include <linux/usermode_driver.h>
-#include "iterators/bpf_preload_common.h"
+struct bpf_preload_info {
+	char link_name[16];
+	struct bpf_link *link;
+};
 
  struct bpf_preload_ops {
-        struct umd_info info;
  	int (*preload)(struct bpf_preload_info *);
-	int (*finish)(void);
  	struct module *owner;
  };
  extern struct bpf_preload_ops *bpf_preload_ops;
kernel/bpf/preload/bpf_preload_kern.c +22 -13
index 53736e52c1df..30207c048d36 100644
@@ -2,101 +2,80 @@
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  #include <linux/init.h>
  #include <linux/module.h>
-#include <linux/pid.h>
-#include <linux/fs.h>
-#include <linux/sched/signal.h>
  #include "bpf_preload.h"
+#include "iterators/iterators.lskel.h"
 
-extern char bpf_preload_umd_start;
-extern char bpf_preload_umd_end;
+static struct bpf_link *maps_link, *progs_link;
+static struct iterators_bpf *skel;
 
-static int preload(struct bpf_preload_info *obj);
-static int finish(void);
+static void free_links_and_skel(void)
+{
+	if (!IS_ERR_OR_NULL(maps_link))
+		bpf_link_put(maps_link);
+	if (!IS_ERR_OR_NULL(progs_link))
+		bpf_link_put(progs_link);
+	iterators_bpf__destroy(skel);
+}
+
+static int preload(struct bpf_preload_info *obj)
+{
+	strlcpy(obj[0].link_name, "maps.debug", sizeof(obj[0].link_name));
+	obj[0].link = maps_link;
+	strlcpy(obj[1].link_name, "progs.debug", sizeof(obj[1].link_name));
+	obj[1].link = progs_link;
+	return 0;
+}
 
-static struct bpf_preload_ops umd_ops = {
-	.info.driver_name = "bpf_preload",
+static struct bpf_preload_ops ops = {
  	.preload = preload,
-	.finish = finish,
  	.owner = THIS_MODULE,
  };
 
-static int preload(struct bpf_preload_info *obj)
+static int load_skel(void)
  {
-	int magic = BPF_PRELOAD_START;
-	loff_t pos = 0;

kernel/bpf/preload/bpf_preload_kern.c line 42

Quoted 10 lines
kernel/bpf/preload/bpf_preload_kern.c +5 -3
-	int i, err;
-	ssize_t n;
+	int err;
 
-	err = fork_usermode_driver(&umd_ops.info);
+	skel = iterators_bpf__open();
+	if (!skel)
+		return -ENOMEM;
+	err = iterators_bpf__load(skel);
  	if (err)

We can do iterators_bpf__open_and_load here, right?

Quoted 26 lines
kernel/bpf/preload/bpf_preload_kern.c +8 -17
-		return err;
-
-	/* send the start magic to let UMD proceed with loading BPF progs */
-	n = kernel_write(umd_ops.info.pipe_to_umh,
-			 &magic, sizeof(magic), &pos);
-	if (n != sizeof(magic))
-		return -EPIPE;
-
-	/* receive bpf_link IDs and names from UMD */
-	pos = 0;
-	for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
-		n = kernel_read(umd_ops.info.pipe_from_umh,
-				&obj[i], sizeof(*obj), &pos);
-		if (n != sizeof(*obj))
-			return -EPIPE;
+		goto out;
+	err = iterators_bpf__attach(skel);
+	if (err)
+		goto out;
+	maps_link = bpf_link_get_from_fd(skel->links.dump_bpf_map_fd);
+	if (IS_ERR(maps_link)) {
+		err = PTR_ERR(maps_link);
+		goto out;
  	}
-	return 0;
-}

[...]

#16 lore raw

Re: [PATCH v2 bpf-next 5/5] bpf: Convert bpf_preload.ko to use light skeleton.

Quoted 8 lines · Yonghong Song

kernel/bpf/preload/bpf_preload_kern.c line 42

Quoted 6 lines
kernel/bpf/preload/bpf_preload_kern.c +4 -1
-	err = fork_usermode_driver(&umd_ops.info);
+	skel = iterators_bpf__open();
+	if (!skel)
+		return -ENOMEM;
+	err = iterators_bpf__load(skel);
  	if (err)

We can do iterators_bpf__open_and_load here, right?

Right. It does __open and __load separately, so it's easier to insert debug printk and adjust rodata for testing.

#17 lore raw

Re: [PATCH v2 bpf-next 5/5] bpf: Convert bpf_preload.ko to use light skeleton.

Quoted 10 lines · Alexei Starovoitov
Quoted 8 lines · Yonghong Song

kernel/bpf/preload/bpf_preload_kern.c line 42

Quoted 6 lines
kernel/bpf/preload/bpf_preload_kern.c +4 -1
-	err = fork_usermode_driver(&umd_ops.info);
+	skel = iterators_bpf__open();
+	if (!skel)
+		return -ENOMEM;
+	err = iterators_bpf__load(skel);
   	if (err)

We can do iterators_bpf__open_and_load here, right?

Quoted 2 lines

Right. It does __open and __load separately, so it's easier to insert debug printk and adjust rodata for testing.

Once all the debugging thing is done as in this patch, iterators_bpf__open_and_load can make code more concise. But I am okay either way.

Acked-by on the v2 series
#18 lore raw
In reply to Alexei Starovoitov
Quoted 18 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

The libbpf performs a set of complex operations to load BPF programs. With "loader program" and "CO-RE in the kernel" the loading job of libbpf was diminished. The light skeleton became lean enough to perform program loading and map creation tasks without libbpf.
It's now possible to tweak it further to make light skeleton usable out of user space and out of kernel module.
This allows bpf_preload.ko to drop user-mode-driver usage,
drop host compiler dependency, allow cross compilation and simplify the code. It's a building block toward safe and portable kernel modules.

v1->v2:
- removed redundant anon struct and added comments (Andrii's reivew)
- added Yonghong's ack
- fixed build warning when JIT is off

Quoted 7 lines

Alexei Starovoitov (5):
bpf: Extend sys_bpf commands for bpf_syscall programs.
libbpf: Prepare light skeleton for the kernel.
bpftool: Generalize light skeleton generation.
bpf: Update iterators.lskel.h.
bpf: Convert bpf_preload.ko to use light skeleton.

See question about error handling for rodata in skeleton. But otherwise LGTM.

For the series:

Quoted 20 lines
 kernel/bpf/inode.c                            |  39 +---
 kernel/bpf/preload/Kconfig                    |   9 +-
 kernel/bpf/preload/Makefile                   |  14 +-
 kernel/bpf/preload/bpf_preload.h              |   8 +-
 kernel/bpf/preload/bpf_preload_kern.c         | 119 +++++------
 kernel/bpf/preload/bpf_preload_umd_blob.S     |   7 -
 .../preload/iterators/bpf_preload_common.h    |  13 --
 kernel/bpf/preload/iterators/iterators.c      | 108 ----------
 .../bpf/preload/iterators/iterators.lskel.h   |  28 +--
 kernel/bpf/syscall.c                          |  40 +++-
 tools/bpf/bpftool/gen.c                       |  45 ++--
 tools/lib/bpf/skel_internal.h                 | 193 ++++++++++++++++--
 12 files changed, 319 insertions(+), 304 deletions(-)
 delete mode 100644 kernel/bpf/preload/bpf_preload_umd_blob.S
 delete mode 100644 kernel/bpf/preload/iterators/bpf_preload_common.h
 delete mode 100644 kernel/bpf/preload/iterators/iterators.c

--
2.30.2