Skip to contentSkip to conversation

Introduce typed pointer support in BPF maps

PATCH Superseded Kumar Kartikeya Dwivedi 4 participants 38 messages Feb 20, 2022 – Feb 24, 2022

Subsystems (8)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover Introduce typed pointer support in BPF maps

Introduction
------------

This set enables storing pointers of a certain type in BPF map, and extends the verifier to enforce type safety and lifetime correctness properties.

The infrastructure being added is generic enough for allowing storing any kind of pointers whose type is available using BTF (user or kernel) in the future (e.g. strongly typed memory allocation in BPF program), which are internally tracked in the verifier as PTR_TO_BTF_ID, but for now the series limits them to four kinds of pointers obtained from the kernel.

Obviously, use of this feature depends on map BTF.

1. Unreferenced kernel pointer

In this case, there are very few restrictions. The pointer type being stored must match the type declared in the map value. However, such a pointer when loaded from the map can only be dereferenced, but not passed to any in-kernel helpers or kernel functions available to the program. This is because while the verifier's exception handling mechanism coverts BPF_LDX to PROBE_MEM loads, which are then handled specially by the JIT implementation, the same liberty is not available to accesses inside the kernel. The pointer by the time it is passed into a helper has no lifetime related guarantees about the object it is pointing to, and may well be referencing invalid memory.

2. Referenced kernel pointer

This case imposes a lot of restrictions on the programmer, to ensure safety. To transfer the ownership of a reference in the BPF program to the map, the user must use the BPF_XCHG instruction, which returns the old pointer contained in the map, as an acquired reference, and releases verifier state for the referenced pointer being exchanged, as it moves into the map.

This a normal PTR_TO_BTF_ID that can be used with in-kernel helpers and kernel functions callable by the program.

However, if BPF_LDX is used to load a referenced pointer from the map, it is still not permitted to pass it to in-kernel helpers or kernel functions. To obtain a reference usable with helpers, the user must invoke a kfunc helper which returns a usable reference (which also must be eventually released before BPF_EXIT, or moved into a map).

Since the load of the pointer (preserving data dependency ordering) must happen inside the RCU read section, the kfunc helper will take a pointer to the map value, which must point to the actual pointer of the object whose reference is to be raised. The type will be verified from the BTF information of the kfunc, as the prototype must be:

	T *func(T **, ... /* other arguments */);

Then, the verifier checks whether pointer at offset of the map value points to the type T, and permits the call.

This convention is followed so that such helpers may also be called from sleepable BPF programs, where RCU read lock is not necessarily held in the BPF program context, hence necessiating the need to pass in a pointer to the actual pointer to perform the load inside the RCU read section.

3. per-CPU kernel pointer

These have very little restrictions. The user can store a PTR_TO_PERCPU_BTF_ID into the map, and when loading from the map, they must NULL check it before use, because while a non-zero value stored into the map should always be valid, it can still be reset to zero on updates. After checking it to be non-NULL, it can be passed to bpf_per_cpu_ptr and bpf_this_cpu_ptr helpers to obtain a PTR_TO_BTF_ID to underlying per-CPU object.

It is also permitted to write 0 and reset the value.

4. Userspace pointer

The verifier recently gained support for annotating BTF with __user type tag. This indicates pointers pointing to memory which must be read using the bpf_probe_read_user helper to ensure correct results. The set also permits storing them into the BPF map, and ensures user pointer cannot be stored into other kinds of pointers mentioned above.

When loaded from the map, the only thing that can be done is to pass this pointer to bpf_probe_read_user. No dereference is allowed.

Notes
-----

This set requires the following LLVM fix to pass the BPF CI:

https://reviews.llvm.org/D119799

Also, I applied Alexei's suggestion of removing callback for btf_find_field, but that 'ugly' is still required, since bad offset alignment etc. can return an error, and we don't want to leave a partial ptr_off_tab around in that case. The other option is freeing inside btf_find_field, but that would be more code conditional on BTF_FIELD_KPTR, when the caller can do it based on ret < 0.

TODO
----

Needs a lot more testing, especially for stuff apart from verifier correctness. Will work on that in parallel during v1 review. The idea was to get a little more feedback (esp. for kptr_get stuff) before moving forward with adding more tests. Posting it now to just get discussion started. The verifier tests fairly comprehensively test many edge cases I could think of.

Kumar Kartikeya Dwivedi (15):
bpf: Factor out fd returning from bpf_btf_find_by_name_kind
bpf: Make btf_find_field more generic
bpf: Allow storing PTR_TO_BTF_ID in map
bpf: Allow storing referenced PTR_TO_BTF_ID in map
bpf: Allow storing PTR_TO_PERCPU_BTF_ID in map
bpf: Allow storing __user PTR_TO_BTF_ID in map
bpf: Prevent escaping of pointers loaded from maps
bpf: Adapt copy_map_value for multiple offset case
bpf: Populate pairs of btf_id and destructor kfunc in btf
bpf: Wire up freeing of referenced PTR_TO_BTF_ID in map
bpf: Teach verifier about kptr_get style kfunc helpers net/netfilter: Add bpf_ct_kptr_get helper
libbpf: Add __kptr* macros to bpf_helpers.h selftests/bpf: Add C tests for PTR_TO_BTF_ID in map selftests/bpf: Add verifier tests for PTR_TO_BTF_ID in map

 include/linux/bpf.h                           |  90 ++-
 include/linux/btf.h                           |  24 +
 include/net/netfilter/nf_conntrack_core.h     |  17 +
 kernel/bpf/arraymap.c                         |  13 +-
 kernel/bpf/btf.c                              | 565 ++++++++++++++--
 kernel/bpf/hashtab.c                          |  27 +-
 kernel/bpf/map_in_map.c                       |   5 +-
 kernel/bpf/syscall.c                          | 227 ++++++-
 kernel/bpf/verifier.c                         | 311 ++++++++-
 net/bpf/test_run.c                            |  17 +-
 net/netfilter/nf_conntrack_bpf.c              | 132 +++-
 net/netfilter/nf_conntrack_core.c             |  17 -
 tools/lib/bpf/bpf_helpers.h                   |   4 +
 .../selftests/bpf/prog_tests/map_btf_ptr.c    |  13 +
 .../testing/selftests/bpf/progs/map_btf_ptr.c | 105 +++
 .../testing/selftests/bpf/progs/test_bpf_nf.c |  31 +
 tools/testing/selftests/bpf/test_verifier.c   |  57 +-
 .../selftests/bpf/verifier/map_btf_ptr.c      | 624 ++++++++++++++++++
 18 files changed, 2144 insertions(+), 135 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/map_btf_ptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/map_btf_ptr.c
 create mode 100644 tools/testing/selftests/bpf/verifier/map_btf_ptr.c
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH 1/15 bpf: Factor out fd returning from bpf_btf_find_by_name_kind

In next few patches, we need a helper that searches all kernel BTFs (vmlinux and module BTFs), and finds the type denoted by 'name' and 'kind'. Turns out bpf_btf_find_by_name_kind already does the same thing, but it instead returns a BTF ID and optionally fd (if module BTF). This is used for relocating ksyms in BPF loader code (bpftool gen skel -L).

We extract the core code out into a new helper btf_find_by_name_kind_all, which returns the BTF ID and BTF pointer in an out parameter. The reference for the returned BTF pointer is only bumped if it is a module BTF, this needs to be kept in mind when using this helper.

Hence, the user must release the BTF reference iff btf_is_module is true, otherwise transfer the ownership to e.g. an fd.

In case of the helper, the fd is only allocated for module BTFs, so no extra handling for btf_vmlinux case is required.

---
 kernel/bpf/btf.c | 47 +++++++++++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 16 deletions(-)
kernel/bpf/btf.c +31 -16
index 2c4c5dbe2abe..3645d8c14a18 100644
@@ -6545,16 +6545,10 @@ static struct btf *btf_get_module_btf(const struct module *module)
 	return btf;
 }
 
-BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
+static s32 btf_find_by_name_kind_all(const char *name, u32 kind, struct btf **btfp)
 {
 	struct btf *btf;
-	long ret;
-
-	if (flags)
-		return -EINVAL;
-
-	if (name_sz <= 1 || name[name_sz - 1])
-		return -EINVAL;
+	s32 ret;
 
 	btf = bpf_get_btf_vmlinux();
 	if (IS_ERR(btf))
@@ -6580,19 +6574,40 @@ BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int
 			spin_unlock_bh(&btf_idr_lock);
 			ret = btf_find_by_name_kind(mod_btf, name, kind);
 			if (ret > 0) {
-				int btf_obj_fd;
-
-				btf_obj_fd = __btf_new_fd(mod_btf);
-				if (btf_obj_fd < 0) {
-					btf_put(mod_btf);
-					return btf_obj_fd;
-				}
-				return ret | (((u64)btf_obj_fd) << 32);
+				*btfp = mod_btf;
+				return ret;
 			}
 			spin_lock_bh(&btf_idr_lock);
 			btf_put(mod_btf);
 		}
 		spin_unlock_bh(&btf_idr_lock);
+	} else {
+		*btfp = btf;
+	}
+	return ret;
+}
+
+BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
+{
+	struct btf *btf = NULL;
+	int btf_obj_fd = 0;
+	long ret;
+
+	if (flags)
+		return -EINVAL;
+
+	if (name_sz <= 1 || name[name_sz - 1])
+		return -EINVAL;
+
+	ret = btf_find_by_name_kind_all(name, kind, &btf);
+	if (ret > 0 && btf_is_module(btf)) {
+		/* reference for btf is only raised if module BTF */
+		btf_obj_fd = __btf_new_fd(btf);
+		if (btf_obj_fd < 0) {
+			btf_put(btf);
+			return btf_obj_fd;
+		}
+		return ret | (((u64)btf_obj_fd) << 32);
 	}
 	return ret;
 }
Signature · 1 line
2.35.1
author maintainer
#4 lore raw

PATCH 2/15 bpf: Make btf_find_field more generic

In reply to Kumar Kartikeya Dwivedi

Next commit's field type will not be struct, but pointer, and it will not be limited to one offset, but multiple ones. Make existing btf_find_struct_field and btf_find_datasec_var functions amenable to use for finding BTF ID pointers in map value, by taking a moving spin_lock and timer specific checks into their own function.

The alignment, and name are checked before the function is called, so it is the last point where we can skip field or return an error before the next loop iteration happens. This is important, because we'll be potentially reallocating memory inside this function in next commit, so being able to do that when everything else is in order is going to be more convenient.

The name parameter is now optional, and only checked if it is not NULL.

The size must be checked in the function, because in case of PTR it will instead point to the underlying BTF ID it is pointing to (or modifiers), so the check becomes wrong to do outside of function, and the base type has to be obtained by removing modifiers.

---
 kernel/bpf/btf.c | 119 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 85 insertions(+), 34 deletions(-)
kernel/bpf/btf.c +85 -34
index 3645d8c14a18..55f6ccac3388 100644
@@ -3119,71 +3119,108 @@ static void btf_struct_log(struct btf_verifier_env *env,
 	btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
 }
 
+enum {
+	BTF_FIELD_SPIN_LOCK,
+	BTF_FIELD_TIMER,
+};
+
+static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t,
+				 u32 off, int sz, void *data)
+{
+	u32 *offp = data;
+
+	if (!__btf_type_is_struct(t))
+		return 0;
+	if (t->size != sz)
+		return 0;
+	if (*offp != -ENOENT)
+		/* only one such field is allowed */
+		return -E2BIG;
+	*offp = off;
+	return 0;
+}
+
 static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t,
-				 const char *name, int sz, int align)
+				 const char *name, int sz, int align, int field_type,
+				 void *data)
 {
 	const struct btf_member *member;
-	u32 i, off = -ENOENT;
+	u32 i, off;
+	int ret;
 
 	for_each_member(i, t, member) {
 		const struct btf_type *member_type = btf_type_by_id(btf,
 								    member->type);
-		if (!__btf_type_is_struct(member_type))
-			continue;
-		if (member_type->size != sz)
-			continue;
-		if (strcmp(__btf_name_by_offset(btf, member_type->name_off), name))
-			continue;
-		if (off != -ENOENT)
-			/* only one such field is allowed */
-			return -E2BIG;
+
 		off = __btf_member_bit_offset(t, member);
+
+		if (name && strcmp(__btf_name_by_offset(btf, member_type->name_off), name))
+			continue;
 		if (off % 8)
 			/* valid C code cannot generate such BTF */
 			return -EINVAL;
 		off /= 8;
 		if (off % align)
 			return -EINVAL;
+
+		switch (field_type) {
+		case BTF_FIELD_SPIN_LOCK:
+		case BTF_FIELD_TIMER:
+			ret = btf_find_field_struct(btf, member_type, off, sz, data);
+			if (ret < 0)
+				return ret;
+			break;
+		default:
+			pr_err("verifier bug: unknown field type requested\n");
+			return -EFAULT;
+		}
 	}
-	return off;
+	return 0;
 }
 
 static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t,
-				const char *name, int sz, int align)
+				const char *name, int sz, int align, int field_type,
+				void *data)
 {
 	const struct btf_var_secinfo *vsi;
-	u32 i, off = -ENOENT;
+	u32 i, off;
+	int ret;
 
 	for_each_vsi(i, t, vsi) {
 		const struct btf_type *var = btf_type_by_id(btf, vsi->type);
 		const struct btf_type *var_type = btf_type_by_id(btf, var->type);
 
-		if (!__btf_type_is_struct(var_type))
-			continue;
-		if (var_type->size != sz)
+		off = vsi->offset;
+
+		if (name && strcmp(__btf_name_by_offset(btf, var_type->name_off), name))
 			continue;
 		if (vsi->size != sz)
 			continue;
-		if (strcmp(__btf_name_by_offset(btf, var_type->name_off), name))
-			continue;
-		if (off != -ENOENT)
-			/* only one such field is allowed */
-			return -E2BIG;
-		off = vsi->offset;
 		if (off % align)
 			return -EINVAL;
+
+		switch (field_type) {
+		case BTF_FIELD_SPIN_LOCK:
+		case BTF_FIELD_TIMER:
+			ret = btf_find_field_struct(btf, var_type, off, sz, data);
+			if (ret < 0)
+				return ret;
+			break;
+		default:
+			return -EFAULT;
+		}
 	}
-	return off;
+	return 0;
 }
 
 static int btf_find_field(const struct btf *btf, const struct btf_type *t,
-			  const char *name, int sz, int align)
+			  const char *name, int sz, int align, int field_type,
+			  void *data)
 {
-
 	if (__btf_type_is_struct(t))
-		return btf_find_struct_field(btf, t, name, sz, align);
+		return btf_find_struct_field(btf, t, name, sz, align, field_type, data);
 	else if (btf_type_is_datasec(t))
-		return btf_find_datasec_var(btf, t, name, sz, align);
+		return btf_find_datasec_var(btf, t, name, sz, align, field_type, data);
 	return -EINVAL;
 }
 
@@ -3193,16 +3230,30 @@ static int btf_find_field(const struct btf *btf, const struct btf_type *t,
  */
 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t)
 {
-	return btf_find_field(btf, t, "bpf_spin_lock",
-			      sizeof(struct bpf_spin_lock),
-			      __alignof__(struct bpf_spin_lock));
+	u32 off = -ENOENT;
+	int ret;
+
+	ret = btf_find_field(btf, t, "bpf_spin_lock",
+			     sizeof(struct bpf_spin_lock),
+			     __alignof__(struct bpf_spin_lock),
+			     BTF_FIELD_SPIN_LOCK, &off);
+	if (ret < 0)
+		return ret;
+	return off;
 }
 
 int btf_find_timer(const struct btf *btf, const struct btf_type *t)
 {
-	return btf_find_field(btf, t, "bpf_timer",
-			      sizeof(struct bpf_timer),
-			      __alignof__(struct bpf_timer));
+	u32 off = -ENOENT;
+	int ret;
+
+	ret = btf_find_field(btf, t, "bpf_timer",
+			     sizeof(struct bpf_timer),
+			     __alignof__(struct bpf_timer),
+			     BTF_FIELD_TIMER, &off);
+	if (ret < 0)
+		return ret;
+	return off;
 }
 
 static void __btf_struct_show(const struct btf *btf, const struct btf_type *t,
Signature · 1 line
2.35.1
author maintainer
#5 lore raw

PATCH 3/15 bpf: Allow storing PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi

This patch allows user to embed PTR_TO_BTF_ID in map value, such that loading it marks the destination register as having the appropriate register type and such a pointer can be dereferenced like usual PTR_TO_BTF_ID and be passed to various BPF helpers.

This feature can be useful to store an object in a map for a long time, and then inspect it later. Since PTR_TO_BTF_ID is safe against invalid access, verifier doesn't need to perform any complex lifetime checks. It can be useful in cases where user already knows pointer will remain valid, so any dereference at a later time (possibly in entirely different BPF program invocation) will yield correct results as far the data read from kernel memory is concerned.

Note that it is quite possible such BTF ID pointer is invalid, in this case the verifier's built-in exception handling mechanism where it converts loads into PTR_TO_BTF_ID into PROBE_MEM loads, would handle the invalid case. Next patch which adds referenced PTR_TO_BTF_ID would need to take more care in ensuring a correct value is stored in the BPF map.

The user indicates that a certain pointer must be treated as PTR_TO_BTF_ID by using a BTF type tag 'btf_id' on the pointed to type of the pointer. Then, this information is recorded in the object BTF which will be passed into the kernel by way of map's BTF information.

The kernel then records the type, and offset of all such pointers, and finds their corresponding built-in kernel type by the name and BTF kind.

Later, during verification this information is used that access to such pointers is sized correctly, and done at a proper offset into the map value. Only BPF_LDX, BPF_STX, and BPF_ST with 0 (to denote NULL) are allowed instructions that can access such a pointer. On BPF_LDX, the destination register is updated to be a PTR_TO_BTF_ID, and on BPF_STX, it is checked whether the source register type is same PTR_TO_BTF_ID, and whether the BTF ID (reg->btf and reg->btf_id) matches the type specified in the map value's definition.

Hence, the verifier allows flexible access to kernel data across program invocations in a type safe manner, without compromising on the runtime safety of the kernel.

Next patch will extend this support to referenced PTR_TO_BTF_ID.

---
 include/linux/bpf.h     |  30 +++++++-
 include/linux/btf.h     |   3 +
 kernel/bpf/btf.c        | 127 ++++++++++++++++++++++++++++++++++
 kernel/bpf/map_in_map.c |   5 +-
 kernel/bpf/syscall.c    | 137 ++++++++++++++++++++++++++++++++++++-
 kernel/bpf/verifier.c   | 148 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 446 insertions(+), 4 deletions(-)
include/linux/bpf.h +29 -1
index f19abc59b6cd..ce45ffb79f82 100644
@@ -155,6 +155,23 @@ struct bpf_map_ops {
 	const struct bpf_iter_seq_info *iter_seq_info;
 };
 
+enum {
+	/* Support at most 8 pointers in a BPF map value */
+	BPF_MAP_VALUE_OFF_MAX = 8,
+};
+
+struct bpf_map_value_off_desc {
+	u32 offset;
+	u32 btf_id;
+	struct btf *btf;
+	struct module *module;
+};
+
+struct bpf_map_value_off {
+	u32 nr_off;
+	struct bpf_map_value_off_desc off[];
+};
+
 struct bpf_map {
 	/* The first two cachelines with read-mostly members of which some
 	 * are also accessed in fast-path (e.g. ops, max_entries).
@@ -171,6 +188,7 @@ struct bpf_map {
 	u64 map_extra; /* any per-map-type extra fields */
 	u32 map_flags;
 	int spin_lock_off; /* >=0 valid offset, <0 error */
+	struct bpf_map_value_off *ptr_off_tab;
 	int timer_off; /* >=0 valid offset, <0 error */
 	u32 id;
 	int numa_node;
@@ -184,7 +202,7 @@ struct bpf_map {
 	char name[BPF_OBJ_NAME_LEN];
 	bool bypass_spec_v1;
 	bool frozen; /* write-once; write-protected by freeze_mutex */
-	/* 14 bytes hole */
+	/* 6 bytes hole */
 
 	/* The 3rd and 4th cacheline with misc members to avoid false sharing
 	 * particularly with refcounting.
@@ -217,6 +235,11 @@ static inline bool map_value_has_timer(const struct bpf_map *map)
 	return map->timer_off >= 0;
 }
 
+static inline bool map_value_has_ptr_to_btf_id(const struct bpf_map *map)
+{
+	return !IS_ERR_OR_NULL(map->ptr_off_tab);
+}
+
 static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
 {
 	if (unlikely(map_value_has_spin_lock(map)))
@@ -1490,6 +1513,11 @@ void bpf_prog_put(struct bpf_prog *prog);
 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
 
+struct bpf_map_value_off_desc *bpf_map_ptr_off_contains(struct bpf_map *map, u32 offset);
+void bpf_map_free_ptr_off_tab(struct bpf_map *map);
+struct bpf_map_value_off *bpf_map_copy_ptr_off_tab(const struct bpf_map *map);
+bool bpf_map_equal_ptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b);
+
 struct bpf_map *bpf_map_get(u32 ufd);
 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
 struct bpf_map *__bpf_map_get(struct fd f);
include/linux/btf.h +3 -0
index 36bc09b8e890..6592183aeb23 100644
@@ -26,6 +26,7 @@ struct btf_type;
 union bpf_attr;
 struct btf_show;
 struct btf_id_set;
+struct bpf_map;
 
 struct btf_kfunc_id_set {
 	struct module *owner;
@@ -123,6 +124,8 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
 			   u32 expected_offset, u32 expected_size);
 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
 int btf_find_timer(const struct btf *btf, const struct btf_type *t);
+int btf_find_ptr_to_btf_id(const struct btf *btf, const struct btf_type *t,
+			   struct bpf_map *map);
 bool btf_type_is_void(const struct btf_type *t);
 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
kernel/bpf/btf.c +127 -0

157 lines not on this page · raw message

kernel/bpf/map_in_map.c +4 -1
index 5cd8f5277279..293e41a4f0b3 100644
@@ -52,6 +52,7 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
 	inner_map_meta->max_entries = inner_map->max_entries;
 	inner_map_meta->spin_lock_off = inner_map->spin_lock_off;
 	inner_map_meta->timer_off = inner_map->timer_off;
+	inner_map_meta->ptr_off_tab = bpf_map_copy_ptr_off_tab(inner_map);
 	if (inner_map->btf) {
 		btf_get(inner_map->btf);
 		inner_map_meta->btf = inner_map->btf;
@@ -71,6 +72,7 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
 
 void bpf_map_meta_free(struct bpf_map *map_meta)
 {
+	bpf_map_free_ptr_off_tab(map_meta);
 	btf_put(map_meta->btf);
 	kfree(map_meta);
 }
@@ -83,7 +85,8 @@ bool bpf_map_meta_equal(const struct bpf_map *meta0,
 		meta0->key_size == meta1->key_size &&
 		meta0->value_size == meta1->value_size &&
 		meta0->timer_off == meta1->timer_off &&
-		meta0->map_flags == meta1->map_flags;
+		meta0->map_flags == meta1->map_flags &&
+		bpf_map_equal_ptr_off_tab(meta0, meta1);
 }
 
 void *bpf_map_fd_get_ptr(struct bpf_map *map,
kernel/bpf/syscall.c +135 -2

169 lines not on this page · raw message

kernel/bpf/verifier.c +148 -0

172 lines not on this page · raw message

Signature · 1 line
2.35.1
author maintainer
#9 lore raw

PATCH 4/15 bpf: Allow storing referenced PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi

This commit enables storing referenced PTR_TO_BTF_ID pointers in maps, with some restrictions to ensure the value of the pointer remains consistent, as it needs to be eventuall freed using a release function, either by the BPF program itself in a later invocation, or by the map's free path.

Such a pointer must be tagged using both 'btf_id' and 'ref' type tags on the type being pointed to by the pointer, in the map value's BTF. The verifier will only permit updating such pointers using BPF_XCHG instruction.

There are valid reasons to enforce this restriction:
- The pointer value must be consistent in face of concurrent modification, and any prior values contained in the map must also be released before a new one is moved into the map. To ensure proper transfer of this ownership, BPF_XCHG returns the old value, which the verifier would require the user to either free or move into another map, and released the reference held for the pointer being moved in.

Hence, after a BPF_XCHG on a map value, the user releases the reference for the pointer they are moving in, and acquires a reference for the old pointer returned by the exchange instruction (in src_reg).

In case of unreferenced PTR_TO_BTF_ID in maps, losing the old value by a store had no adverse effect. The only thing we need to ensure is that the actual word sized pointer is written and read concurrently using word sized instructions on the actual architecture, even if BPF ABI has 64-bit pointers, the underlying pointer value on 32-bit systems will be 32-bit, so emitting a load and store as two 32-bit sized loads and stores would still be valid, however doing the same on a 64-bit system would be wrong, as the pointer value being read can be inconsistent.

This is because while pointer dereference inside a BPF program is concerned, the verifier patches loads to PROBE_MEM loads, which support exception handling of faulting loads, but PTR_TO_BTF_ID can also be passed into BPF helpers and kernel functions, which do not have the same liberty.

- This also ensures that BPF programs executing concurrently never end up in a state where a certain pointer value was lost due to manipulations of the map value, thus leaking the reference that was moved in.

There is always an entity which releases the reference eventually, it will either be the map's free path (which will detect and release live pointers in the map), or the BPF program itself, which can exchange a referenced pointer with NULL and free the old reference.

- In case of multiple such pointers, doing many BPF_XCHG can be a bit costly, especially if those pointers are already protected by a BPF spin lock against concurrent modification. In the future, this support can be extended so that a single spin lock protects multiple such pointers and this move operation can be enforced using a helper, while also ensuring linearizability of the pointer move operations. This will amortize the cost of each individual BPF_XCHG that would be needed otherwise using a spin lock. However, this is work that has been left as an exercise for a future patch.

This mechanism would also require the user to indicate to the verifier which members of the map value are protected by the BPF spin lock, by using annotation in map's BTF.

---
 include/linux/bpf.h   |   5 ++
 kernel/bpf/btf.c      |  22 +++++++-
 kernel/bpf/verifier.c | 117 ++++++++++++++++++++++++++++++++++++------
 3 files changed, 126 insertions(+), 18 deletions(-)
include/linux/bpf.h +5 -0
index ce45ffb79f82..923b9f36c275 100644
@@ -160,11 +160,16 @@ enum {
 	BPF_MAP_VALUE_OFF_MAX = 8,
 };
 
+enum {
+	BPF_MAP_VALUE_OFF_F_REF = (1U << 0),
+};
+
 struct bpf_map_value_off_desc {
 	u32 offset;
 	u32 btf_id;
 	struct btf *btf;
 	struct module *module;
+	int flags;
 };
 
 struct bpf_map_value_off {
kernel/bpf/btf.c +20 -2
index 1edb5710e155..20124f4a421c 100644
@@ -3146,10 +3146,10 @@ static s32 btf_find_by_name_kind_all(const char *name, u32 kind, struct btf **bt
 static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 			       u32 off, int sz, void *data)
 {
+	bool btf_id_tag = false, ref_tag = false;
 	struct bpf_map_value_off *tab;
 	struct bpf_map *map = data;
 	struct module *mod = NULL;
-	bool btf_id_tag = false;
 	struct btf *kernel_btf;
 	int nr_off, ret;
 	s32 id;
@@ -3167,6 +3167,13 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 				goto end;
 			}
 			btf_id_tag = true;
+		} else if (!strcmp("kernel.bpf.ref", __btf_name_by_offset(btf, t->name_off))) {
+			/* repeated tag */
+			if (ref_tag) {
+				ret = -EINVAL;
+				goto end;
+			}
+			ref_tag = true;
 		} else if (!strncmp("kernel.", __btf_name_by_offset(btf, t->name_off),
 			   sizeof("kernel.") - 1)) {
 			/* TODO: Should we reject these when loading BTF? */
@@ -3177,8 +3184,14 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 		/* Look for next tag */
 		t = btf_type_by_id(btf, t->type);
 	}
-	if (!btf_id_tag)
+	if (!btf_id_tag) {
+		/* 'ref' tag must be specified together with 'btf_id' tag */
+		if (ref_tag) {
+			ret = -EINVAL;
+			goto end;
+		}
 		return 0;
+	}
 
 	/* Get the base type */
 	if (btf_type_is_modifier(t))
@@ -3215,6 +3228,10 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 
 	/* We take reference to make sure valid pointers into module data don't
 	 * become invalid across program invocation.
+	 *
+	 * We also need to hold a reference to the module, which corresponds to
+	 * the referenced type, as it has the destructor function we need to
+	 * call when map goes away and a live pointer exists at offset.
 	 */
 	if (btf_is_module(kernel_btf)) {
 		mod = btf_try_get_module(kernel_btf);
@@ -3228,6 +3245,7 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 	tab->off[nr_off].btf_id = id;
 	tab->off[nr_off].btf    = kernel_btf;
 	tab->off[nr_off].module = mod;
+	tab->off[nr_off].flags  = ref_tag ? BPF_MAP_VALUE_OFF_F_REF : 0;
 	tab->nr_off++;
 
 	return 0;
kernel/bpf/verifier.c +101 -16

252 lines not on this page · raw message

Signature · 1 line
2.35.1
author maintainer
#16 lore raw

PATCH 5/15 bpf: Allow storing PTR_TO_PERCPU_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi

Make adjustments to the code to allow storing PTR_TO_PERCPU_BTF_ID in a map. Note that these are not yet supported as referenced pointers, so that is explicitly disallowed during BTF tag parsing. Similar to 'ref' tag, a new 'percpu' tag composes with 'btf_id' tag on the pointed to type to hint that it is a percpu btf_id pointer.

---
 include/linux/bpf.h   |  3 ++-
 kernel/bpf/btf.c      | 27 ++++++++++++++++++++++-----
 kernel/bpf/verifier.c | 37 ++++++++++++++++++++++++++++---------
 3 files changed, 52 insertions(+), 15 deletions(-)
include/linux/bpf.h +2 -1
index 923b9f36c275..843c8c01cf9d 100644
@@ -161,7 +161,8 @@ enum {
 };
 
 enum {
-	BPF_MAP_VALUE_OFF_F_REF = (1U << 0),
+	BPF_MAP_VALUE_OFF_F_REF    = (1U << 0),
+	BPF_MAP_VALUE_OFF_F_PERCPU = (1U << 1),
 };
 
 struct bpf_map_value_off_desc {
kernel/bpf/btf.c +22 -5
index 20124f4a421c..eb57584ee0a8 100644
@@ -3146,12 +3146,12 @@ static s32 btf_find_by_name_kind_all(const char *name, u32 kind, struct btf **bt
 static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 			       u32 off, int sz, void *data)
 {
-	bool btf_id_tag = false, ref_tag = false;
+	bool btf_id_tag = false, ref_tag = false, percpu_tag = false;
 	struct bpf_map_value_off *tab;
 	struct bpf_map *map = data;
+	int nr_off, ret, flags = 0;
 	struct module *mod = NULL;
 	struct btf *kernel_btf;
-	int nr_off, ret;
 	s32 id;
 
 	/* For PTR, sz is always == 8 */
@@ -3174,6 +3174,13 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 				goto end;
 			}
 			ref_tag = true;
+		} else if (!strcmp("kernel.bpf.percpu", __btf_name_by_offset(btf, t->name_off))) {
+			/* repeated tag */
+			if (percpu_tag) {
+				ret = -EINVAL;
+				goto end;
+			}
+			percpu_tag = true;
 		} else if (!strncmp("kernel.", __btf_name_by_offset(btf, t->name_off),
 			   sizeof("kernel.") - 1)) {
 			/* TODO: Should we reject these when loading BTF? */
@@ -3185,13 +3192,18 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 		t = btf_type_by_id(btf, t->type);
 	}
 	if (!btf_id_tag) {
-		/* 'ref' tag must be specified together with 'btf_id' tag */
-		if (ref_tag) {
+		/* 'ref' or 'percpu' tag must be specified together with 'btf_id' tag */
+		if (ref_tag || percpu_tag) {
 			ret = -EINVAL;
 			goto end;
 		}
 		return 0;
 	}
+	/* referenced percpu btf_id pointer is not yet supported */
+	if (ref_tag && percpu_tag) {
+		ret = -EINVAL;
+		goto end;
+	}
 
 	/* Get the base type */
 	if (btf_type_is_modifier(t))
@@ -3241,11 +3253,16 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 		}
 	}
 
+	if (ref_tag)
+		flags |= BPF_MAP_VALUE_OFF_F_REF;
+	else if (percpu_tag)
+		flags |= BPF_MAP_VALUE_OFF_F_PERCPU;
+
 	tab->off[nr_off].offset = off;
 	tab->off[nr_off].btf_id = id;
 	tab->off[nr_off].btf    = kernel_btf;
 	tab->off[nr_off].module = mod;
-	tab->off[nr_off].flags  = ref_tag ? BPF_MAP_VALUE_OFF_F_REF : 0;
+	tab->off[nr_off].flags  = flags;
 	tab->nr_off++;
 
 	return 0;
kernel/bpf/verifier.c +28 -9
index a9d8c0d3c919..00d6ab49033d 100644
@@ -1559,12 +1559,13 @@ static void mark_btf_ld_reg(struct bpf_verifier_env *env,
 			    struct btf *btf, u32 btf_id,
 			    enum bpf_type_flag flag)
 {
-	if (reg_type == SCALAR_VALUE) {
+	if (reg_type == SCALAR_VALUE ||
+	    WARN_ON_ONCE(reg_type != PTR_TO_BTF_ID && reg_type != PTR_TO_PERCPU_BTF_ID)) {
 		mark_reg_unknown(env, regs, regno);
 		return;
 	}
 	mark_reg_known_zero(env, regs, regno);
-	regs[regno].type = PTR_TO_BTF_ID | flag;
+	regs[regno].type = reg_type | flag;
 	regs[regno].btf = btf;
 	regs[regno].btf_id = btf_id;
 }
@@ -3478,10 +3479,18 @@ static int map_ptr_to_btf_id_match_type(struct bpf_verifier_env *env,
 					bool ref_ptr)
 {
 	const char *targ_name = kernel_type_name(off_desc->btf, off_desc->btf_id);
+	enum bpf_reg_type reg_type;
 	const char *reg_name = "";
 
-	if (reg->type != PTR_TO_BTF_ID && reg->type != PTR_TO_BTF_ID_OR_NULL)
-		goto end;
+	if (off_desc->flags & BPF_MAP_VALUE_OFF_F_PERCPU) {
+		if (reg->type != PTR_TO_PERCPU_BTF_ID &&
+		    reg->type != (PTR_TO_PERCPU_BTF_ID | PTR_MAYBE_NULL))
+			goto end;
+	} else { /* referenced and unreferenced case */
+		if (reg->type != PTR_TO_BTF_ID &&
+		    reg->type != (PTR_TO_BTF_ID | PTR_MAYBE_NULL))
+			goto end;
+	}
 
 	if (!btf_is_kernel(reg->btf)) {
 		verbose(env, "R%d must point to kernel BTF\n", regno);
@@ -3524,11 +3533,16 @@ static int map_ptr_to_btf_id_match_type(struct bpf_verifier_env *env,
 	if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off,
 				  off_desc->btf, off_desc->btf_id))
 		goto end;
+
 	return 0;
 end:
+	if (off_desc->flags & BPF_MAP_VALUE_OFF_F_PERCPU)
+		reg_type = PTR_TO_PERCPU_BTF_ID | PTR_MAYBE_NULL;
+	else
+		reg_type = PTR_TO_BTF_ID | PTR_MAYBE_NULL;
 	verbose(env, "invalid btf_id pointer access, R%d type=%s%s ", regno,
 		reg_type_str(env, reg->type), reg_name);
-	verbose(env, "expected=%s%s\n", reg_type_str(env, PTR_TO_BTF_ID), targ_name);
+	verbose(env, "expected=%s%s\n", reg_type_str(env, reg_type), targ_name);
 	return -EINVAL;
 }
 
@@ -3543,10 +3557,11 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 {
 	struct bpf_reg_state *reg = reg_state(env, regno), *val_reg;
 	struct bpf_insn *insn = &env->prog->insnsi[insn_idx];
+	enum bpf_reg_type reg_type = PTR_TO_BTF_ID;
+	bool ref_ptr = false, percpu_ptr = false;
 	struct bpf_map_value_off_desc *off_desc;
 	int insn_class = BPF_CLASS(insn->code);
 	struct bpf_map *map = reg->map_ptr;
-	bool ref_ptr = false;
 	u32 ref_obj_id = 0;
 	int ret;
 
@@ -3561,7 +3576,6 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 	off_desc = bpf_map_ptr_off_contains(map, off + reg->var_off.value);
 	if (!off_desc)
 		return 0;
-	ref_ptr = off_desc->flags & BPF_MAP_VALUE_OFF_F_REF;
 
 	if (WARN_ON_ONCE(size != bpf_size_to_bytes(BPF_DW)))
 		return -EACCES;
@@ -3574,6 +3588,11 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 		return -EPERM;
 	}
 
+	ref_ptr = off_desc->flags & BPF_MAP_VALUE_OFF_F_REF;
+	percpu_ptr = off_desc->flags & BPF_MAP_VALUE_OFF_F_PERCPU;
+	if (percpu_ptr)
+		reg_type = PTR_TO_PERCPU_BTF_ID;
+
 	if (is_xchg_insn(insn)) {
 		/* We do checks and updates during register fill call for fetch case */
 		if (t != BPF_READ || value_regno < 0)
@@ -3603,7 +3622,7 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 			ref_obj_id = ret;
 		}
 		/* val_reg might be NULL at this point */
-		mark_btf_ld_reg(env, cur_regs(env), value_regno, PTR_TO_BTF_ID, off_desc->btf,
+		mark_btf_ld_reg(env, cur_regs(env), value_regno, reg_type, off_desc->btf,
 				off_desc->btf_id, PTR_MAYBE_NULL);
 		/* __mark_ptr_or_null_regs needs ref_obj_id == id to clear
 		 * reference state for ptr == NULL branch.
@@ -3621,7 +3640,7 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 		/* We can simply mark the value_regno receiving the pointer
 		 * value from map as PTR_TO_BTF_ID, with the correct type.
 		 */
-		mark_btf_ld_reg(env, cur_regs(env), value_regno, PTR_TO_BTF_ID, off_desc->btf,
+		mark_btf_ld_reg(env, cur_regs(env), value_regno, reg_type, off_desc->btf,
 				off_desc->btf_id, PTR_MAYBE_NULL);
 		val_reg->id = ++env->id_gen;
 	} else if (insn_class == BPF_STX) {
Signature · 1 line
2.35.1
author maintainer
#18 lore raw

PATCH 6/15 bpf: Allow storing __user PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi

Recently, verifier gained __user annotation support [0] where it prevents BPF program from normally derefering user memory pointer in the kernel, and instead requires use of bpf_probe_read_user. We can allow the user to also store these pointers in BPF maps, with the logic that whenever user loads it from the BPF map, it gets marked as MEM_USER.

[0]: https://lore.kernel.org/bpf/[email protected]

---
 include/linux/bpf.h   |  1 +
 kernel/bpf/btf.c      | 20 +++++++++++++++-----
 kernel/bpf/verifier.c | 21 +++++++++++++++------
 3 files changed, 31 insertions(+), 11 deletions(-)
include/linux/bpf.h +1 -0
index 843c8c01cf9d..37ca92f4c7b7 100644
@@ -163,6 +163,7 @@ enum {
 enum {
 	BPF_MAP_VALUE_OFF_F_REF    = (1U << 0),
 	BPF_MAP_VALUE_OFF_F_PERCPU = (1U << 1),
+	BPF_MAP_VALUE_OFF_F_USER   = (1U << 2),
 };
 
 struct bpf_map_value_off_desc {
kernel/bpf/btf.c +15 -5
index eb57584ee0a8..bafceae90c32 100644
@@ -3146,7 +3146,7 @@ static s32 btf_find_by_name_kind_all(const char *name, u32 kind, struct btf **bt
 static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 			       u32 off, int sz, void *data)
 {
-	bool btf_id_tag = false, ref_tag = false, percpu_tag = false;
+	bool btf_id_tag = false, ref_tag = false, percpu_tag = false, user_tag = false;
 	struct bpf_map_value_off *tab;
 	struct bpf_map *map = data;
 	int nr_off, ret, flags = 0;
@@ -3181,6 +3181,13 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 				goto end;
 			}
 			percpu_tag = true;
+		} else if (!strcmp("kernel.bpf.user", __btf_name_by_offset(btf, t->name_off))) {
+			/* repeated tag */
+			if (user_tag) {
+				ret = -EINVAL;
+				goto end;
+			}
+			user_tag = true;
 		} else if (!strncmp("kernel.", __btf_name_by_offset(btf, t->name_off),
 			   sizeof("kernel.") - 1)) {
 			/* TODO: Should we reject these when loading BTF? */
@@ -3192,15 +3199,16 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 		t = btf_type_by_id(btf, t->type);
 	}
 	if (!btf_id_tag) {
-		/* 'ref' or 'percpu' tag must be specified together with 'btf_id' tag */
-		if (ref_tag || percpu_tag) {
+		/* 'ref', 'percpu', 'user' tag must be specified together with 'btf_id' tag */
+		if (ref_tag || percpu_tag || user_tag) {
 			ret = -EINVAL;
 			goto end;
 		}
 		return 0;
 	}
-	/* referenced percpu btf_id pointer is not yet supported */
-	if (ref_tag && percpu_tag) {
+	/* All three are mutually exclusive */
+	ret = ref_tag + percpu_tag + user_tag;
+	if (ret > 1) {
 		ret = -EINVAL;
 		goto end;
 	}
@@ -3257,6 +3265,8 @@ static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
 		flags |= BPF_MAP_VALUE_OFF_F_REF;
 	else if (percpu_tag)
 		flags |= BPF_MAP_VALUE_OFF_F_PERCPU;
+	else if (user_tag)
+		flags |= BPF_MAP_VALUE_OFF_F_USER;
 
 	tab->off[nr_off].offset = off;
 	tab->off[nr_off].btf_id = id;
kernel/bpf/verifier.c +15 -6
index 00d6ab49033d..28da858bb921 100644
@@ -3482,7 +3482,11 @@ static int map_ptr_to_btf_id_match_type(struct bpf_verifier_env *env,
 	enum bpf_reg_type reg_type;
 	const char *reg_name = "";
 
-	if (off_desc->flags & BPF_MAP_VALUE_OFF_F_PERCPU) {
+	if (off_desc->flags & BPF_MAP_VALUE_OFF_F_USER) {
+		if (reg->type != (PTR_TO_BTF_ID | MEM_USER) &&
+		    reg->type != (PTR_TO_BTF_ID | PTR_MAYBE_NULL | MEM_USER))
+			goto end;
+	} else if (off_desc->flags & BPF_MAP_VALUE_OFF_F_PERCPU) {
 		if (reg->type != PTR_TO_PERCPU_BTF_ID &&
 		    reg->type != (PTR_TO_PERCPU_BTF_ID | PTR_MAYBE_NULL))
 			goto end;
@@ -3536,7 +3540,9 @@ static int map_ptr_to_btf_id_match_type(struct bpf_verifier_env *env,
 
 	return 0;
 end:
-	if (off_desc->flags & BPF_MAP_VALUE_OFF_F_PERCPU)
+	if (off_desc->flags & BPF_MAP_VALUE_OFF_F_USER)
+		reg_type = PTR_TO_BTF_ID | PTR_MAYBE_NULL | MEM_USER;
+	else if (off_desc->flags & BPF_MAP_VALUE_OFF_F_PERCPU)
 		reg_type = PTR_TO_PERCPU_BTF_ID | PTR_MAYBE_NULL;
 	else
 		reg_type = PTR_TO_BTF_ID | PTR_MAYBE_NULL;
@@ -3556,14 +3562,14 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 				   struct bpf_reg_state *atomic_load_reg)
 {
 	struct bpf_reg_state *reg = reg_state(env, regno), *val_reg;
+	bool ref_ptr = false, percpu_ptr = false, user_ptr = false;
 	struct bpf_insn *insn = &env->prog->insnsi[insn_idx];
 	enum bpf_reg_type reg_type = PTR_TO_BTF_ID;
-	bool ref_ptr = false, percpu_ptr = false;
 	struct bpf_map_value_off_desc *off_desc;
 	int insn_class = BPF_CLASS(insn->code);
+	int ret, reg_flags = PTR_MAYBE_NULL;
 	struct bpf_map *map = reg->map_ptr;
 	u32 ref_obj_id = 0;
-	int ret;
 
 	/* Things we already checked for in check_map_access:
 	 *  - Reject cases where variable offset may touch BTF ID pointer
@@ -3590,8 +3596,11 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 
 	ref_ptr = off_desc->flags & BPF_MAP_VALUE_OFF_F_REF;
 	percpu_ptr = off_desc->flags & BPF_MAP_VALUE_OFF_F_PERCPU;
+	user_ptr = off_desc->flags & BPF_MAP_VALUE_OFF_F_USER;
 	if (percpu_ptr)
 		reg_type = PTR_TO_PERCPU_BTF_ID;
+	else if (user_ptr)
+		reg_flags |= MEM_USER;
 
 	if (is_xchg_insn(insn)) {
 		/* We do checks and updates during register fill call for fetch case */
@@ -3623,7 +3632,7 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 		}
 		/* val_reg might be NULL at this point */
 		mark_btf_ld_reg(env, cur_regs(env), value_regno, reg_type, off_desc->btf,
-				off_desc->btf_id, PTR_MAYBE_NULL);
+				off_desc->btf_id, reg_flags);
 		/* __mark_ptr_or_null_regs needs ref_obj_id == id to clear
 		 * reference state for ptr == NULL branch.
 		 */
@@ -3641,7 +3650,7 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 		 * value from map as PTR_TO_BTF_ID, with the correct type.
 		 */
 		mark_btf_ld_reg(env, cur_regs(env), value_regno, reg_type, off_desc->btf,
-				off_desc->btf_id, PTR_MAYBE_NULL);
+				off_desc->btf_id, reg_flags);
 		val_reg->id = ++env->id_gen;
 	} else if (insn_class == BPF_STX) {
 		if (WARN_ON_ONCE(value_regno < 0))
Signature · 1 line
2.35.1
author maintainer
#19 lore raw

PATCH 7/15 bpf: Prevent escaping of pointers loaded from maps

In reply to Kumar Kartikeya Dwivedi

While we can guarantee that even for unreferenced pointer, the object pointer points to being freed etc. can be handled by the verifier's exception handling (normal load patching to PROBE_MEM loads), we still cannot allow the user to pass these pointers to BPF helpers and kfunc, because the same exception handling won't be done for accesses inside the kernel.

Hence introduce a new type flag, PTR_UNTRUSTED, which is used to mark all registers loading unreferenced PTR_TO_BTF_ID from BPF maps, and ensure they can never escape the BPF program and into the kernel by way of calling stable/unstable helpers.

Adjust the check in check_mem_access so that we allow calling check_ptr_to_btf_access only when no or PTR_UNTRUSTED type flag is set.

---
 include/linux/bpf.h   |  7 +++++++
 kernel/bpf/verifier.c | 29 ++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 3 deletions(-)
include/linux/bpf.h +7 -0
index 37ca92f4c7b7..ae599aaf8d4c 100644
@@ -364,6 +364,13 @@ enum bpf_type_flag {
 	/* MEM is in user address space. */
 	MEM_USER		= BIT(3 + BPF_BASE_TYPE_BITS),
 
+	/* PTR is not trusted. This is only used with PTR_TO_BTF_ID, to mark
+	 * unrefcounted pointers loaded from map value, so that they can only
+	 * be dereferenced but not escape the BPF program into the kernel (i.e.
+	 * cannot be passed as arguments to kfunc or bpf helpers).
+	 */
+	PTR_UNTRUSTED		= BIT(4 + BPF_BASE_TYPE_BITS),
+
 	__BPF_TYPE_LAST_FLAG	= MEM_USER,
 };
 
kernel/bpf/verifier.c +26 -3
index 28da858bb921..0a2cd21d9ec1 100644
@@ -582,6 +582,8 @@ static const char *reg_type_str(struct bpf_verifier_env *env,
 		strncpy(prefix, "alloc_", 32);
 	if (type & MEM_USER)
 		strncpy(prefix, "user_", 32);
+	if (type & PTR_UNTRUSTED)
+		strncpy(prefix, "untrusted_", 32);
 
 	snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s",
 		 prefix, str[base_type(type)], postfix);
@@ -3490,10 +3492,17 @@ static int map_ptr_to_btf_id_match_type(struct bpf_verifier_env *env,
 		if (reg->type != PTR_TO_PERCPU_BTF_ID &&
 		    reg->type != (PTR_TO_PERCPU_BTF_ID | PTR_MAYBE_NULL))
 			goto end;
-	} else { /* referenced and unreferenced case */
+	} else if (off_desc->flags & BPF_MAP_VALUE_OFF_F_REF) {
+		/* register state (ref_obj_id) must be checked by caller */
 		if (reg->type != PTR_TO_BTF_ID &&
 		    reg->type != (PTR_TO_BTF_ID | PTR_MAYBE_NULL))
 			goto end;
+	} else { /* only unreferenced case accepts untrusted pointers */
+		if (reg->type != PTR_TO_BTF_ID &&
+		    reg->type != (PTR_TO_BTF_ID | PTR_MAYBE_NULL) &&
+		    reg->type != (PTR_TO_BTF_ID | PTR_UNTRUSTED) &&
+		    reg->type != (PTR_TO_BTF_ID | PTR_MAYBE_NULL | PTR_UNTRUSTED))
+			goto end;
 	}
 
 	if (!btf_is_kernel(reg->btf)) {
@@ -3597,10 +3606,13 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 	ref_ptr = off_desc->flags & BPF_MAP_VALUE_OFF_F_REF;
 	percpu_ptr = off_desc->flags & BPF_MAP_VALUE_OFF_F_PERCPU;
 	user_ptr = off_desc->flags & BPF_MAP_VALUE_OFF_F_USER;
+
 	if (percpu_ptr)
 		reg_type = PTR_TO_PERCPU_BTF_ID;
 	else if (user_ptr)
 		reg_flags |= MEM_USER;
+	else
+		reg_flags |= PTR_UNTRUSTED;
 
 	if (is_xchg_insn(insn)) {
 		/* We do checks and updates during register fill call for fetch case */
@@ -3629,6 +3641,10 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 			if (ret < 0)
 				return ret;
 			ref_obj_id = ret;
+			/* Unset PTR_UNTRUSTED, so that it can be passed to bpf
+			 * helpers or kfunc.
+			 */
+			reg_flags &= ~PTR_UNTRUSTED;
 		}
 		/* val_reg might be NULL at this point */
 		mark_btf_ld_reg(env, cur_regs(env), value_regno, reg_type, off_desc->btf,
@@ -4454,6 +4470,12 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
 	if (ret < 0)
 		return ret;
 
+	/* If this is an untrusted pointer, all btf_id pointers formed by
+	 * walking it also inherit the untrusted flag.
+	 */
+	if (type_flag(reg->type) & PTR_UNTRUSTED)
+		flag |= PTR_UNTRUSTED;
+
 	if (atype == BPF_READ && value_regno >= 0)
 		mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id, flag);
 
@@ -4804,7 +4826,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 		err = check_tp_buffer_access(env, reg, regno, off, size);
 		if (!err && t == BPF_READ && value_regno >= 0)
 			mark_reg_unknown(env, regs, value_regno);
-	} else if (reg->type == PTR_TO_BTF_ID) {
+	} else if (base_type(reg->type) == PTR_TO_BTF_ID && !(type_flag(reg->type) & ~PTR_UNTRUSTED)) {
 		err = check_ptr_to_btf_access(env, regs, regno, off, size, t,
 					      value_regno);
 	} else if (reg->type == CONST_PTR_TO_MAP) {
@@ -13041,7 +13063,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
 		if (!ctx_access)
 			continue;
 
-		switch (env->insn_aux_data[i + delta].ptr_type) {
+		switch ((int)env->insn_aux_data[i + delta].ptr_type) {
 		case PTR_TO_CTX:
 			if (!ops->convert_ctx_access)
 				continue;
@@ -13058,6 +13080,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
 			convert_ctx_access = bpf_xdp_sock_convert_ctx_access;
 			break;
 		case PTR_TO_BTF_ID:
+		case PTR_TO_BTF_ID | PTR_UNTRUSTED:
 			if (type == BPF_READ) {
 				insn->code = BPF_LDX | BPF_PROBE_MEM |
 					BPF_SIZE((insn)->code);
Signature · 1 line
2.35.1
author maintainer
#20 lore raw

PATCH 8/15 bpf: Adapt copy_map_value for multiple offset case

In reply to Kumar Kartikeya Dwivedi

The changes in this patch deserve closer look, so it has been split into its own independent patch. While earlier we just had to skip two objects at most while copying in and out of map, now we have potentially many objects (at most 8 + 2 = 10, due to the BPF_MAP_VALUE_OFF_MAX limit).

Hence, divide the copy_map_value function into an inlined fast path and function call to slowpath. The slowpath handles the case of > 3 offsets, while we handle the most common cases (0, 1, 2, or 3 offsets) in the inline function itself.

In copy_map_value_slow, we use 11 offsets, just to make the for loop that copies the value free of edge cases for the last offset, by using map->value_size as final offset to subtract remaining area to copy from.

---
 include/linux/bpf.h  | 43 +++++++++++++++++++++++++++++++---
 kernel/bpf/syscall.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 3 deletions(-)
include/linux/bpf.h +40 -3
index ae599aaf8d4c..5d845ca02eba 100644
@@ -253,12 +253,22 @@ static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
 		memset(dst + map->spin_lock_off, 0, sizeof(struct bpf_spin_lock));
 	if (unlikely(map_value_has_timer(map)))
 		memset(dst + map->timer_off, 0, sizeof(struct bpf_timer));
+	if (unlikely(map_value_has_ptr_to_btf_id(map))) {
+		struct bpf_map_value_off *tab = map->ptr_off_tab;
+		int i;
+
+		for (i = 0; i < tab->nr_off; i++)
+			*(u64 *)(dst + tab->off[i].offset) = 0;
+	}
 }
 
+void copy_map_value_slow(struct bpf_map *map, void *dst, void *src, u32 s_off,
+			 u32 s_sz, u32 t_off, u32 t_sz);
+
 /* copy everything but bpf_spin_lock and bpf_timer. There could be one of each. */
 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
 {
-	u32 s_off = 0, s_sz = 0, t_off = 0, t_sz = 0;
+	u32 s_off = 0, s_sz = 0, t_off = 0, t_sz = 0, p_off = 0, p_sz = 0;
 
 	if (unlikely(map_value_has_spin_lock(map))) {
 		s_off = map->spin_lock_off;
@@ -268,13 +278,40 @@ static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
 		t_off = map->timer_off;
 		t_sz = sizeof(struct bpf_timer);
 	}
+	/* Multiple offset case is slow, offload to function */
+	if (unlikely(map_value_has_ptr_to_btf_id(map))) {
+		struct bpf_map_value_off *tab = map->ptr_off_tab;
+
+		/* Inline the likely common case */
+		if (likely(tab->nr_off == 1)) {
+			p_off = tab->off[0].offset;
+			p_sz = sizeof(u64);
+		} else {
+			copy_map_value_slow(map, dst, src, s_off, s_sz, t_off, t_sz);
+			return;
+		}
+	}
+
+	if (unlikely(s_sz || t_sz || p_sz)) {
+		/* The order is p_off, t_off, s_off, use insertion sort */
 
-	if (unlikely(s_sz || t_sz)) {
+		if (t_off < p_off || !t_sz) {
+			swap(t_off, p_off);
+			swap(t_sz, p_sz);
+		}
 		if (s_off < t_off || !s_sz) {
 			swap(s_off, t_off);
 			swap(s_sz, t_sz);
+			if (t_off < p_off || !t_sz) {
+				swap(t_off, p_off);
+				swap(t_sz, p_sz);
+			}
 		}
-		memcpy(dst, src, t_off);
+
+		memcpy(dst, src, p_off);
+		memcpy(dst + p_off + p_sz,
+		       src + p_off + p_sz,
+		       t_off - p_off - p_sz);
 		memcpy(dst + t_off + t_sz,
 		       src + t_off + t_sz,
 		       s_off - t_off - t_sz);
kernel/bpf/syscall.c +55 -0

67 lines not on this page · raw message

Signature · 1 line
2.35.1
author maintainer
#24 lore raw

PATCH 9/15 bpf: Populate pairs of btf_id and destructor kfunc in btf

In reply to Kumar Kartikeya Dwivedi

To support storing referenced PTR_TO_BTF_ID in maps, we require associating a specific BTF ID with a 'destructor' kfunc. This is because we need to release a live referenced pointer at a certain offset in map value from the map destruction path, otherwise we end up leaking resources.

Hence, introduce support for passing an array of btf_id, kfunc_btf_id pairs that denote a BTF ID and its associated release function. Then, add an accessor 'btf_find_dtor_kfunc' which can be used to look up the destructor kfunc of a certain BTF ID. If found, we can use it to free the object from the map free path.

The registration of these pairs also serve as a whitelist of structures which are allowed as referenced PTR_TO_BTF_ID in a BPF map, because without finding the destructor kfunc, we will bail and return an error.

---
 include/linux/btf.h |  17 +++++++
 kernel/bpf/btf.c    | 109 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+)
include/linux/btf.h +17 -0
index 6592183aeb23..a304a1ea39d9 100644
@@ -41,6 +41,11 @@ struct btf_kfunc_id_set {
 	};
 };
 
+struct btf_id_dtor_kfunc {
+	u32 btf_id;
+	u32 kfunc_btf_id;
+};
+
 extern const struct file_operations btf_fops;
 
 void btf_get(struct btf *btf);
@@ -347,6 +352,9 @@ bool btf_kfunc_id_set_contains(const struct btf *btf,
 			       enum btf_kfunc_type type, u32 kfunc_btf_id);
 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
 			      const struct btf_kfunc_id_set *s);
+s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
+int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
+				struct module *owner);
 #else
 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
 						    u32 type_id)
@@ -370,6 +378,15 @@ static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
 {
 	return 0;
 }
+static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
+{
+	return -ENOENT;
+}
+static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
+					      u32 add_cnt, struct module *owner)
+{
+	return 0;
+}
 #endif
 
 #endif
kernel/bpf/btf.c +109 -0

141 lines not on this page · raw message

Signature · 1 line
2.35.1
author maintainer
#30 lore raw

PATCH 12/15 net/netfilter: Add bpf_ct_kptr_get helper

In reply to Kumar Kartikeya Dwivedi

Require some more feedback on whether this is OK, before refactoring netfilter functions to share code to increment reference and match the tuple. Also probably need to work on allowing taking reference to struct net * to save another lookup inside this function.

---
 include/net/netfilter/nf_conntrack_core.h |  17 +++
 net/netfilter/nf_conntrack_bpf.c          | 132 +++++++++++++++++-----
 net/netfilter/nf_conntrack_core.c         |  17 ---
 3 files changed, 119 insertions(+), 47 deletions(-)
include/net/netfilter/nf_conntrack_core.h +17 -0
index 13807ea94cd2..09389769dce3 100644
@@ -51,6 +51,23 @@ nf_conntrack_find_get(struct net *net,
 
 int __nf_conntrack_confirm(struct sk_buff *skb);
 
+static inline bool
+nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
+		const struct nf_conntrack_tuple *tuple,
+		const struct nf_conntrack_zone *zone,
+		const struct net *net)
+{
+	struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
+
+	/* A conntrack can be recreated with the equal tuple,
+	 * so we need to check that the conntrack is confirmed
+	 */
+	return nf_ct_tuple_equal(tuple, &h->tuple) &&
+	       nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
+	       nf_ct_is_confirmed(ct) &&
+	       net_eq(net, nf_ct_net(ct));
+}
+
 /* Confirm a connection: returns NF_DROP if packet must be dropped. */
 static inline int nf_conntrack_confirm(struct sk_buff *skb)
 {
net/netfilter/nf_conntrack_bpf.c +102 -30

189 lines not on this page · raw message

net/netfilter/nf_conntrack_core.c +0 -17
index 9b7f9c966f73..0aae98f60769 100644
@@ -710,23 +710,6 @@ bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
 }
 EXPORT_SYMBOL_GPL(nf_ct_delete);
 
-static inline bool
-nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
-		const struct nf_conntrack_tuple *tuple,
-		const struct nf_conntrack_zone *zone,
-		const struct net *net)
-{
-	struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
-
-	/* A conntrack can be recreated with the equal tuple,
-	 * so we need to check that the conntrack is confirmed
-	 */
-	return nf_ct_tuple_equal(tuple, &h->tuple) &&
-	       nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
-	       nf_ct_is_confirmed(ct) &&
-	       net_eq(net, nf_ct_net(ct));
-}
-
 static inline bool
 nf_ct_match(const struct nf_conn *ct1, const struct nf_conn *ct2)
 {
Signature · 1 line
2.35.1
author maintainer
#29 lore raw

PATCH 11/15 bpf: Teach verifier about kptr_get style kfunc helpers

In reply to Kumar Kartikeya Dwivedi

We introduce a new style of kfunc helpers, namely *_kptr_get, where they take pointer to the map value which points to a referenced kernel pointer contained in the map. Since this is referenced, only BPF_XCHG from kernel and BPF side is allowed to change the current value, and each pointer that resides in that location would be referenced, and RCU protected (must be kept in mind while adding kernel types embeddable as reference kptr in BPF maps).

This means that if do the load of the pointer value in an RCU read section, and find a live pointer, then as long as we hold RCU read lock, it won't be freed by a parallel xchg + release operation. This allows us to implement a safe refcount increment scheme. Hence, enforce that first argument of all such kfunc is a proper PTR_TO_MAP_VALUE pointing at the right offset to referenced pointer.

For the rest of the arguments, they are subjected to typical kfunc argument checks, hence allowing some flexibility in passing more intent into how the reference should be taken.

For instance, in case of struct nf_conn, it is not freed until all RCU grace period ends, but can still be reused for another tuple once refcount has dropped to zero. Hence, a bpf_ct_kptr_get helper not only needs to call refcount_inc_not_zero, but also do a tuple match after incrementing the reference, and when it fails to match it, put the reference again and return NULL.

This can be implemented easily if we allow passing additional parameters to the bpf_ct_kptr_get kfunc, like a struct bpf_sock_tuple * and a tuple__sz pair.

---
 include/linux/btf.h   |  2 ++
 kernel/bpf/btf.c      | 48 +++++++++++++++++++++++++++++++++++++++++--
 kernel/bpf/verifier.c |  9 ++++----
 3 files changed, 53 insertions(+), 6 deletions(-)
include/linux/btf.h +2 -0
index c7e75be9637f..10918ac0e55f 100644
@@ -17,6 +17,7 @@ enum btf_kfunc_type {
 	BTF_KFUNC_TYPE_ACQUIRE,
 	BTF_KFUNC_TYPE_RELEASE,
 	BTF_KFUNC_TYPE_RET_NULL,
+	BTF_KFUNC_TYPE_KPTR_ACQUIRE,
 	BTF_KFUNC_TYPE_MAX,
 };
 
@@ -36,6 +37,7 @@ struct btf_kfunc_id_set {
 			struct btf_id_set *acquire_set;
 			struct btf_id_set *release_set;
 			struct btf_id_set *ret_null_set;
+			struct btf_id_set *kptr_acquire_set;
 		};
 		struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX];
 	};
kernel/bpf/btf.c +46 -2

70 lines not on this page · raw message

kernel/bpf/verifier.c +5 -4
index 0a2cd21d9ec1..a4ff951ea46f 100644
@@ -3657,11 +3657,12 @@ static int check_map_ptr_to_btf_id(struct bpf_verifier_env *env, u32 regno, int
 	} else if (insn_class == BPF_LDX) {
 		if (WARN_ON_ONCE(value_regno < 0))
 			return -EFAULT;
+		/* We allow loading referenced pointer, but mark it as
+		 * untrusted. User needs to use a kptr_get helper to obtain a
+		 * trusted refcounted PTR_TO_BTF_ID by passing in the map
+		 * value pointing to the referenced pointer.
+		 */
 		val_reg = reg_state(env, value_regno);
-		if (ref_ptr) {
-			verbose(env, "referenced btf_id pointer can only be accessed using BPF_XCHG\n");
-			return -EACCES;
-		}
 		/* We can simply mark the value_regno receiving the pointer
 		 * value from map as PTR_TO_BTF_ID, with the correct type.
 		 */
Signature · 1 line
2.35.1
author maintainer
#25 lore raw

PATCH 10/15 bpf: Wire up freeing of referenced PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi

A destructor kfunc can be defined as void func(type *), where type may be void or any other pointer type as per convenience. The kfunc doesn't have to take care about the map side pointer width, as it will be passed a pointer after converting the u64 address embedded in the map.

In this patch, we ensure that the type is sane and capture the function pointer into off_desc of ptr_off_tab for the specific pointer offset, with the invariant that the dtor pointer is always set when 'ref' tag is applied to the pointer's pointee type, which is indicated by the flag BPF_MAP_VALUE_OFF_F_REF.

Note that only BTF IDs whose destructor kfunc is registered, thus become the allowed BTF IDs for embedding as referenced PTR_TO_BTF_ID. Hence btf_find_dtor_kfunc serves the purpose of finding dtor kfunc BTF ID, as well acting as a check against the whitelist of allowed BTF IDs for this purpose.

Finally, wire up the actual freeing of the referenced pointer if any at all available offsets, so that no references are leaked after the BPF map goes away and the BPF program previously moved the ownership a referenced pointer into it.

The behavior is similar to BPF timers, where bpf_map_{update,delete}_elem will free any existing referenced PTR_TO_BTF_ID. The same case is with LRU map's bpf_lru_push_free/htab_lru_push_free functions, which are extended to reset and free referenced pointers.

---
 include/linux/bpf.h   |  3 ++
 include/linux/btf.h   |  2 ++
 kernel/bpf/arraymap.c | 13 ++++++--
 kernel/bpf/btf.c      | 72 ++++++++++++++++++++++++++++++++++++++++++-
 kernel/bpf/hashtab.c  | 27 ++++++++++------
 kernel/bpf/syscall.c  | 37 ++++++++++++++++++++--
 6 files changed, 139 insertions(+), 15 deletions(-)
include/linux/bpf.h +3 -0
index 5d845ca02eba..744f1886cf91 100644
@@ -23,6 +23,7 @@
 #include <linux/slab.h>
 #include <linux/percpu-refcount.h>
 #include <linux/bpfptr.h>
+#include <linux/btf.h>
 
 struct bpf_verifier_env;
 struct bpf_verifier_log;
@@ -171,6 +172,7 @@ struct bpf_map_value_off_desc {
 	u32 btf_id;
 	struct btf *btf;
 	struct module *module;
+	btf_dtor_kfunc_t dtor; /* only set when flags & BPF_MAP_VALUE_OFF_F_REF is true */
 	int flags;
 };
 
@@ -1568,6 +1570,7 @@ struct bpf_map_value_off_desc *bpf_map_ptr_off_contains(struct bpf_map *map, u32
 void bpf_map_free_ptr_off_tab(struct bpf_map *map);
 struct bpf_map_value_off *bpf_map_copy_ptr_off_tab(const struct bpf_map *map);
 bool bpf_map_equal_ptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b);
+void bpf_map_free_ptr_to_btf_id(struct bpf_map *map, void *map_value);
 
 struct bpf_map *bpf_map_get(u32 ufd);
 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
include/linux/btf.h +2 -0
index a304a1ea39d9..c7e75be9637f 100644
@@ -46,6 +46,8 @@ struct btf_id_dtor_kfunc {
 	u32 kfunc_btf_id;
 };
 
+typedef void (*btf_dtor_kfunc_t)(void *);
+
 extern const struct file_operations btf_fops;
 
 void btf_get(struct btf *btf);
kernel/bpf/arraymap.c +11 -2
index 7f145aefbff8..de4baca3edd7 100644
@@ -287,10 +287,12 @@ static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key
 	return 0;
 }
 
-static void check_and_free_timer_in_array(struct bpf_array *arr, void *val)
+static void check_and_free_timer_and_ptr_in_array(struct bpf_array *arr, void *val)
 {
 	if (unlikely(map_value_has_timer(&arr->map)))
 		bpf_timer_cancel_and_free(val + arr->map.timer_off);
+	if (unlikely(map_value_has_ptr_to_btf_id(&arr->map)))
+		bpf_map_free_ptr_to_btf_id(&arr->map, val);
 }
 
 /* Called from syscall or from eBPF program */
@@ -327,7 +329,7 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
 			copy_map_value_locked(map, val, value, false);
 		else
 			copy_map_value(map, val, value);
-		check_and_free_timer_in_array(array, val);
+		check_and_free_timer_and_ptr_in_array(array, val);
 	}
 	return 0;
 }
@@ -398,6 +400,13 @@ static void array_map_free_timers(struct bpf_map *map)
 static void array_map_free(struct bpf_map *map)
 {
 	struct bpf_array *array = container_of(map, struct bpf_array, map);
+	int i;
+
+	if (unlikely(map_value_has_ptr_to_btf_id(map))) {
+		for (i = 0; i < array->map.max_entries; i++)
+			bpf_map_free_ptr_to_btf_id(map, array->value + array->elem_size * i);
+		bpf_map_free_ptr_off_tab(map);
+	}
 
 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
 		bpf_array_free_percpu(array);
kernel/bpf/btf.c +71 -1

87 lines not on this page · raw message

kernel/bpf/hashtab.c +17 -10
index d29af9988f37..3c33b58e8d3e 100644
@@ -725,12 +725,15 @@ static int htab_lru_map_gen_lookup(struct bpf_map *map,
 	return insn - insn_buf;
 }
 
-static void check_and_free_timer(struct bpf_htab *htab, struct htab_elem *elem)
+static void check_and_free_timer_and_ptr(struct bpf_htab *htab,
+					 struct htab_elem *elem, bool free_ptr)
 {
+	void *map_value = elem->key + round_up(htab->map.key_size, 8);
+
 	if (unlikely(map_value_has_timer(&htab->map)))
-		bpf_timer_cancel_and_free(elem->key +
-					  round_up(htab->map.key_size, 8) +
-					  htab->map.timer_off);
+		bpf_timer_cancel_and_free(map_value + htab->map.timer_off);
+	if (unlikely(map_value_has_ptr_to_btf_id(&htab->map)) && free_ptr)
+		bpf_map_free_ptr_to_btf_id(&htab->map, map_value);
 }
 
 /* It is called from the bpf_lru_list when the LRU needs to delete
@@ -757,7 +760,7 @@ static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
 		if (l == tgt_l) {
 			hlist_nulls_del_rcu(&l->hash_node);
-			check_and_free_timer(htab, l);
+			check_and_free_timer_and_ptr(htab, l, true);
 			break;
 		}
 
@@ -829,7 +832,7 @@ static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
 {
 	if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
 		free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
-	check_and_free_timer(htab, l);
+	check_and_free_timer_and_ptr(htab, l, true);
 	kfree(l);
 }
 
@@ -857,7 +860,7 @@ static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
 	htab_put_fd_value(htab, l);
 
 	if (htab_is_prealloc(htab)) {
-		check_and_free_timer(htab, l);
+		check_and_free_timer_and_ptr(htab, l, true);
 		__pcpu_freelist_push(&htab->freelist, &l->fnode);
 	} else {
 		atomic_dec(&htab->count);
@@ -1104,7 +1107,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
 		if (!htab_is_prealloc(htab))
 			free_htab_elem(htab, l_old);
 		else
-			check_and_free_timer(htab, l_old);
+			check_and_free_timer_and_ptr(htab, l_old, true);
 	}
 	ret = 0;
 err:
@@ -1114,7 +1117,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
 
 static void htab_lru_push_free(struct bpf_htab *htab, struct htab_elem *elem)
 {
-	check_and_free_timer(htab, elem);
+	check_and_free_timer_and_ptr(htab, elem, true);
 	bpf_lru_push_free(&htab->lru, &elem->lru_node);
 }
 
@@ -1420,7 +1423,10 @@ static void htab_free_malloced_timers(struct bpf_htab *htab)
 		struct htab_elem *l;
 
 		hlist_nulls_for_each_entry(l, n, head, hash_node)
-			check_and_free_timer(htab, l);
+			/* We are called from map_release_uref, so we don't free
+			 * ref'd pointers.
+			 */
+			check_and_free_timer_and_ptr(htab, l, false);
 		cond_resched_rcu();
 	}
 	rcu_read_unlock();
@@ -1458,6 +1464,7 @@ static void htab_map_free(struct bpf_map *map)
 	else
 		prealloc_destroy(htab);
 
+	bpf_map_free_ptr_off_tab(map);
 	free_percpu(htab->extra_elems);
 	bpf_map_area_free(htab->buckets);
 	for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++)
kernel/bpf/syscall.c +35 -2
index 83d71d6912f5..925e8c615ad2 100644
@@ -638,15 +638,48 @@ bool bpf_map_equal_ptr_off_tab(const struct bpf_map *map_a, const struct bpf_map
 	return !memcmp(tab_a, tab_b, size);
 }
 
+/* Caller must ensure map_value_has_ptr_to_btf_id is true. Note that this
+ * function can be called on a map value while the map_value is visible to BPF
+ * programs, as it ensures the correct synchronization, and we already enforce
+ * the same using the verifier on the BPF program side, esp. for referenced
+ * pointers.
+ */
+void bpf_map_free_ptr_to_btf_id(struct bpf_map *map, void *map_value)
+{
+	struct bpf_map_value_off *tab = map->ptr_off_tab;
+	u64 *btf_id_ptr;
+	int i;
+
+	for (i = 0; i < tab->nr_off; i++) {
+		struct bpf_map_value_off_desc *off_desc = &tab->off[i];
+		u64 old_ptr;
+
+		btf_id_ptr = map_value + off_desc->offset;
+		if (!(off_desc->flags & BPF_MAP_VALUE_OFF_F_REF)) {
+			/* On 32-bit platforms, WRITE_ONCE 64-bit store tearing
+			 * into two 32-bit stores is fine for us, as we only
+			 * permit pointer values to be stored at this address,
+			 * which are word sized, so the other half of 64-bit
+			 * value will always be zeroed.
+			 */
+			WRITE_ONCE(*btf_id_ptr, 0);
+			continue;
+		}
+		old_ptr = xchg(btf_id_ptr, 0);
+		off_desc->dtor((void *)old_ptr);
+	}
+}
+
 /* called from workqueue */
 static void bpf_map_free_deferred(struct work_struct *work)
 {
 	struct bpf_map *map = container_of(work, struct bpf_map, work);
 
 	security_bpf_map_free(map);
-	bpf_map_free_ptr_off_tab(map);
 	bpf_map_release_memcg(map);
-	/* implementation dependent freeing */
+	/* implementation dependent freeing, map_free callback also does
+	 * bpf_map_free_ptr_off_tab, if needed.
+	 */
 	map->ops->map_free(map);
 }
 
Signature · 1 line
2.35.1
author maintainer
#32 lore raw

PATCH 13/15 libbpf: Add __kptr* macros to bpf_helpers.h

In reply to Kumar Kartikeya Dwivedi

Include convenience definitions:
__kptr: Unreferenced BTF ID pointer
__kptr_ref: Referenced BTF ID pointer
__kptr_percpu: per-CPU BTF ID pointer
__kptr_user: Userspace BTF ID pointer

Users can use them to tag the pointer type meant to be used with the new support directly in the map value definition. Note that these attributes require https://reviews.llvm.org/D119799 to be emitted in BPF object BTF correctly when applied to a non-builtin type.

---
 tools/lib/bpf/bpf_helpers.h | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/bpf_helpers.h +4 -0
index 44df982d2a5c..feb311fe1c72 100644
@@ -149,6 +149,10 @@ enum libbpf_tristate {
 
 #define __kconfig __attribute__((section(".kconfig")))
 #define __ksym __attribute__((section(".ksyms")))
+#define __kptr __attribute__((btf_type_tag("kernel.bpf.btf_id")))
+#define __kptr_ref __kptr __attribute__((btf_type_tag("kernel.bpf.ref")))
+#define __kptr_percpu __kptr __attribute__((btf_type_tag("kernel.bpf.percpu")))
+#define __kptr_user __kptr __attribute__((btf_type_tag("kernel.bpf.user")))
 
 #ifndef ___bpf_concat
 #define ___bpf_concat(a, b) a ## b
Signature · 1 line
2.35.1
author maintainer
#33 lore raw

PATCH 14/15 selftests/bpf: Add C tests for PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi

This uses the __kptr* macros as well, and tries to test the stuff that is supposed to work, since we have negative tests in test_verifier suite.

---
 .../selftests/bpf/prog_tests/map_btf_ptr.c    |  13 +++
 .../testing/selftests/bpf/progs/map_btf_ptr.c | 105 ++++++++++++++++++
 .../testing/selftests/bpf/progs/test_bpf_nf.c |  31 ++++++
 3 files changed, 149 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/map_btf_ptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/map_btf_ptr.c
tools/testing/selftests/bpf/prog_tests/map_btf_ptr.c new file +13 -0
new file mode 100644
index 000000000000..8fb6acf1b89d
@@ -0,0 +1,13 @@
+#include <test_progs.h>
+
+#include "map_btf_ptr.skel.h"
+
+void test_map_btf_ptr(void)
+{
+	struct map_btf_ptr *skel;
+
+	skel = map_btf_ptr__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "map_btf_ptr__open_and_load"))
+		return;
+	map_btf_ptr__destroy(skel);
+}
tools/testing/selftests/bpf/progs/map_btf_ptr.c new file +105 -0

105 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_bpf_nf.c +31 -0
index f00a9731930e..74e3892be544 100644
@@ -30,8 +30,21 @@ struct nf_conn *bpf_xdp_ct_lookup(struct xdp_md *, struct bpf_sock_tuple *, u32,
 				  struct bpf_ct_opts___local *, u32) __ksym;
 struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32,
 				  struct bpf_ct_opts___local *, u32) __ksym;
+struct nf_conn *bpf_ct_kptr_get(struct nf_conn **, struct bpf_sock_tuple *, u32,
+				u8, u8) __ksym;
 void bpf_ct_release(struct nf_conn *) __ksym;
 
+struct nf_map_value {
+	struct nf_conn __kptr_ref *ct;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__type(key, int);
+	__type(value, struct nf_map_value);
+	__uint(max_entries, 1);
+} array_map SEC(".maps");
+
 static __always_inline void
 nf_ct_test(struct nf_conn *(*func)(void *, struct bpf_sock_tuple *, u32,
 				   struct bpf_ct_opts___local *, u32),
@@ -101,10 +114,27 @@ nf_ct_test(struct nf_conn *(*func)(void *, struct bpf_sock_tuple *, u32,
 		test_eafnosupport = opts_def.error;
 }
 
+static __always_inline void
+nf_ct_test_kptr(void)
+{
+	struct bpf_sock_tuple tuple = {};
+	struct nf_map_value *v;
+	struct nf_conn *ct;
+
+	v = bpf_map_lookup_elem(&array_map, &(int){0});
+	if (!v)
+		return;
+	ct = bpf_ct_kptr_get(&v->ct, &tuple, sizeof(tuple.ipv4), IPPROTO_TCP, IP_CT_DIR_ORIGINAL);
+	if (!ct)
+		return;
+	bpf_ct_release(ct);
+}
+
 SEC("xdp")
 int nf_xdp_ct_test(struct xdp_md *ctx)
 {
 	nf_ct_test((void *)bpf_xdp_ct_lookup, ctx);
+	nf_ct_test_kptr();
 	return 0;
 }
 
@@ -112,6 +142,7 @@ SEC("tc")
 int nf_skb_ct_test(struct __sk_buff *ctx)
 {
 	nf_ct_test((void *)bpf_skb_ct_lookup, ctx);
+	nf_ct_test_kptr();
 	return 0;
 }
 
Signature · 1 line
2.35.1
author maintainer
#34 lore raw

PATCH 15/15 selftests/bpf: Add verifier tests for PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi

Reuse bpf_prog_test functions to test the support for PTR_TO_BTF_ID in BPF map case, including some tests that verify implementation sanity and corner cases.

---
 net/bpf/test_run.c                            |  17 +-
 tools/testing/selftests/bpf/test_verifier.c   |  57 +-
 .../selftests/bpf/verifier/map_btf_ptr.c      | 624 ++++++++++++++++++
 3 files changed, 695 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/verifier/map_btf_ptr.c
net/bpf/test_run.c +16 -1
index f08034500813..caa289f63849 100644
@@ -1263,8 +1263,23 @@ static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
 	.ret_null_set = &test_sk_ret_null_kfunc_ids,
 };
 
+BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids)
+BTF_ID(struct, prog_test_ref_kfunc)
+BTF_ID(func, bpf_kfunc_call_test_release)
+
 static int __init bpf_prog_test_run_init(void)
 {
-	return register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set);
+	const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = {
+		{
+		  .btf_id       = bpf_prog_test_dtor_kfunc_ids[0],
+		  .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1]
+		},
+	};
+	int ret;
+
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set);
+	return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc,
+						  ARRAY_SIZE(bpf_prog_test_dtor_kfunc),
+						  THIS_MODULE);
 }
 late_initcall(bpf_prog_test_run_init);
tools/testing/selftests/bpf/test_verifier.c +55 -2

100 lines not on this page · raw message

tools/testing/selftests/bpf/verifier/map_btf_ptr.c new file +624 -0

624 lines not on this page · raw message

Signature · 1 line
2.35.1
#17 lore raw

Re: [PATCH bpf-next v1 05/15] bpf: Allow storing PTR_TO_PERCPU_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi
Thank you for the patch! Perhaps something to improve:

Hi Kumar,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on next-20220217]
[cannot apply to bpf-next/master bpf/master linus/master v5.17-rc4 v5.17-rc3 v5.17-rc2 v5.17-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105
base:    3c30cf91b5ecc7272b3d2942ae0505dd8320b81c
config: openrisc-randconfig-s032-20220220 (https://download.01.org/0day-ci/archive/20220221/[email protected]/config)
compiler: or1k-linux-gcc (GCC) 11.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-dirty
# https://github.com/0day-ci/linux/commit/255d8431d2cae10fb3ac6abd44b1bf73f15dd060
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105
git checkout 255d8431d2cae10fb3ac6abd44b1bf73f15dd060
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=openrisc SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

>> kernel/bpf/verifier.c:1568:39: sparse: sparse: mixing different enum types:
>> kernel/bpf/verifier.c:1568:39: sparse:    unsigned int enum bpf_reg_type
>> kernel/bpf/verifier.c:1568:39: sparse:    unsigned int enum bpf_type_flag

kernel/bpf/verifier.c:13916:38: sparse: sparse: subtraction of functions? Share your drugs kernel/bpf/verifier.c: note: in included file (through include/linux/bpf.h, include/linux/bpf-cgroup.h):
include/linux/bpfptr.h:52:47: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:52:47: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:63:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:63:40: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:52:47: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:52:47: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:63:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:63:40: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:52:47: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:52:47: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:63:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:63:40: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:52:47: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:52:47: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:52:47: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:52:47: sparse: sparse: cast from non-scalar

vim +1568 kernel/bpf/verifier.c

1555
1556 static void mark_btf_ld_reg(struct bpf_verifier_env *env,

  1557				    struct bpf_reg_state *regs, u32 regno,
  1558				    enum bpf_reg_type reg_type,
  1559				    struct btf *btf, u32 btf_id,
  1560				    enum bpf_type_flag flag)
  1561	{
  1562		if (reg_type == SCALAR_VALUE ||
  1563		    WARN_ON_ONCE(reg_type != PTR_TO_BTF_ID && reg_type != PTR_TO_PERCPU_BTF_ID)) {
  1564			mark_reg_unknown(env, regs, regno);
  1565			return;
  1566		}
  1567		mark_reg_known_zero(env, regs, regno);
> 1568		regs[regno].type = reg_type | flag;

1569 regs[regno].btf = btf;
1570 regs[regno].btf_id = btf_id;
1571 }
1572

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#26 lore raw

Re: [PATCH bpf-next v1 10/15] bpf: Wire up freeing of referenced PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi
Thank you for the patch! Yet something to improve:

Hi Kumar,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on next-20220217]
[cannot apply to bpf-next/master bpf/master linus/master v5.17-rc4 v5.17-rc3 v5.17-rc2 v5.17-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105
base:    3c30cf91b5ecc7272b3d2942ae0505dd8320b81c
config: openrisc-randconfig-s032-20220220 (https://download.01.org/0day-ci/archive/20220221/[email protected]/config)
compiler: or1k-linux-gcc (GCC) 11.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-dirty
# https://github.com/0day-ci/linux/commit/09a47522ec608218eb6aabd5011316d78ad245e0
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105
git checkout 09a47522ec608218eb6aabd5011316d78ad245e0
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=openrisc SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag as appropriate

All error/warnings (new ones prefixed by >>):

kernel/bpf/syscall.c: In function 'bpf_map_free_ptr_to_btf_id':

>> kernel/bpf/syscall.c:669:32: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
     669 |                 off_desc->dtor((void *)old_ptr);
         |                                ^
   In file included from arch/openrisc/include/asm/atomic.h:131,
                    from include/linux/atomic.h:7,
                    from include/asm-generic/bitops/lock.h:5,
                    from arch/openrisc/include/asm/bitops.h:41,
                    from include/linux/bitops.h:33,
                    from include/linux/log2.h:12,
                    from include/asm-generic/div64.h:55,
                    from ./arch/openrisc/include/generated/asm/div64.h:1,
                    from include/linux/math.h:6,
                    from include/linux/math64.h:6,
                    from include/linux/time.h:6,
                    from include/linux/ktime.h:24,
                    from include/linux/timer.h:6,
                    from include/linux/workqueue.h:9,
                    from include/linux/bpf.h:9,
                    from kernel/bpf/syscall.c:4:
   In function '__xchg',
       inlined from 'bpf_map_free_ptr_to_btf_id' at kernel/bpf/syscall.c:668:13:
>> arch/openrisc/include/asm/cmpxchg.h:160:24: error: call to '__xchg_called_with_bad_pointer' declared with attribute error: Bad argument size for xchg
     160 |                 return __xchg_called_with_bad_pointer();
         |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +669 kernel/bpf/syscall.c

640
641 /* Caller must ensure map_value_has_ptr_to_btf_id is true. Note that this
642 * function can be called on a map value while the map_value is visible to BPF
643 * programs, as it ensures the correct synchronization, and we already enforce
644 * the same using the verifier on the BPF program side, esp. for referenced
645 * pointers.
646 */
647 void bpf_map_free_ptr_to_btf_id(struct bpf_map *map, void *map_value)
648 {
649 struct bpf_map_value_off *tab = map->ptr_off_tab;
650 u64 *btf_id_ptr;
651 int i;
652
653 for (i = 0; i < tab->nr_off; i++) {
654 struct bpf_map_value_off_desc *off_desc = &tab->off[i];
655 u64 old_ptr;
656
657 btf_id_ptr = map_value + off_desc->offset;
658 if (!(off_desc->flags & BPF_MAP_VALUE_OFF_F_REF)) {
659 /* On 32-bit platforms, WRITE_ONCE 64-bit store tearing
660 * into two 32-bit stores is fine for us, as we only
661 * permit pointer values to be stored at this address,
662 * which are word sized, so the other half of 64-bit
663 * value will always be zeroed.
664 */
665 WRITE_ONCE(*btf_id_ptr, 0);
666 continue;
667 }
668 old_ptr = xchg(btf_id_ptr, 0);

 > 669			off_desc->dtor((void *)old_ptr);

670 }
671 }
672

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#27 lore raw

Re: [PATCH bpf-next v1 10/15] bpf: Wire up freeing of referenced PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi
Thank you for the patch! Yet something to improve:

Hi Kumar,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on next-20220217]
[cannot apply to bpf-next/master bpf/master linus/master v5.17-rc4 v5.17-rc3 v5.17-rc2 v5.17-rc5] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105
base:    3c30cf91b5ecc7272b3d2942ae0505dd8320b81c
config: mips-randconfig-r012-20220220 (https://download.01.org/0day-ci/archive/20220221/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project d271fc04d5b97b12e6b797c6067d3c96a8d7470e)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install mips cross compiling tool for clang build
# apt-get install binutils-mips-linux-gnu
# https://github.com/0day-ci/linux/commit/09a47522ec608218eb6aabd5011316d78ad245e0
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105 git checkout 09a47522ec608218eb6aabd5011316d78ad245e0
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from kernel/bpf/syscall.c:4:
In file included from include/linux/bpf.h:9:
In file included from include/linux/workqueue.h:9:
In file included from include/linux/timer.h:6:
In file included from include/linux/ktime.h:24:
In file included from include/linux/time.h:60:
In file included from include/linux/time32.h:13:
In file included from include/linux/timex.h:65:
In file included from arch/mips/include/asm/timex.h:19:
In file included from arch/mips/include/asm/cpu-type.h:12:
In file included from include/linux/smp.h:13:
In file included from include/linux/cpumask.h:13:
In file included from include/linux/atomic.h:7:
In file included from arch/mips/include/asm/atomic.h:23:

>> arch/mips/include/asm/cmpxchg.h:83:11: error: call to __xchg_called_with_bad_pointer declared with 'error' attribute: Bad argument size for xchg
                           return __xchg_called_with_bad_pointer();
                                  ^

1 error generated.

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for OMAP_GPMC
Depends on MEMORY && OF_ADDRESS
Selected by
- MTD_NAND_OMAP2 && MTD && MTD_RAW_NAND && (ARCH_OMAP2PLUS || ARCH_KEYSTONE || ARCH_K3 || COMPILE_TEST && HAS_IOMEM

vim +/error +83 arch/mips/include/asm/cmpxchg.h

5154f3b4194910 Paul Burton         2017-06-09  66  
b70eb30056dc84 Paul Burton         2017-06-09  67  extern unsigned long __xchg_small(volatile void *ptr, unsigned long val,
b70eb30056dc84 Paul Burton         2017-06-09  68  				  unsigned int size);
b70eb30056dc84 Paul Burton         2017-06-09  69  
46f1619500d022 Thomas Bogendoerfer 2019-10-09  70  static __always_inline
46f1619500d022 Thomas Bogendoerfer 2019-10-09  71  unsigned long __xchg(volatile void *ptr, unsigned long x, int size)
b81947c646bfef David Howells       2012-03-28  72  {
b81947c646bfef David Howells       2012-03-28  73  	switch (size) {
b70eb30056dc84 Paul Burton         2017-06-09  74  	case 1:
b70eb30056dc84 Paul Burton         2017-06-09  75  	case 2:
b70eb30056dc84 Paul Burton         2017-06-09  76  		return __xchg_small(ptr, x, size);
b70eb30056dc84 Paul Burton         2017-06-09  77  
b81947c646bfef David Howells       2012-03-28  78  	case 4:
62c6081dca75d6 Paul Burton         2017-06-09  79  		return __xchg_asm("ll", "sc", (volatile u32 *)ptr, x);
62c6081dca75d6 Paul Burton         2017-06-09  80  
b81947c646bfef David Howells       2012-03-28  81  	case 8:
62c6081dca75d6 Paul Burton         2017-06-09  82  		if (!IS_ENABLED(CONFIG_64BIT))
62c6081dca75d6 Paul Burton         2017-06-09 @83  			return __xchg_called_with_bad_pointer();
62c6081dca75d6 Paul Burton         2017-06-09  84  
62c6081dca75d6 Paul Burton         2017-06-09  85  		return __xchg_asm("lld", "scd", (volatile u64 *)ptr, x);
62c6081dca75d6 Paul Burton         2017-06-09  86  
d15dc68c1143e2 Paul Burton         2017-06-09  87  	default:
d15dc68c1143e2 Paul Burton         2017-06-09  88  		return __xchg_called_with_bad_pointer();
b81947c646bfef David Howells       2012-03-28  89  	}
b81947c646bfef David Howells       2012-03-28  90  }
b81947c646bfef David Howells       2012-03-28  91  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#28 lore raw

Re: [PATCH bpf-next v1 10/15] bpf: Wire up freeing of referenced PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi
Thank you for the patch! Yet something to improve:

Hi Kumar,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on next-20220217]
[cannot apply to bpf-next/master bpf/master linus/master v5.17-rc4 v5.17-rc3 v5.17-rc2 v5.17-rc5] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105
base:    3c30cf91b5ecc7272b3d2942ae0505dd8320b81c
config: microblaze-randconfig-r022-20220220 (https://download.01.org/0day-ci/archive/20220221/[email protected]/config)
compiler: microblaze-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/09a47522ec608218eb6aabd5011316d78ad245e0
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105 git checkout 09a47522ec608218eb6aabd5011316d78ad245e0
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=microblaze SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

microblaze-linux-ld: kernel/bpf/syscall.o: in function `bpf_map_free_ptr_to_btf_id':

>> (.text+0x555c): undefined reference to `__generic_xchg_called_with_bad_pointer'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#31 lore raw

Re: [PATCH bpf-next v1 12/15] net/netfilter: Add bpf_ct_kptr_get helper

In reply to Kumar Kartikeya Dwivedi
Thank you for the patch! Perhaps something to improve:

Hi Kumar,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on next-20220217]
[cannot apply to bpf-next/master bpf/master linus/master v5.17-rc4 v5.17-rc3 v5.17-rc2 v5.17-rc5] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105
base:    3c30cf91b5ecc7272b3d2942ae0505dd8320b81c
config: s390-defconfig (https://download.01.org/0day-ci/archive/20220221/[email protected]/config)
compiler: s390-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/79e35d4e4ee33a7692f0612065012307a361cd56
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Introduce-typed-pointer-support-in-BPF-maps/20220220-215105 git checkout 79e35d4e4ee33a7692f0612065012307a361cd56
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=s390 SHELL=/bin/bash net/netfilter/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

net/netfilter/nf_conntrack_bpf.c: In function 'bpf_ct_kptr_get':

>> net/netfilter/nf_conntrack_bpf.c:226:21: warning: variable 'net' set but not used [-Wunused-but-set-variable]
     226 |         struct net *net;
         |                     ^~~

net/netfilter/nf_conntrack_bpf.c: At top level:
net/netfilter/nf_conntrack_bpf.c:314:5: warning: no previous prototype for 'register_nf_conntrack_bpf' [-Wmissing-prototypes]
314 | int register_nf_conntrack_bpf(void)
| ^~~~~~~~~~~~~~~~~~~~~~~~~

vim +/net +226 net/netfilter/nf_conntrack_bpf.c

219
220 /* TODO: Just a PoC, need to reuse code in __nf_conntrack_find_get for this */
221 struct nf_conn *bpf_ct_kptr_get(struct nf_conn **ptr, struct bpf_sock_tuple *bpf_tuple,
222 u32 tuple__sz, u8 protonum, u8 direction)
223 {
224 struct nf_conntrack_tuple tuple;
225 struct nf_conn *nfct;

 > 226		struct net *net;

227 u64 *nfct_p;
228 int ret;
229
230 WARN_ON_ONCE(!rcu_read_lock_held());
231
232 if ((protonum != IPPROTO_TCP && protonum != IPPROTO_UDP) ||
233 (direction != IP_CT_DIR_ORIGINAL && direction != IP_CT_DIR_REPLY))
234 return NULL;
235
236 /* ptr is actually pointer to u64 having address, hence recast u64 load
237 * to native pointer width.
238 */
239 nfct_p = (u64 *)ptr;
240 nfct = (struct nf_conn *)READ_ONCE(*nfct_p);
241 if (!nfct || unlikely(!refcount_inc_not_zero(&nfct->ct_general.use)))
242 return NULL;
243
244 memset(&tuple, 0, sizeof(tuple));
245 ret = bpf_fill_nf_tuple(&tuple, bpf_tuple, tuple__sz);
246 if (ret < 0)
247 goto end;
248 tuple.dst.protonum = protonum;
249
250 /* XXX: Need to allow passing in struct net *, or take netns_id, this is non-sense */
251 net = nf_ct_net(nfct);
252 if (!nf_ct_key_equal(&nfct->tuplehash[direction], &tuple,
253 &nf_ct_zone_dflt, nf_ct_net(nfct)))
254 goto end;
255 return nfct;
256 end:
257 nf_ct_put(nfct);
258 return NULL;
259 }
260

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#2 lore raw

Re: [PATCH bpf-next v1 01/15] bpf: Factor out fd returning from bpf_btf_find_by_name_kind

In reply to Kumar Kartikeya Dwivedi
Quoted 28 lines · Kumar Kartikeya Dwivedi

In next few patches, we need a helper that searches all kernel BTFs (vmlinux and module BTFs), and finds the type denoted by 'name' and 'kind'. Turns out bpf_btf_find_by_name_kind already does the same thing, but it instead returns a BTF ID and optionally fd (if module BTF). This is used for relocating ksyms in BPF loader code (bpftool gen skel -L).

We extract the core code out into a new helper btf_find_by_name_kind_all, which returns the BTF ID and BTF pointer in an out parameter. The reference for the returned BTF pointer is only bumped if it is a module BTF, this needs to be kept in mind when using this helper.

Hence, the user must release the BTF reference iff btf_is_module is true, otherwise transfer the ownership to e.g. an fd.

In case of the helper, the fd is only allocated for module BTFs, so no extra handling for btf_vmlinux case is required.

---
 kernel/bpf/btf.c | 47 +++++++++++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 16 deletions(-)
kernel/bpf/btf.c +0 -0
index 2c4c5dbe2abe..3645d8c14a18 100644

kernel/bpf/btf.c static struct btf *btf_get_module_btf(const struct module *module) · line 6548

Quoted 5 lines
kernel/bpf/btf.c +1 -1
@@ -6545,16 +6545,10 @@ static struct btf *btf_get_module_btf(const struct module *module)
 	return btf;
 }
 
-BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
+static s32 btf_find_by_name_kind_all(const char *name, u32 kind, struct btf **btfp)

The name is getting too long.
How about bpf_find_btf_id() ?

Quoted 25 lines
kernel/bpf/btf.c +1 -15
 {
 	struct btf *btf;
-	long ret;
-
-	if (flags)
-		return -EINVAL;
-
-	if (name_sz <= 1 || name[name_sz - 1])
-		return -EINVAL;
+	s32 ret;
 
 	btf = bpf_get_btf_vmlinux();
 	if (IS_ERR(btf))
@@ -6580,19 +6574,40 @@ BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int
 			spin_unlock_bh(&btf_idr_lock);
 			ret = btf_find_by_name_kind(mod_btf, name, kind);
 			if (ret > 0) {
-				int btf_obj_fd;
-
-				btf_obj_fd = __btf_new_fd(mod_btf);
-				if (btf_obj_fd < 0) {
-					btf_put(mod_btf);
-					return btf_obj_fd;
-				}
-				return ret | (((u64)btf_obj_fd) << 32);

kernel/bpf/btf.c BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int · line 6586

Quoted 10 lines
kernel/bpf/btf.c +5 -0
+				*btfp = mod_btf;
+				return ret;
 			}
 			spin_lock_bh(&btf_idr_lock);
 			btf_put(mod_btf);
 		}
 		spin_unlock_bh(&btf_idr_lock);
+	} else {
+		*btfp = btf;
+	}

Since we're refactoring let's drop the indent.
How about

  if (ret > 0) {
    *btfp = btf;
    return ret;

}
idr_for_each_entry().

and move the func right after btf_find_by_name_kind(), so that later patch doesn't need to do:
static s32 bpf_find_btf_id();
Eventually this helper might become global with this name.

Also may be do btf_get() for vmlinux_btf too?
In case it reduces 'if (btf_is_module())' checks.

reviewer
#35 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 73 lines · Kumar Kartikeya Dwivedi

Introduction
------------

This set enables storing pointers of a certain type in BPF map, and extends the verifier to enforce type safety and lifetime correctness properties.

The infrastructure being added is generic enough for allowing storing any kind of pointers whose type is available using BTF (user or kernel) in the future (e.g. strongly typed memory allocation in BPF program), which are internally tracked in the verifier as PTR_TO_BTF_ID, but for now the series limits them to four kinds of pointers obtained from the kernel.

Obviously, use of this feature depends on map BTF.

1. Unreferenced kernel pointer

In this case, there are very few restrictions. The pointer type being stored must match the type declared in the map value. However, such a pointer when loaded from the map can only be dereferenced, but not passed to any in-kernel helpers or kernel functions available to the program. This is because while the verifier's exception handling mechanism coverts BPF_LDX to PROBE_MEM loads, which are then handled specially by the JIT implementation, the same liberty is not available to accesses inside the kernel. The pointer by the time it is passed into a helper has no lifetime related guarantees about the object it is pointing to, and may well be referencing invalid memory.

2. Referenced kernel pointer

This case imposes a lot of restrictions on the programmer, to ensure safety. To transfer the ownership of a reference in the BPF program to the map, the user must use the BPF_XCHG instruction, which returns the old pointer contained in the map, as an acquired reference, and releases verifier state for the referenced pointer being exchanged, as it moves into the map.

This a normal PTR_TO_BTF_ID that can be used with in-kernel helpers and kernel functions callable by the program.

However, if BPF_LDX is used to load a referenced pointer from the map, it is still not permitted to pass it to in-kernel helpers or kernel functions. To obtain a reference usable with helpers, the user must invoke a kfunc helper which returns a usable reference (which also must be eventually released before BPF_EXIT, or moved into a map).

Since the load of the pointer (preserving data dependency ordering) must happen inside the RCU read section, the kfunc helper will take a pointer to the map value, which must point to the actual pointer of the object whose reference is to be raised. The type will be verified from the BTF information of the kfunc, as the prototype must be:

        T *func(T **, ... /* other arguments */);

Then, the verifier checks whether pointer at offset of the map value points to the type T, and permits the call.

This convention is followed so that such helpers may also be called from sleepable BPF programs, where RCU read lock is not necessarily held in the BPF program context, hence necessiating the need to pass in a pointer to the actual pointer to perform the load inside the RCU read section.

3. per-CPU kernel pointer

These have very little restrictions. The user can store a PTR_TO_PERCPU_BTF_ID into the map, and when loading from the map, they must NULL check it before use, because while a non-zero value stored into the map should always be valid, it can still be reset to zero on updates. After checking it to be non-NULL, it can be passed to bpf_per_cpu_ptr and bpf_this_cpu_ptr helpers to obtain a PTR_TO_BTF_ID to underlying per-CPU object.

It is also permitted to write 0 and reset the value.

4. Userspace pointer

Quoted 9 lines

The verifier recently gained support for annotating BTF with __user type tag. This indicates pointers pointing to memory which must be read using the bpf_probe_read_user helper to ensure correct results. The set also permits storing them into the BPF map, and ensures user pointer cannot be stored into other kinds of pointers mentioned above.

When loaded from the map, the only thing that can be done is to pass this pointer to bpf_probe_read_user. No dereference is allowed.

I guess I missed some context here. Could you please provide some reference to the use cases of these features?

For Unreferenced kernel pointer and userspace pointer, it seems that there is no guarantee the pointer will still be valid during access (we only know it is valid when it is stored in the map). Is this correct?

Thanks,
Song

[...]

#6 lore raw

Re: [PATCH bpf-next v1 03/15] bpf: Allow storing PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi
Quoted 168 lines · Kumar Kartikeya Dwivedi

This patch allows user to embed PTR_TO_BTF_ID in map value, such that loading it marks the destination register as having the appropriate register type and such a pointer can be dereferenced like usual PTR_TO_BTF_ID and be passed to various BPF helpers.

This feature can be useful to store an object in a map for a long time, and then inspect it later. Since PTR_TO_BTF_ID is safe against invalid access, verifier doesn't need to perform any complex lifetime checks. It can be useful in cases where user already knows pointer will remain valid, so any dereference at a later time (possibly in entirely different BPF program invocation) will yield correct results as far the data read from kernel memory is concerned.

Note that it is quite possible such BTF ID pointer is invalid, in this case the verifier's built-in exception handling mechanism where it converts loads into PTR_TO_BTF_ID into PROBE_MEM loads, would handle the invalid case. Next patch which adds referenced PTR_TO_BTF_ID would need to take more care in ensuring a correct value is stored in the BPF map.

The user indicates that a certain pointer must be treated as PTR_TO_BTF_ID by using a BTF type tag 'btf_id' on the pointed to type of the pointer. Then, this information is recorded in the object BTF which will be passed into the kernel by way of map's BTF information.

The kernel then records the type, and offset of all such pointers, and finds their corresponding built-in kernel type by the name and BTF kind.

Later, during verification this information is used that access to such pointers is sized correctly, and done at a proper offset into the map value. Only BPF_LDX, BPF_STX, and BPF_ST with 0 (to denote NULL) are allowed instructions that can access such a pointer. On BPF_LDX, the destination register is updated to be a PTR_TO_BTF_ID, and on BPF_STX, it is checked whether the source register type is same PTR_TO_BTF_ID, and whether the BTF ID (reg->btf and reg->btf_id) matches the type specified in the map value's definition.

Hence, the verifier allows flexible access to kernel data across program invocations in a type safe manner, without compromising on the runtime safety of the kernel.

Next patch will extend this support to referenced PTR_TO_BTF_ID.

---
 include/linux/bpf.h     |  30 +++++++-
 include/linux/btf.h     |   3 +
 kernel/bpf/btf.c        | 127 ++++++++++++++++++++++++++++++++++
 kernel/bpf/map_in_map.c |   5 +-
 kernel/bpf/syscall.c    | 137 ++++++++++++++++++++++++++++++++++++-
 kernel/bpf/verifier.c   | 148 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 446 insertions(+), 4 deletions(-)
include/linux/bpf.h +29 -1
index f19abc59b6cd..ce45ffb79f82 100644
@@ -155,6 +155,23 @@ struct bpf_map_ops {
 	const struct bpf_iter_seq_info *iter_seq_info;
 };
 
+enum {
+	/* Support at most 8 pointers in a BPF map value */
+	BPF_MAP_VALUE_OFF_MAX = 8,
+};
+
+struct bpf_map_value_off_desc {
+	u32 offset;
+	u32 btf_id;
+	struct btf *btf;
+	struct module *module;
+};
+
+struct bpf_map_value_off {
+	u32 nr_off;
+	struct bpf_map_value_off_desc off[];
+};
+
 struct bpf_map {
 	/* The first two cachelines with read-mostly members of which some
 	 * are also accessed in fast-path (e.g. ops, max_entries).
@@ -171,6 +188,7 @@ struct bpf_map {
 	u64 map_extra; /* any per-map-type extra fields */
 	u32 map_flags;
 	int spin_lock_off; /* >=0 valid offset, <0 error */
+	struct bpf_map_value_off *ptr_off_tab;
 	int timer_off; /* >=0 valid offset, <0 error */
 	u32 id;
 	int numa_node;
@@ -184,7 +202,7 @@ struct bpf_map {
 	char name[BPF_OBJ_NAME_LEN];
 	bool bypass_spec_v1;
 	bool frozen; /* write-once; write-protected by freeze_mutex */
-	/* 14 bytes hole */
+	/* 6 bytes hole */
 
 	/* The 3rd and 4th cacheline with misc members to avoid false sharing
 	 * particularly with refcounting.
@@ -217,6 +235,11 @@ static inline bool map_value_has_timer(const struct bpf_map *map)
 	return map->timer_off >= 0;
 }
 
+static inline bool map_value_has_ptr_to_btf_id(const struct bpf_map *map)
+{
+	return !IS_ERR_OR_NULL(map->ptr_off_tab);
+}
+
 static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
 {
 	if (unlikely(map_value_has_spin_lock(map)))
@@ -1490,6 +1513,11 @@ void bpf_prog_put(struct bpf_prog *prog);
 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
 
+struct bpf_map_value_off_desc *bpf_map_ptr_off_contains(struct bpf_map *map, u32 offset);
+void bpf_map_free_ptr_off_tab(struct bpf_map *map);
+struct bpf_map_value_off *bpf_map_copy_ptr_off_tab(const struct bpf_map *map);
+bool bpf_map_equal_ptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b);
+
 struct bpf_map *bpf_map_get(u32 ufd);
 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
 struct bpf_map *__bpf_map_get(struct fd f);
include/linux/btf.h +3 -0
index 36bc09b8e890..6592183aeb23 100644
@@ -26,6 +26,7 @@ struct btf_type;
 union bpf_attr;
 struct btf_show;
 struct btf_id_set;
+struct bpf_map;
 
 struct btf_kfunc_id_set {
 	struct module *owner;
@@ -123,6 +124,8 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
 			   u32 expected_offset, u32 expected_size);
 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
 int btf_find_timer(const struct btf *btf, const struct btf_type *t);
+int btf_find_ptr_to_btf_id(const struct btf *btf, const struct btf_type *t,
+			   struct bpf_map *map);
 bool btf_type_is_void(const struct btf_type *t);
 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
kernel/bpf/btf.c +11 -0
index 55f6ccac3388..1edb5710e155 100644
@@ -3122,6 +3122,7 @@ static void btf_struct_log(struct btf_verifier_env *env,
 enum {
 	BTF_FIELD_SPIN_LOCK,
 	BTF_FIELD_TIMER,
+	BTF_FIELD_KPTR,
 };
 
 static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t,
@@ -3140,6 +3141,106 @@ static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t
 	return 0;
 }
 
+static s32 btf_find_by_name_kind_all(const char *name, u32 kind, struct btf **btfp);
+
+static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
+			       u32 off, int sz, void *data)
+{
+	struct bpf_map_value_off *tab;
+	struct bpf_map *map = data;
+	struct module *mod = NULL;
+	bool btf_id_tag = false;
+	struct btf *kernel_btf;

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3163

Quoted 10 lines
kernel/bpf/btf.c +10 -0
+	int nr_off, ret;
+	s32 id;
+
+	/* For PTR, sz is always == 8 */
+	if (!btf_type_is_ptr(t))
+		return 0;
+	t = btf_type_by_id(btf, t->type);
+
+	while (btf_type_is_type_tag(t)) {
+		if (!strcmp("kernel.bpf.btf_id", __btf_name_by_offset(btf, t->name_off))) {

All of these strings consume space.
Multiple tags consume space too.
I would just do:
#define __kptr __attribute__((btf_type_tag("kptr"))) #define __kptr_ref __attribute__((btf_type_tag("kptr_ref"))) #define __kptr_percpu __attribute__((btf_type_tag("kptr_percpu"))) #define __kptr_user __attribute__((btf_type_tag("kptr_user")))

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3173

Quoted 10 lines
kernel/bpf/btf.c +10 -0
+			/* repeated tag */
+			if (btf_id_tag) {
+				ret = -EINVAL;
+				goto end;
+			}
+			btf_id_tag = true;
+		} else if (!strncmp("kernel.", __btf_name_by_offset(btf, t->name_off),
+			   sizeof("kernel.") - 1)) {
+			/* TODO: Should we reject these when loading BTF? */
+			/* Unavailable tag in reserved tag namespace */

I don't think we need to reserve the tag space. There is little risk to break progs with future tags. I would just drop this 'if'.

Quoted 23 lines
kernel/bpf/btf.c +23 -0
+			ret = -EACCES;
+			goto end;
+		}
+		/* Look for next tag */
+		t = btf_type_by_id(btf, t->type);
+	}
+	if (!btf_id_tag)
+		return 0;
+
+	/* Get the base type */
+	if (btf_type_is_modifier(t))
+		t = btf_type_skip_modifiers(btf, t->type, NULL);
+	/* Only pointer to struct is allowed */
+	if (!__btf_type_is_struct(t)) {
+		ret = -EINVAL;
+		goto end;
+	}
+
+	id = btf_find_by_name_kind_all(__btf_name_by_offset(btf, t->name_off),
+				       BTF_INFO_KIND(t->info), &kernel_btf);
+	if (id < 0) {
+		ret = id;
+		goto end;

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3206

Quoted 10 lines
kernel/bpf/btf.c +10 -0
+	}
+
+	nr_off = map->ptr_off_tab ? map->ptr_off_tab->nr_off : 0;
+	if (nr_off == BPF_MAP_VALUE_OFF_MAX) {
+		ret = -E2BIG;
+		goto end_btf;
+	}
+
+	tab = krealloc(map->ptr_off_tab, offsetof(struct bpf_map_value_off, off[nr_off + 1]),
+		       GFP_KERNEL | __GFP_NOWARN);

Argh.
If the function is called btf_find_field() it should do 'find' and only 'find'. It should be side effect free and should find _one_ field.
If you want a function with side effcts it should be called something like btf_walk_fields.

For this case how about side effect free btf_find_fieldS() that will populate array struct bpf_field_info {
struct btf *type; /* set for spin_lock, timer, kptr */
u32 off;
int flags; /* ref|percpu|user for kptr */
};

cnt = btf_find_fields(prog_btf, value_type, BTF_FIELD_SPIN_LOCK|TIMER|KPTR, fields);

btf_find_struct_field/btf_find_datasec_var will keep the count and will error when it reaches BPF_MAP_VALUE_OFF_MAX.
switch (field_type) {
case BTF_FIELD_SPIN_LOCK:
btf_find_field_struct(... "bpf_spin_lock",

                             sizeof(struct bpf_spin_lock),
                             __alignof__(struct bpf_spin_lock),
                             fields + i);

case BTF_FIELD_TIMER:
btf_find_field_struct(... "bpf_timer", sizeof, alignof, fields + i);
case BTF_FIELD_KPTR:
btf_find_field_kptr(... fields + i);
}

btf_find_by_name_kind_all (or new name bpf_find_btf_id) will be done after btf_find_fields() is over. dtor will be found after as well.
struct bpf_map_value_off will be allocated once.

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3218

Quoted 12 lines
kernel/bpf/btf.c +12 -0
+	if (!tab) {
+		ret = -ENOMEM;
+		goto end_btf;
+	}
+	/* Initialize nr_off for newly allocated ptr_off_tab */
+	if (!map->ptr_off_tab)
+		tab->nr_off = 0;
+	map->ptr_off_tab = tab;
+
+	/* We take reference to make sure valid pointers into module data don't
+	 * become invalid across program invocation.
+	 */

what is the point of grabbing mod ref?
This patch needs btf only and its refcnt will be incremented by bpf_find_btf_id. Is that because of future dtor ?
Then it should be part of that patch.

Quoted 9 lines
kernel/bpf/btf.c +9 -0
+	if (btf_is_module(kernel_btf)) {
+		mod = btf_try_get_module(kernel_btf);
+		if (!mod) {
+			ret = -ENXIO;
+			goto end_btf;
+		}
+	}
+
+	tab->off[nr_off].offset = off;

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3237

Quoted 10 lines
kernel/bpf/btf.c +10 -0
+	tab->off[nr_off].btf_id = id;
+	tab->off[nr_off].btf    = kernel_btf;
+	tab->off[nr_off].module = mod;
+	tab->nr_off++;
+
+	return 0;
+end_btf:
+	/* Reference is only raised for module BTF */
+	if (btf_is_module(kernel_btf))
+		btf_put(kernel_btf);

see earlier suggestion. this 'if' can be dropped if we btf_get for vmlinux_btf too.

Quoted 5 lines
kernel/bpf/btf.c +5 -0
+end:
+	bpf_map_free_ptr_off_tab(map);
+	map->ptr_off_tab = ERR_PTR(ret);
+	return ret;
+}
#10 lore raw

Re: [PATCH bpf-next v1 04/15] bpf: Allow storing referenced PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi

kernel/bpf/verifier.c static int check_stack_access_within_bounds( · line 4586

Quoted 5 lines · Kumar Kartikeya Dwivedi

static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,

kernel/bpf/verifier.c +2 -1
 			    int off, int bpf_size, enum bpf_access_type t,
-			    int value_regno, bool strict_alignment_once)
+			    int value_regno, bool strict_alignment_once,
+			    struct bpf_reg_state *atomic_load_reg)

No new side effects please.
value_regno is not pretty already.
At least its known ugliness that we need to clean up one day.

Quoted 25 lines

static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) {

kernel/bpf/verifier.c +5 -1
+	struct bpf_reg_state atomic_load_reg;
 	int load_reg;
 	int err;
 
+	__mark_reg_unknown(env, &atomic_load_reg);
+
 	switch (insn->imm) {
 	case BPF_ADD:
 	case BPF_ADD | BPF_FETCH:
@@ -4813,6 +4894,7 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
 		else
 			load_reg = insn->src_reg;
 
+		atomic_load_reg = *reg_state(env, load_reg);
 		/* check and record load of old value */
 		err = check_reg_arg(env, load_reg, DST_OP);
 		if (err)
@@ -4825,20 +4907,21 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
 	}
 
 	/* Check whether we can read the memory, with second call for fetch
-	 * case to simulate the register fill.
+	 * case to simulate the register fill, which also triggers checks

kernel/bpf/verifier.c static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i · line 4918

Quoted 10 lines
kernel/bpf/verifier.c +3 -2
+	 * for manipulation of BTF ID pointers embedded in BPF maps.
 	 */
 	err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
-			       BPF_SIZE(insn->code), BPF_READ, -1, true);
+			       BPF_SIZE(insn->code), BPF_READ, -1, true, NULL);
 	if (!err && load_reg >= 0)
 		err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
 				       BPF_SIZE(insn->code), BPF_READ, load_reg,
-				       true);
+				       true, load_reg >= 0 ? &atomic_load_reg : NULL);

Special xchg logic should be down outside of check_mem_access() instead of hidden by layers of calls.

#21 lore raw

Re: [PATCH bpf-next v1 08/15] bpf: Adapt copy_map_value for multiple offset case

In reply to Kumar Kartikeya Dwivedi
Quoted 139 lines · Kumar Kartikeya Dwivedi

The changes in this patch deserve closer look, so it has been split into its own independent patch. While earlier we just had to skip two objects at most while copying in and out of map, now we have potentially many objects (at most 8 + 2 = 10, due to the BPF_MAP_VALUE_OFF_MAX limit).

Hence, divide the copy_map_value function into an inlined fast path and function call to slowpath. The slowpath handles the case of > 3 offsets, while we handle the most common cases (0, 1, 2, or 3 offsets) in the inline function itself.

In copy_map_value_slow, we use 11 offsets, just to make the for loop that copies the value free of edge cases for the last offset, by using map->value_size as final offset to subtract remaining area to copy from.

---
 include/linux/bpf.h  | 43 +++++++++++++++++++++++++++++++---
 kernel/bpf/syscall.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 3 deletions(-)
include/linux/bpf.h +40 -3
index ae599aaf8d4c..5d845ca02eba 100644
@@ -253,12 +253,22 @@ static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
 		memset(dst + map->spin_lock_off, 0, sizeof(struct bpf_spin_lock));
 	if (unlikely(map_value_has_timer(map)))
 		memset(dst + map->timer_off, 0, sizeof(struct bpf_timer));
+	if (unlikely(map_value_has_ptr_to_btf_id(map))) {
+		struct bpf_map_value_off *tab = map->ptr_off_tab;
+		int i;
+
+		for (i = 0; i < tab->nr_off; i++)
+			*(u64 *)(dst + tab->off[i].offset) = 0;
+	}
 }
 
+void copy_map_value_slow(struct bpf_map *map, void *dst, void *src, u32 s_off,
+			 u32 s_sz, u32 t_off, u32 t_sz);
+
 /* copy everything but bpf_spin_lock and bpf_timer. There could be one of each. */
 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
 {
-	u32 s_off = 0, s_sz = 0, t_off = 0, t_sz = 0;
+	u32 s_off = 0, s_sz = 0, t_off = 0, t_sz = 0, p_off = 0, p_sz = 0;
 
 	if (unlikely(map_value_has_spin_lock(map))) {
 		s_off = map->spin_lock_off;
@@ -268,13 +278,40 @@ static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
 		t_off = map->timer_off;
 		t_sz = sizeof(struct bpf_timer);
 	}
+	/* Multiple offset case is slow, offload to function */
+	if (unlikely(map_value_has_ptr_to_btf_id(map))) {
+		struct bpf_map_value_off *tab = map->ptr_off_tab;
+
+		/* Inline the likely common case */
+		if (likely(tab->nr_off == 1)) {
+			p_off = tab->off[0].offset;
+			p_sz = sizeof(u64);
+		} else {
+			copy_map_value_slow(map, dst, src, s_off, s_sz, t_off, t_sz);
+			return;
+		}
+	}
+
+	if (unlikely(s_sz || t_sz || p_sz)) {
+		/* The order is p_off, t_off, s_off, use insertion sort */
 
-	if (unlikely(s_sz || t_sz)) {
+		if (t_off < p_off || !t_sz) {
+			swap(t_off, p_off);
+			swap(t_sz, p_sz);
+		}
 		if (s_off < t_off || !s_sz) {
 			swap(s_off, t_off);
 			swap(s_sz, t_sz);
+			if (t_off < p_off || !t_sz) {
+				swap(t_off, p_off);
+				swap(t_sz, p_sz);
+			}
 		}
-		memcpy(dst, src, t_off);
+
+		memcpy(dst, src, p_off);
+		memcpy(dst + p_off + p_sz,
+		       src + p_off + p_sz,
+		       t_off - p_off - p_sz);
 		memcpy(dst + t_off + t_sz,
 		       src + t_off + t_sz,
 		       s_off - t_off - t_sz);
kernel/bpf/syscall.c +33 -0
index beb96866f34d..83d71d6912f5 100644
@@ -30,6 +30,7 @@
 #include <linux/pgtable.h>
 #include <linux/bpf_lsm.h>
 #include <linux/poll.h>
+#include <linux/sort.h>
 #include <linux/bpf-netns.h>
 #include <linux/rcupdate_trace.h>
 #include <linux/memcontrol.h>
@@ -230,6 +231,60 @@ static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
 	return err;
 }
 
+static int copy_map_value_cmp(const void *_a, const void *_b)
+{
+	const u32 a = *(const u32 *)_a;
+	const u32 b = *(const u32 *)_b;
+
+	/* We only need to sort based on offset */
+	if (a < b)
+		return -1;
+	else if (a > b)
+		return 1;
+	return 0;
+}
+
+void copy_map_value_slow(struct bpf_map *map, void *dst, void *src, u32 s_off,
+			 u32 s_sz, u32 t_off, u32 t_sz)
+{
+	struct bpf_map_value_off *tab = map->ptr_off_tab; /* already set to non-NULL */
+	/* 3 = 2 for bpf_timer, bpf_spin_lock, 1 for map->value_size sentinel */
+	struct {
+		u32 off;
+		u32 sz;
+	} off_arr[BPF_MAP_VALUE_OFF_MAX + 3];
+	int i, cnt = 0;
+
+	/* Reconsider stack usage when bumping BPF_MAP_VALUE_OFF_MAX */
+	BUILD_BUG_ON(sizeof(off_arr) != 88);
+
+	for (i = 0; i < tab->nr_off; i++) {
+		off_arr[cnt].off = tab->off[i].offset;
+		off_arr[cnt++].sz = sizeof(u64);
+	}
+	if (s_sz) {

kernel/bpf/syscall.c static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key, · line 275

Quoted 10 lines
kernel/bpf/syscall.c +10 -0
+		off_arr[cnt].off = s_off;
+		off_arr[cnt++].sz = s_sz;
+	}
+	if (t_sz) {
+		off_arr[cnt].off = t_off;
+		off_arr[cnt++].sz = t_sz;
+	}
+	off_arr[cnt].off = map->value_size;
+
+	sort(off_arr, cnt, sizeof(off_arr[0]), copy_map_value_cmp, NULL);

Ouch. sort every time we need to copy map value?
sort it once please. 88 bytes in a map are worth it. Especially since "slow" version will trigger with just 2 kptrs. (if I understand this correctly).

author maintainer
#11 lore raw

Re: [PATCH bpf-next v1 04/15] bpf: Allow storing referenced PTR_TO_BTF_ID in map

In reply to Alexei Starovoitov
Quoted 33 lines · Alexei Starovoitov

kernel/bpf/verifier.c static int check_stack_access_within_bounds( · line 4586

Quoted 5 lines · Kumar Kartikeya Dwivedi

static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,

kernel/bpf/verifier.c +2 -1
 			    int off, int bpf_size, enum bpf_access_type t,
-			    int value_regno, bool strict_alignment_once)
+			    int value_regno, bool strict_alignment_once,
+			    struct bpf_reg_state *atomic_load_reg)

No new side effects please.
value_regno is not pretty already.
At least its known ugliness that we need to clean up one day.

Quoted 22 lines

static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) {

kernel/bpf/verifier.c +4 -0
+	struct bpf_reg_state atomic_load_reg;
 	int load_reg;
 	int err;
 
+	__mark_reg_unknown(env, &atomic_load_reg);
+
 	switch (insn->imm) {
 	case BPF_ADD:
 	case BPF_ADD | BPF_FETCH:
@@ -4813,6 +4894,7 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
 		else
 			load_reg = insn->src_reg;
 
+		atomic_load_reg = *reg_state(env, load_reg);
 		/* check and record load of old value */
 		err = check_reg_arg(env, load_reg, DST_OP);
 		if (err)
@@ -4825,20 +4907,21 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
 	}
 
Quoted 16 lines

kernel/bpf/verifier.c static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i · line 4918

Quoted 13 lines
kernel/bpf/verifier.c +4 -3
 	/* Check whether we can read the memory, with second call for fetch
-	 * case to simulate the register fill.
+	 * case to simulate the register fill, which also triggers checks
+	 * for manipulation of BTF ID pointers embedded in BPF maps.
 	 */
 	err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
-			       BPF_SIZE(insn->code), BPF_READ, -1, true);
+			       BPF_SIZE(insn->code), BPF_READ, -1, true, NULL);
 	if (!err && load_reg >= 0)
 		err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
 				       BPF_SIZE(insn->code), BPF_READ, load_reg,
-				       true);
+				       true, load_reg >= 0 ? &atomic_load_reg : NULL);

Special xchg logic should be down outside of check_mem_access() instead of hidden by layers of calls.

Right, it's ugly, but if we don't capture the reg state before that check_reg_arg(env, load_reg, DST_OP), it's not possible to see the actual PTR_TO_BTF_ID being moved into the map, since check_reg_arg will do a mark_reg_unknown for value_regno. Any other ideas on what I can do?

37086bfdc737 ("bpf: Propagate stack bounds to registers in atomics w/ BPF_FETCH") changed the order of check_mem_access and DST_OP check_reg_arg.

--
Kartikeya

author maintainer
#36 lore raw
In reply to Song Liu
Quoted 81 lines · Song Liu
Quoted 79 lines · Kumar Kartikeya Dwivedi

Introduction
------------

This set enables storing pointers of a certain type in BPF map, and extends the verifier to enforce type safety and lifetime correctness properties.

The infrastructure being added is generic enough for allowing storing any kind of pointers whose type is available using BTF (user or kernel) in the future (e.g. strongly typed memory allocation in BPF program), which are internally tracked in the verifier as PTR_TO_BTF_ID, but for now the series limits them to four kinds of pointers obtained from the kernel.

Obviously, use of this feature depends on map BTF.

1. Unreferenced kernel pointer

In this case, there are very few restrictions. The pointer type being stored must match the type declared in the map value. However, such a pointer when loaded from the map can only be dereferenced, but not passed to any in-kernel helpers or kernel functions available to the program. This is because while the verifier's exception handling mechanism coverts BPF_LDX to PROBE_MEM loads, which are then handled specially by the JIT implementation, the same liberty is not available to accesses inside the kernel. The pointer by the time it is passed into a helper has no lifetime related guarantees about the object it is pointing to, and may well be referencing invalid memory.

2. Referenced kernel pointer

This case imposes a lot of restrictions on the programmer, to ensure safety. To transfer the ownership of a reference in the BPF program to the map, the user must use the BPF_XCHG instruction, which returns the old pointer contained in the map, as an acquired reference, and releases verifier state for the referenced pointer being exchanged, as it moves into the map.

This a normal PTR_TO_BTF_ID that can be used with in-kernel helpers and kernel functions callable by the program.

However, if BPF_LDX is used to load a referenced pointer from the map, it is still not permitted to pass it to in-kernel helpers or kernel functions. To obtain a reference usable with helpers, the user must invoke a kfunc helper which returns a usable reference (which also must be eventually released before BPF_EXIT, or moved into a map).

Since the load of the pointer (preserving data dependency ordering) must happen inside the RCU read section, the kfunc helper will take a pointer to the map value, which must point to the actual pointer of the object whose reference is to be raised. The type will be verified from the BTF information of the kfunc, as the prototype must be:

        T *func(T **, ... /* other arguments */);

Then, the verifier checks whether pointer at offset of the map value points to the type T, and permits the call.

This convention is followed so that such helpers may also be called from sleepable BPF programs, where RCU read lock is not necessarily held in the BPF program context, hence necessiating the need to pass in a pointer to the actual pointer to perform the load inside the RCU read section.

3. per-CPU kernel pointer

These have very little restrictions. The user can store a PTR_TO_PERCPU_BTF_ID into the map, and when loading from the map, they must NULL check it before use, because while a non-zero value stored into the map should always be valid, it can still be reset to zero on updates. After checking it to be non-NULL, it can be passed to bpf_per_cpu_ptr and bpf_this_cpu_ptr helpers to obtain a PTR_TO_BTF_ID to underlying per-CPU object.

It is also permitted to write 0 and reset the value.

4. Userspace pointer

The verifier recently gained support for annotating BTF with __user type tag. This indicates pointers pointing to memory which must be read using the bpf_probe_read_user helper to ensure correct results. The set also permits storing them into the BPF map, and ensures user pointer cannot be stored into other kinds of pointers mentioned above.

Quoted 7 lines
Quoted 3 lines

When loaded from the map, the only thing that can be done is to pass this pointer to bpf_probe_read_user. No dereference is allowed.

I guess I missed some context here. Could you please provide some reference to the use cases of these features?

The common usecase is caching references to objects inside BPF maps, to avoid costly lookups, and being able to raise it once for the duration of program invocation when passing it to multiple helpers (to avoid further re-lookups). Storing references also allows you to control object lifetime.

One other use case is enabling xdp_frame queueing in XDP using this, but that still needs some integration work after this lands, so it's a bit early to comment on the specifics.

Other than that, I think Alexei already mentioned this could be easily extended to do memory allocation returning a PTR_TO_BTF_ID in a BPF program [0] in the future.

[0]: https://lore.kernel.org/bpf/[email protected]

Quoted 4 lines

For Unreferenced kernel pointer and userspace pointer, it seems that there is no guarantee the pointer will still be valid during access (we only know it is valid when it is stored in the map). Is this correct?

That is correct. In the case of unreferenced and referenced kernel pointers, when you do a BPF_LDX, both are marked as PTR_UNTRUSTED, and it is not allowed to pass them into helpers or kfuncs, because from that point onwards we cannot claim that the object is still alive when pointer is used later. Still, dereference is permitted because verifier handles faults for bad accesses using PROBE_MEM conversion for PTR_TO_BTF_ID loads in convert_ctx_accesses (which is then later detected by JIT to build exception table used by exception handler).

In case of reading unreferenced pointer, in some cases you know that the pointer will stay valid, so you can just store it in the map and load and directly access it, it imposes very little restrictions.

For the referenced case, and BPF_LDX marking it as PTR_UNTRUSTED, you could say that this makes it a lot less useful, because if BPF program already holds reference, just to make sure I _read valid data_, I still have to use the kptr_get style helper to raise and put reference to ensure the object is alive when it is accessed.

So in that case, for RCU protected objects, it should still wait for BPF program to hit BPF_EXIT before the actual release, but for other cases like the case of sleepable programs, or objects where refcount alone manages lifetime, you can also detect writer presence of the other BPF program (to detect if pointer during our access was xchg'd out) using a seqlock style scheme:

	v = bpf_map_lookup_elem(&map, ...);
	if (!v)
		return 0;
	seq_begin = v->seq;
	atomic_thread_fence(memory_order_acquire); // A
	<do access>
	atomic_thread_fence(memory_order_acquire); // B
	seq_end = v->seq;
	if (seq_begin & 1 || seq_begin != seq_end)
		goto bad_read;
	<use data>

Ofcourse, barriers are not yet in BPF, but you get the idea (it should work on x86). The updater BPF program will increment v->seq before and after xchg, ensuring proper ordering. v->seq starts as 0, so odd seq indicates writer update is in progress.

This would allow you to not raise refcount, while still ensuring that as long as object was accessed, it was still valid between A and B. Even if raising uncontended refcount is cheap, this is much cheaper.

The case of userspace pointer is different, it sets the MEM_USER flag, so the only useful thing to do is calling bpf_probe_read_user, you can't even dereference it. You are right that in most cases that userspace pointer won't be useful, but for some cooperative cases between BPF program and userspace thread, it can act as a way to share certain thread local areas/userspace memory that the BPF program can then store keyed by the task_struct *, where using a BPF map to share memory is not always possible.

Quoted 4 lines

Thanks,
Song

[...]

--
Kartikeya

#12 lore raw

Re: [PATCH bpf-next v1 04/15] bpf: Allow storing referenced PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi
Quoted 52 lines · Kumar Kartikeya Dwivedi
Quoted 33 lines · Alexei Starovoitov

kernel/bpf/verifier.c static int check_stack_access_within_bounds( · line 4586

Quoted 5 lines · Kumar Kartikeya Dwivedi

static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
int off, int bpf_size, enum bpf_access_type t,

kernel/bpf/verifier.c +2 -1
-                       int value_regno, bool strict_alignment_once)
+                       int value_regno, bool strict_alignment_once,
+                       struct bpf_reg_state *atomic_load_reg)

No new side effects please.
value_regno is not pretty already.
At least its known ugliness that we need to clean up one day.

Quoted 22 lines

static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) {

kernel/bpf/verifier.c +4 -0
+   struct bpf_reg_state atomic_load_reg;
    int load_reg;
    int err;
 
+   __mark_reg_unknown(env, &atomic_load_reg);
+
    switch (insn->imm) {
    case BPF_ADD:
    case BPF_ADD | BPF_FETCH:
@@ -4813,6 +4894,7 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
            else
                    load_reg = insn->src_reg;
 
+           atomic_load_reg = *reg_state(env, load_reg);
            /* check and record load of old value */
            err = check_reg_arg(env, load_reg, DST_OP);
            if (err)
@@ -4825,20 +4907,21 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
    }
 
Quoted 16 lines

kernel/bpf/verifier.c static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i · line 4918

Quoted 13 lines
kernel/bpf/verifier.c +4 -3
    /* Check whether we can read the memory, with second call for fetch
-    * case to simulate the register fill.
+    * case to simulate the register fill, which also triggers checks
+    * for manipulation of BTF ID pointers embedded in BPF maps.
     */
    err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
-                          BPF_SIZE(insn->code), BPF_READ, -1, true);
+                          BPF_SIZE(insn->code), BPF_READ, -1, true, NULL);
    if (!err && load_reg >= 0)
            err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
                                   BPF_SIZE(insn->code), BPF_READ, load_reg,
-                                  true);
+                                  true, load_reg >= 0 ? &atomic_load_reg : NULL);

Special xchg logic should be down outside of check_mem_access() instead of hidden by layers of calls.

Quoted 7 lines

Right, it's ugly, but if we don't capture the reg state before that check_reg_arg(env, load_reg, DST_OP), it's not possible to see the actual PTR_TO_BTF_ID being moved into the map, since check_reg_arg will do a mark_reg_unknown for value_regno. Any other ideas on what I can do?

37086bfdc737 ("bpf: Propagate stack bounds to registers in atomics w/ BPF_FETCH") changed the order of check_mem_access and DST_OP check_reg_arg.

That highlights my point that side effects are bad. That commit tries to work around that behavior and makes things harder to extend like you found out with xchg logic. Another option would be to add bpf_kptr_xchg() helper instead of dealing with insn. It will be tiny bit slower, but it will work on all architectures. While xchg bpf jit is on x86,s390,mips so far.
We need to think more on how to refactor check_mem_acess without digging ourselves into an even bigger hole.

author maintainer
#13 lore raw

Re: [PATCH bpf-next v1 04/15] bpf: Allow storing referenced PTR_TO_BTF_ID in map

Quoted 62 lines · Alexei Starovoitov
Quoted 52 lines · Kumar Kartikeya Dwivedi
Quoted 33 lines · Alexei Starovoitov

kernel/bpf/verifier.c static int check_stack_access_within_bounds( · line 4586

Quoted 5 lines · Kumar Kartikeya Dwivedi

static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
int off, int bpf_size, enum bpf_access_type t,

kernel/bpf/verifier.c +2 -1
-                       int value_regno, bool strict_alignment_once)
+                       int value_regno, bool strict_alignment_once,
+                       struct bpf_reg_state *atomic_load_reg)

No new side effects please.
value_regno is not pretty already.
At least its known ugliness that we need to clean up one day.

Quoted 22 lines

static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) {

kernel/bpf/verifier.c +4 -0
+   struct bpf_reg_state atomic_load_reg;
    int load_reg;
    int err;
 
+   __mark_reg_unknown(env, &atomic_load_reg);
+
    switch (insn->imm) {
    case BPF_ADD:
    case BPF_ADD | BPF_FETCH:
@@ -4813,6 +4894,7 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
            else
                    load_reg = insn->src_reg;
 
+           atomic_load_reg = *reg_state(env, load_reg);
            /* check and record load of old value */
            err = check_reg_arg(env, load_reg, DST_OP);
            if (err)
@@ -4825,20 +4907,21 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
    }
 
Quoted 16 lines

kernel/bpf/verifier.c static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i · line 4918

Quoted 13 lines
kernel/bpf/verifier.c +4 -3
    /* Check whether we can read the memory, with second call for fetch
-    * case to simulate the register fill.
+    * case to simulate the register fill, which also triggers checks
+    * for manipulation of BTF ID pointers embedded in BPF maps.
     */
    err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
-                          BPF_SIZE(insn->code), BPF_READ, -1, true);
+                          BPF_SIZE(insn->code), BPF_READ, -1, true, NULL);
    if (!err && load_reg >= 0)
            err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
                                   BPF_SIZE(insn->code), BPF_READ, load_reg,
-                                  true);
+                                  true, load_reg >= 0 ? &atomic_load_reg : NULL);

Special xchg logic should be down outside of check_mem_access() instead of hidden by layers of calls.

Quoted 7 lines

Right, it's ugly, but if we don't capture the reg state before that check_reg_arg(env, load_reg, DST_OP), it's not possible to see the actual PTR_TO_BTF_ID being moved into the map, since check_reg_arg will do a mark_reg_unknown for value_regno. Any other ideas on what I can do?

37086bfdc737 ("bpf: Propagate stack bounds to registers in atomics w/ BPF_FETCH") changed the order of check_mem_access and DST_OP check_reg_arg.

Quoted 7 lines

That highlights my point that side effects are bad. That commit tries to work around that behavior and makes things harder to extend like you found out with xchg logic. Another option would be to add bpf_kptr_xchg() helper instead of dealing with insn. It will be tiny bit slower, but it will work on all architectures. While xchg bpf jit is on x86,s390,mips so far.

Right, but kfunc is currently limited to x86, which is required to obtain a refcounted PTR_TO_BTF_ID that you can move into the map, so it wouldn't make much of a difference.

Quoted 2 lines

We need to think more on how to refactor check_mem_acess without digging ourselves into an even bigger hole.

So I'm ok with working on untangling check_mem_access as a follow up, but for now should we go forward with how it is? Just looking at it yesterday makes me think it's going to require a fair amount of refactoring and discussion.

Also, do you have any ideas on how to change it? Do you want it to work like how is_valid_access callbacks work? So passing something like a bpf_insn_access_aux into the call, where it sets how it'd like to update the register, and then actual updates take place in caller context?

--
Kartikeya

author maintainer
#3 lore raw

Re: [PATCH bpf-next v1 01/15] bpf: Factor out fd returning from bpf_btf_find_by_name_kind

In reply to Alexei Starovoitov
Quoted 79 lines · Alexei Starovoitov
Quoted 28 lines · Kumar Kartikeya Dwivedi

In next few patches, we need a helper that searches all kernel BTFs (vmlinux and module BTFs), and finds the type denoted by 'name' and 'kind'. Turns out bpf_btf_find_by_name_kind already does the same thing, but it instead returns a BTF ID and optionally fd (if module BTF). This is used for relocating ksyms in BPF loader code (bpftool gen skel -L).

We extract the core code out into a new helper btf_find_by_name_kind_all, which returns the BTF ID and BTF pointer in an out parameter. The reference for the returned BTF pointer is only bumped if it is a module BTF, this needs to be kept in mind when using this helper.

Hence, the user must release the BTF reference iff btf_is_module is true, otherwise transfer the ownership to e.g. an fd.

In case of the helper, the fd is only allocated for module BTFs, so no extra handling for btf_vmlinux case is required.

---
 kernel/bpf/btf.c | 47 +++++++++++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 16 deletions(-)
kernel/bpf/btf.c +0 -0
index 2c4c5dbe2abe..3645d8c14a18 100644

kernel/bpf/btf.c static struct btf *btf_get_module_btf(const struct module *module) · line 6548

Quoted 5 lines
kernel/bpf/btf.c +1 -1
@@ -6545,16 +6545,10 @@ static struct btf *btf_get_module_btf(const struct module *module)
 	return btf;
 }
 
-BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
+static s32 btf_find_by_name_kind_all(const char *name, u32 kind, struct btf **btfp)

The name is getting too long.
How about bpf_find_btf_id() ?

Quoted 25 lines
kernel/bpf/btf.c +1 -15
 {
 	struct btf *btf;
-	long ret;
-
-	if (flags)
-		return -EINVAL;
-
-	if (name_sz <= 1 || name[name_sz - 1])
-		return -EINVAL;
+	s32 ret;
 
 	btf = bpf_get_btf_vmlinux();
 	if (IS_ERR(btf))
@@ -6580,19 +6574,40 @@ BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int
 			spin_unlock_bh(&btf_idr_lock);
 			ret = btf_find_by_name_kind(mod_btf, name, kind);
 			if (ret > 0) {
-				int btf_obj_fd;
-
-				btf_obj_fd = __btf_new_fd(mod_btf);
-				if (btf_obj_fd < 0) {
-					btf_put(mod_btf);
-					return btf_obj_fd;
-				}
-				return ret | (((u64)btf_obj_fd) << 32);

kernel/bpf/btf.c BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int · line 6586

Quoted 10 lines
kernel/bpf/btf.c +5 -0
+				*btfp = mod_btf;
+				return ret;
 			}
 			spin_lock_bh(&btf_idr_lock);
 			btf_put(mod_btf);
 		}
 		spin_unlock_bh(&btf_idr_lock);
+	} else {
+		*btfp = btf;
+	}

Since we're refactoring let's drop the indent.
How about

  if (ret > 0) {
    *btfp = btf;
    return ret;
Quoted 8 lines

}
idr_for_each_entry().

and move the func right after btf_find_by_name_kind(), so that later patch doesn't need to do:
static s32 bpf_find_btf_id();
Eventually this helper might become global with this name.

Ok, will change.

Quoted 2 lines

Also may be do btf_get() for vmlinux_btf too?
In case it reduces 'if (btf_is_module())' checks.

Right, should also change this for btf_get_module_btf then, to make things consistent.

--
Kartikeya

author maintainer
#7 lore raw

Re: [PATCH bpf-next v1 03/15] bpf: Allow storing PTR_TO_BTF_ID in map

In reply to Alexei Starovoitov
Quoted 180 lines · Alexei Starovoitov
Quoted 168 lines · Kumar Kartikeya Dwivedi

This patch allows user to embed PTR_TO_BTF_ID in map value, such that loading it marks the destination register as having the appropriate register type and such a pointer can be dereferenced like usual PTR_TO_BTF_ID and be passed to various BPF helpers.

This feature can be useful to store an object in a map for a long time, and then inspect it later. Since PTR_TO_BTF_ID is safe against invalid access, verifier doesn't need to perform any complex lifetime checks. It can be useful in cases where user already knows pointer will remain valid, so any dereference at a later time (possibly in entirely different BPF program invocation) will yield correct results as far the data read from kernel memory is concerned.

Note that it is quite possible such BTF ID pointer is invalid, in this case the verifier's built-in exception handling mechanism where it converts loads into PTR_TO_BTF_ID into PROBE_MEM loads, would handle the invalid case. Next patch which adds referenced PTR_TO_BTF_ID would need to take more care in ensuring a correct value is stored in the BPF map.

The user indicates that a certain pointer must be treated as PTR_TO_BTF_ID by using a BTF type tag 'btf_id' on the pointed to type of the pointer. Then, this information is recorded in the object BTF which will be passed into the kernel by way of map's BTF information.

The kernel then records the type, and offset of all such pointers, and finds their corresponding built-in kernel type by the name and BTF kind.

Later, during verification this information is used that access to such pointers is sized correctly, and done at a proper offset into the map value. Only BPF_LDX, BPF_STX, and BPF_ST with 0 (to denote NULL) are allowed instructions that can access such a pointer. On BPF_LDX, the destination register is updated to be a PTR_TO_BTF_ID, and on BPF_STX, it is checked whether the source register type is same PTR_TO_BTF_ID, and whether the BTF ID (reg->btf and reg->btf_id) matches the type specified in the map value's definition.

Hence, the verifier allows flexible access to kernel data across program invocations in a type safe manner, without compromising on the runtime safety of the kernel.

Next patch will extend this support to referenced PTR_TO_BTF_ID.

---
 include/linux/bpf.h     |  30 +++++++-
 include/linux/btf.h     |   3 +
 kernel/bpf/btf.c        | 127 ++++++++++++++++++++++++++++++++++
 kernel/bpf/map_in_map.c |   5 +-
 kernel/bpf/syscall.c    | 137 ++++++++++++++++++++++++++++++++++++-
 kernel/bpf/verifier.c   | 148 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 446 insertions(+), 4 deletions(-)
include/linux/bpf.h +29 -1
index f19abc59b6cd..ce45ffb79f82 100644
@@ -155,6 +155,23 @@ struct bpf_map_ops {
 	const struct bpf_iter_seq_info *iter_seq_info;
 };
 
+enum {
+	/* Support at most 8 pointers in a BPF map value */
+	BPF_MAP_VALUE_OFF_MAX = 8,
+};
+
+struct bpf_map_value_off_desc {
+	u32 offset;
+	u32 btf_id;
+	struct btf *btf;
+	struct module *module;
+};
+
+struct bpf_map_value_off {
+	u32 nr_off;
+	struct bpf_map_value_off_desc off[];
+};
+
 struct bpf_map {
 	/* The first two cachelines with read-mostly members of which some
 	 * are also accessed in fast-path (e.g. ops, max_entries).
@@ -171,6 +188,7 @@ struct bpf_map {
 	u64 map_extra; /* any per-map-type extra fields */
 	u32 map_flags;
 	int spin_lock_off; /* >=0 valid offset, <0 error */
+	struct bpf_map_value_off *ptr_off_tab;
 	int timer_off; /* >=0 valid offset, <0 error */
 	u32 id;
 	int numa_node;
@@ -184,7 +202,7 @@ struct bpf_map {
 	char name[BPF_OBJ_NAME_LEN];
 	bool bypass_spec_v1;
 	bool frozen; /* write-once; write-protected by freeze_mutex */
-	/* 14 bytes hole */
+	/* 6 bytes hole */
 
 	/* The 3rd and 4th cacheline with misc members to avoid false sharing
 	 * particularly with refcounting.
@@ -217,6 +235,11 @@ static inline bool map_value_has_timer(const struct bpf_map *map)
 	return map->timer_off >= 0;
 }
 
+static inline bool map_value_has_ptr_to_btf_id(const struct bpf_map *map)
+{
+	return !IS_ERR_OR_NULL(map->ptr_off_tab);
+}
+
 static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
 {
 	if (unlikely(map_value_has_spin_lock(map)))
@@ -1490,6 +1513,11 @@ void bpf_prog_put(struct bpf_prog *prog);
 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
 
+struct bpf_map_value_off_desc *bpf_map_ptr_off_contains(struct bpf_map *map, u32 offset);
+void bpf_map_free_ptr_off_tab(struct bpf_map *map);
+struct bpf_map_value_off *bpf_map_copy_ptr_off_tab(const struct bpf_map *map);
+bool bpf_map_equal_ptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b);
+
 struct bpf_map *bpf_map_get(u32 ufd);
 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
 struct bpf_map *__bpf_map_get(struct fd f);
include/linux/btf.h +3 -0
index 36bc09b8e890..6592183aeb23 100644
@@ -26,6 +26,7 @@ struct btf_type;
 union bpf_attr;
 struct btf_show;
 struct btf_id_set;
+struct bpf_map;
 
 struct btf_kfunc_id_set {
 	struct module *owner;
@@ -123,6 +124,8 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
 			   u32 expected_offset, u32 expected_size);
 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
 int btf_find_timer(const struct btf *btf, const struct btf_type *t);
+int btf_find_ptr_to_btf_id(const struct btf *btf, const struct btf_type *t,
+			   struct bpf_map *map);
 bool btf_type_is_void(const struct btf_type *t);
 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
kernel/bpf/btf.c +11 -0
index 55f6ccac3388..1edb5710e155 100644
@@ -3122,6 +3122,7 @@ static void btf_struct_log(struct btf_verifier_env *env,
 enum {
 	BTF_FIELD_SPIN_LOCK,
 	BTF_FIELD_TIMER,
+	BTF_FIELD_KPTR,
 };
 
 static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t,
@@ -3140,6 +3141,106 @@ static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t
 	return 0;
 }
 
+static s32 btf_find_by_name_kind_all(const char *name, u32 kind, struct btf **btfp);
+
+static int btf_find_field_kptr(const struct btf *btf, const struct btf_type *t,
+			       u32 off, int sz, void *data)
+{
+	struct bpf_map_value_off *tab;
+	struct bpf_map *map = data;
+	struct module *mod = NULL;
+	bool btf_id_tag = false;
+	struct btf *kernel_btf;

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3163

Quoted 10 lines
kernel/bpf/btf.c +10 -0
+	int nr_off, ret;
+	s32 id;
+
+	/* For PTR, sz is always == 8 */
+	if (!btf_type_is_ptr(t))
+		return 0;
+	t = btf_type_by_id(btf, t->type);
+
+	while (btf_type_is_type_tag(t)) {
+		if (!strcmp("kernel.bpf.btf_id", __btf_name_by_offset(btf, t->name_off))) {
Quoted 8 lines

All of these strings consume space.
Multiple tags consume space too.
I would just do:
#define __kptr __attribute__((btf_type_tag("kptr"))) #define __kptr_ref __attribute__((btf_type_tag("kptr_ref"))) #define __kptr_percpu __attribute__((btf_type_tag("kptr_percpu"))) #define __kptr_user __attribute__((btf_type_tag("kptr_user")))

Ok.

Quoted 6 lines
Quoted 6 lines
kernel/bpf/btf.c +6 -0
+			/* repeated tag */
+			if (btf_id_tag) {
+				ret = -EINVAL;
+				goto end;
+			}
+			btf_id_tag = true;
Quoted 9 lines

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3173

Quoted 4 lines
kernel/bpf/btf.c +4 -0
+		} else if (!strncmp("kernel.", __btf_name_by_offset(btf, t->name_off),
+			   sizeof("kernel.") - 1)) {
+			/* TODO: Should we reject these when loading BTF? */
+			/* Unavailable tag in reserved tag namespace */

I don't think we need to reserve the tag space. There is little risk to break progs with future tags. I would just drop this 'if'.

Fine with dropping, but what is the expected behavior when userspace has set a tag in map value BTF that we give some meaning in the kernel later?

Quoted 62 lines
Quoted 23 lines
kernel/bpf/btf.c +23 -0
+			ret = -EACCES;
+			goto end;
+		}
+		/* Look for next tag */
+		t = btf_type_by_id(btf, t->type);
+	}
+	if (!btf_id_tag)
+		return 0;
+
+	/* Get the base type */
+	if (btf_type_is_modifier(t))
+		t = btf_type_skip_modifiers(btf, t->type, NULL);
+	/* Only pointer to struct is allowed */
+	if (!__btf_type_is_struct(t)) {
+		ret = -EINVAL;
+		goto end;
+	}
+
+	id = btf_find_by_name_kind_all(__btf_name_by_offset(btf, t->name_off),
+				       BTF_INFO_KIND(t->info), &kernel_btf);
+	if (id < 0) {
+		ret = id;
+		goto end;

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3206

Quoted 10 lines
kernel/bpf/btf.c +10 -0
+	}
+
+	nr_off = map->ptr_off_tab ? map->ptr_off_tab->nr_off : 0;
+	if (nr_off == BPF_MAP_VALUE_OFF_MAX) {
+		ret = -E2BIG;
+		goto end_btf;
+	}
+
+	tab = krealloc(map->ptr_off_tab, offsetof(struct bpf_map_value_off, off[nr_off + 1]),
+		       GFP_KERNEL | __GFP_NOWARN);

Argh.
If the function is called btf_find_field() it should do 'find' and only 'find'. It should be side effect free and should find _one_ field.
If you want a function with side effcts it should be called something like btf_walk_fields.

For this case how about side effect free btf_find_fieldS() that will populate array struct bpf_field_info {
struct btf *type; /* set for spin_lock, timer, kptr */
u32 off;
int flags; /* ref|percpu|user for kptr */
};

cnt = btf_find_fields(prog_btf, value_type, BTF_FIELD_SPIN_LOCK|TIMER|KPTR, fields);

btf_find_struct_field/btf_find_datasec_var will keep the count and will error when it reaches BPF_MAP_VALUE_OFF_MAX.
switch (field_type) {
case BTF_FIELD_SPIN_LOCK:

   btf_find_field_struct(... "bpf_spin_lock",
                             sizeof(struct bpf_spin_lock),
                             __alignof__(struct bpf_spin_lock),
                             fields + i);
case BTF_FIELD_TIMER:
   btf_find_field_struct(... "bpf_timer", sizeof, alignof, fields + i);
case BTF_FIELD_KPTR:
   btf_find_field_kptr(... fields + i);
}
Quoted 5 lines

btf_find_by_name_kind_all (or new name bpf_find_btf_id) will be done after btf_find_fields() is over. dtor will be found after as well.
struct bpf_map_value_off will be allocated once.

Ack, sounds good.

Quoted 9 lines
Quoted 9 lines
kernel/bpf/btf.c +9 -0
+	if (!tab) {
+		ret = -ENOMEM;
+		goto end_btf;
+	}
+	/* Initialize nr_off for newly allocated ptr_off_tab */
+	if (!map->ptr_off_tab)
+		tab->nr_off = 0;
+	map->ptr_off_tab = tab;
+
Quoted 9 lines

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3218

Quoted 3 lines
kernel/bpf/btf.c +3 -0
+	/* We take reference to make sure valid pointers into module data don't
+	 * become invalid across program invocation.
+	 */

what is the point of grabbing mod ref?
This patch needs btf only and its refcnt will be incremented by bpf_find_btf_id. Is that because of future dtor ?
Then it should be part of that patch.

Right, screwed it up while rebasing. Will fix.

Quoted 22 lines
Quoted 9 lines
kernel/bpf/btf.c +9 -0
+	if (btf_is_module(kernel_btf)) {
+		mod = btf_try_get_module(kernel_btf);
+		if (!mod) {
+			ret = -ENXIO;
+			goto end_btf;
+		}
+	}
+
+	tab->off[nr_off].offset = off;

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3237

Quoted 10 lines
kernel/bpf/btf.c +10 -0
+	tab->off[nr_off].btf_id = id;
+	tab->off[nr_off].btf    = kernel_btf;
+	tab->off[nr_off].module = mod;
+	tab->nr_off++;
+
+	return 0;
+end_btf:
+	/* Reference is only raised for module BTF */
+	if (btf_is_module(kernel_btf))
+		btf_put(kernel_btf);

see earlier suggestion. this 'if' can be dropped if we btf_get for vmlinux_btf too.

Quoted 5 lines

kernel/bpf/btf.c static int btf_find_field_struct(const struct btf *btf, const struct btf_type *t · line 3242

Quoted 5 lines
kernel/bpf/btf.c +5 -0
+end:
+	bpf_map_free_ptr_off_tab(map);
+	map->ptr_off_tab = ERR_PTR(ret);
+	return ret;
+}

--
Kartikeya

author maintainer
#22 lore raw

Re: [PATCH bpf-next v1 08/15] bpf: Adapt copy_map_value for multiple offset case

In reply to Alexei Starovoitov
Quoted 147 lines · Alexei Starovoitov
Quoted 146 lines · Kumar Kartikeya Dwivedi

The changes in this patch deserve closer look, so it has been split into its own independent patch. While earlier we just had to skip two objects at most while copying in and out of map, now we have potentially many objects (at most 8 + 2 = 10, due to the BPF_MAP_VALUE_OFF_MAX limit).

Hence, divide the copy_map_value function into an inlined fast path and function call to slowpath. The slowpath handles the case of > 3 offsets, while we handle the most common cases (0, 1, 2, or 3 offsets) in the inline function itself.

In copy_map_value_slow, we use 11 offsets, just to make the for loop that copies the value free of edge cases for the last offset, by using map->value_size as final offset to subtract remaining area to copy from.

---
 include/linux/bpf.h  | 43 +++++++++++++++++++++++++++++++---
 kernel/bpf/syscall.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 3 deletions(-)
include/linux/bpf.h +40 -3
index ae599aaf8d4c..5d845ca02eba 100644
@@ -253,12 +253,22 @@ static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
 		memset(dst + map->spin_lock_off, 0, sizeof(struct bpf_spin_lock));
 	if (unlikely(map_value_has_timer(map)))
 		memset(dst + map->timer_off, 0, sizeof(struct bpf_timer));
+	if (unlikely(map_value_has_ptr_to_btf_id(map))) {
+		struct bpf_map_value_off *tab = map->ptr_off_tab;
+		int i;
+
+		for (i = 0; i < tab->nr_off; i++)
+			*(u64 *)(dst + tab->off[i].offset) = 0;
+	}
 }
 
+void copy_map_value_slow(struct bpf_map *map, void *dst, void *src, u32 s_off,
+			 u32 s_sz, u32 t_off, u32 t_sz);
+
 /* copy everything but bpf_spin_lock and bpf_timer. There could be one of each. */
 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
 {
-	u32 s_off = 0, s_sz = 0, t_off = 0, t_sz = 0;
+	u32 s_off = 0, s_sz = 0, t_off = 0, t_sz = 0, p_off = 0, p_sz = 0;
 
 	if (unlikely(map_value_has_spin_lock(map))) {
 		s_off = map->spin_lock_off;
@@ -268,13 +278,40 @@ static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
 		t_off = map->timer_off;
 		t_sz = sizeof(struct bpf_timer);
 	}
+	/* Multiple offset case is slow, offload to function */
+	if (unlikely(map_value_has_ptr_to_btf_id(map))) {
+		struct bpf_map_value_off *tab = map->ptr_off_tab;
+
+		/* Inline the likely common case */
+		if (likely(tab->nr_off == 1)) {
+			p_off = tab->off[0].offset;
+			p_sz = sizeof(u64);
+		} else {
+			copy_map_value_slow(map, dst, src, s_off, s_sz, t_off, t_sz);
+			return;
+		}
+	}
+
+	if (unlikely(s_sz || t_sz || p_sz)) {
+		/* The order is p_off, t_off, s_off, use insertion sort */
 
-	if (unlikely(s_sz || t_sz)) {
+		if (t_off < p_off || !t_sz) {
+			swap(t_off, p_off);
+			swap(t_sz, p_sz);
+		}
 		if (s_off < t_off || !s_sz) {
 			swap(s_off, t_off);
 			swap(s_sz, t_sz);
+			if (t_off < p_off || !t_sz) {
+				swap(t_off, p_off);
+				swap(t_sz, p_sz);
+			}
 		}
-		memcpy(dst, src, t_off);
+
+		memcpy(dst, src, p_off);
+		memcpy(dst + p_off + p_sz,
+		       src + p_off + p_sz,
+		       t_off - p_off - p_sz);
 		memcpy(dst + t_off + t_sz,
 		       src + t_off + t_sz,
 		       s_off - t_off - t_sz);
kernel/bpf/syscall.c +40 -0
index beb96866f34d..83d71d6912f5 100644
@@ -30,6 +30,7 @@
 #include <linux/pgtable.h>
 #include <linux/bpf_lsm.h>
 #include <linux/poll.h>
+#include <linux/sort.h>
 #include <linux/bpf-netns.h>
 #include <linux/rcupdate_trace.h>
 #include <linux/memcontrol.h>
@@ -230,6 +231,60 @@ static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
 	return err;
 }
 
+static int copy_map_value_cmp(const void *_a, const void *_b)
+{
+	const u32 a = *(const u32 *)_a;
+	const u32 b = *(const u32 *)_b;
+
+	/* We only need to sort based on offset */
+	if (a < b)
+		return -1;
+	else if (a > b)
+		return 1;
+	return 0;
+}
+
+void copy_map_value_slow(struct bpf_map *map, void *dst, void *src, u32 s_off,
+			 u32 s_sz, u32 t_off, u32 t_sz)
+{
+	struct bpf_map_value_off *tab = map->ptr_off_tab; /* already set to non-NULL */
+	/* 3 = 2 for bpf_timer, bpf_spin_lock, 1 for map->value_size sentinel */
+	struct {
+		u32 off;
+		u32 sz;
+	} off_arr[BPF_MAP_VALUE_OFF_MAX + 3];
+	int i, cnt = 0;
+
+	/* Reconsider stack usage when bumping BPF_MAP_VALUE_OFF_MAX */
+	BUILD_BUG_ON(sizeof(off_arr) != 88);
+
+	for (i = 0; i < tab->nr_off; i++) {
+		off_arr[cnt].off = tab->off[i].offset;
+		off_arr[cnt++].sz = sizeof(u64);
+	}
+	if (s_sz) {
+		off_arr[cnt].off = s_off;
+		off_arr[cnt++].sz = s_sz;
+	}
+	if (t_sz) {
+		off_arr[cnt].off = t_off;
+		off_arr[cnt++].sz = t_sz;
+	}
Quoted 8 lines

kernel/bpf/syscall.c static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key, · line 275

Quoted 3 lines
kernel/bpf/syscall.c +3 -0
+	off_arr[cnt].off = map->value_size;
+
+	sort(off_arr, cnt, sizeof(off_arr[0]), copy_map_value_cmp, NULL);

Ouch. sort every time we need to copy map value?
sort it once please. 88 bytes in a map are worth it. Especially since "slow" version will trigger with just 2 kptrs. (if I understand this correctly).

Ok, also think we can reduce the size of the 88 bytes down to 55 bytes (32-bit off + 8-bit size), and embed it in struct map. Then the shuffling needed for timer and spin lock should also be gone.

--
Kartikeya

reviewer
#37 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 1 line · Kumar Kartikeya Dwivedi

[...]

Quoted 70 lines
Quoted 4 lines

I guess I missed some context here. Could you please provide some reference to the use cases of these features?

The common usecase is caching references to objects inside BPF maps, to avoid costly lookups, and being able to raise it once for the duration of program invocation when passing it to multiple helpers (to avoid further re-lookups). Storing references also allows you to control object lifetime.

One other use case is enabling xdp_frame queueing in XDP using this, but that still needs some integration work after this lands, so it's a bit early to comment on the specifics.

Other than that, I think Alexei already mentioned this could be easily extended to do memory allocation returning a PTR_TO_BTF_ID in a BPF program [0] in the future.

[0]: https://lore.kernel.org/bpf/[email protected]

Quoted 4 lines

For Unreferenced kernel pointer and userspace pointer, it seems that there is no guarantee the pointer will still be valid during access (we only know it is valid when it is stored in the map). Is this correct?

That is correct. In the case of unreferenced and referenced kernel pointers, when you do a BPF_LDX, both are marked as PTR_UNTRUSTED, and it is not allowed to pass them into helpers or kfuncs, because from that point onwards we cannot claim that the object is still alive when pointer is used later. Still, dereference is permitted because verifier handles faults for bad accesses using PROBE_MEM conversion for PTR_TO_BTF_ID loads in convert_ctx_accesses (which is then later detected by JIT to build exception table used by exception handler).

In case of reading unreferenced pointer, in some cases you know that the pointer will stay valid, so you can just store it in the map and load and directly access it, it imposes very little restrictions.

For the referenced case, and BPF_LDX marking it as PTR_UNTRUSTED, you could say that this makes it a lot less useful, because if BPF program already holds reference, just to make sure I _read valid data_, I still have to use the kptr_get style helper to raise and put reference to ensure the object is alive when it is accessed.

So in that case, for RCU protected objects, it should still wait for BPF program to hit BPF_EXIT before the actual release, but for other cases like the case of sleepable programs, or objects where refcount alone manages lifetime, you can also detect writer presence of the other BPF program (to detect if pointer during our access was xchg'd out) using a seqlock style scheme:

        v = bpf_map_lookup_elem(&map, ...);
        if (!v)
                return 0;
        seq_begin = v->seq;
        atomic_thread_fence(memory_order_acquire); // A
        <do access>
        atomic_thread_fence(memory_order_acquire); // B
        seq_end = v->seq;
        if (seq_begin & 1 || seq_begin != seq_end)
                goto bad_read;
        <use data>

Ofcourse, barriers are not yet in BPF, but you get the idea (it should work on x86). The updater BPF program will increment v->seq before and after xchg, ensuring proper ordering. v->seq starts as 0, so odd seq indicates writer update is in progress.

This would allow you to not raise refcount, while still ensuring that as long as object was accessed, it was still valid between A and B. Even if raising uncontended refcount is cheap, this is much cheaper.

Quoted 7 lines

The case of userspace pointer is different, it sets the MEM_USER flag, so the only useful thing to do is calling bpf_probe_read_user, you can't even dereference it. You are right that in most cases that userspace pointer won't be useful, but for some cooperative cases between BPF program and userspace thread, it can act as a way to share certain thread local areas/userspace memory that the BPF program can then store keyed by the task_struct *, where using a BPF map to share memory is not always possible.

Thanks for the explanation! I can see the referenced kernel pointer be very powerful in many use cases. The per cpu pointer is also interesting.

Song

#23 lore raw

Re: [PATCH bpf-next v1 08/15] bpf: Adapt copy_map_value for multiple offset case

In reply to Kumar Kartikeya Dwivedi
Quoted 158 lines · Kumar Kartikeya Dwivedi
Quoted 147 lines · Alexei Starovoitov
Quoted 146 lines · Kumar Kartikeya Dwivedi

The changes in this patch deserve closer look, so it has been split into its own independent patch. While earlier we just had to skip two objects at most while copying in and out of map, now we have potentially many objects (at most 8 + 2 = 10, due to the BPF_MAP_VALUE_OFF_MAX limit).

Hence, divide the copy_map_value function into an inlined fast path and function call to slowpath. The slowpath handles the case of > 3 offsets, while we handle the most common cases (0, 1, 2, or 3 offsets) in the inline function itself.

In copy_map_value_slow, we use 11 offsets, just to make the for loop that copies the value free of edge cases for the last offset, by using map->value_size as final offset to subtract remaining area to copy from.

---
 include/linux/bpf.h  | 43 +++++++++++++++++++++++++++++++---
 kernel/bpf/syscall.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 3 deletions(-)
include/linux/bpf.h +40 -3
index ae599aaf8d4c..5d845ca02eba 100644
@@ -253,12 +253,22 @@ static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
            memset(dst + map->spin_lock_off, 0, sizeof(struct bpf_spin_lock));
    if (unlikely(map_value_has_timer(map)))
            memset(dst + map->timer_off, 0, sizeof(struct bpf_timer));
+   if (unlikely(map_value_has_ptr_to_btf_id(map))) {
+           struct bpf_map_value_off *tab = map->ptr_off_tab;
+           int i;
+
+           for (i = 0; i < tab->nr_off; i++)
+                   *(u64 *)(dst + tab->off[i].offset) = 0;
+   }
 }
 
+void copy_map_value_slow(struct bpf_map *map, void *dst, void *src, u32 s_off,
+                    u32 s_sz, u32 t_off, u32 t_sz);
+
 /* copy everything but bpf_spin_lock and bpf_timer. There could be one of each. */
 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
 {
-   u32 s_off = 0, s_sz = 0, t_off = 0, t_sz = 0;
+   u32 s_off = 0, s_sz = 0, t_off = 0, t_sz = 0, p_off = 0, p_sz = 0;
 
    if (unlikely(map_value_has_spin_lock(map))) {
            s_off = map->spin_lock_off;
@@ -268,13 +278,40 @@ static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
            t_off = map->timer_off;
            t_sz = sizeof(struct bpf_timer);
    }
+   /* Multiple offset case is slow, offload to function */
+   if (unlikely(map_value_has_ptr_to_btf_id(map))) {
+           struct bpf_map_value_off *tab = map->ptr_off_tab;
+
+           /* Inline the likely common case */
+           if (likely(tab->nr_off == 1)) {
+                   p_off = tab->off[0].offset;
+                   p_sz = sizeof(u64);
+           } else {
+                   copy_map_value_slow(map, dst, src, s_off, s_sz, t_off, t_sz);
+                   return;
+           }
+   }
+
+   if (unlikely(s_sz || t_sz || p_sz)) {
+           /* The order is p_off, t_off, s_off, use insertion sort */
 
-   if (unlikely(s_sz || t_sz)) {
+           if (t_off < p_off || !t_sz) {
+                   swap(t_off, p_off);
+                   swap(t_sz, p_sz);
+           }
            if (s_off < t_off || !s_sz) {
                    swap(s_off, t_off);
                    swap(s_sz, t_sz);
+                   if (t_off < p_off || !t_sz) {
+                           swap(t_off, p_off);
+                           swap(t_sz, p_sz);
+                   }
            }
-           memcpy(dst, src, t_off);
+
+           memcpy(dst, src, p_off);
+           memcpy(dst + p_off + p_sz,
+                  src + p_off + p_sz,
+                  t_off - p_off - p_sz);
            memcpy(dst + t_off + t_sz,
                   src + t_off + t_sz,
                   s_off - t_off - t_sz);
kernel/bpf/syscall.c +40 -0
index beb96866f34d..83d71d6912f5 100644
@@ -30,6 +30,7 @@
 #include <linux/pgtable.h>
 #include <linux/bpf_lsm.h>
 #include <linux/poll.h>
+#include <linux/sort.h>
 #include <linux/bpf-netns.h>
 #include <linux/rcupdate_trace.h>
 #include <linux/memcontrol.h>
@@ -230,6 +231,60 @@ static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
    return err;
 }
 
+static int copy_map_value_cmp(const void *_a, const void *_b)
+{
+   const u32 a = *(const u32 *)_a;
+   const u32 b = *(const u32 *)_b;
+
+   /* We only need to sort based on offset */
+   if (a < b)
+           return -1;
+   else if (a > b)
+           return 1;
+   return 0;
+}
+
+void copy_map_value_slow(struct bpf_map *map, void *dst, void *src, u32 s_off,
+                    u32 s_sz, u32 t_off, u32 t_sz)
+{
+   struct bpf_map_value_off *tab = map->ptr_off_tab; /* already set to non-NULL */
+   /* 3 = 2 for bpf_timer, bpf_spin_lock, 1 for map->value_size sentinel */
+   struct {
+           u32 off;
+           u32 sz;
+   } off_arr[BPF_MAP_VALUE_OFF_MAX + 3];
+   int i, cnt = 0;
+
+   /* Reconsider stack usage when bumping BPF_MAP_VALUE_OFF_MAX */
+   BUILD_BUG_ON(sizeof(off_arr) != 88);
+
+   for (i = 0; i < tab->nr_off; i++) {
+           off_arr[cnt].off = tab->off[i].offset;
+           off_arr[cnt++].sz = sizeof(u64);
+   }
+   if (s_sz) {
+           off_arr[cnt].off = s_off;
+           off_arr[cnt++].sz = s_sz;
+   }
+   if (t_sz) {
+           off_arr[cnt].off = t_off;
+           off_arr[cnt++].sz = t_sz;
+   }
Quoted 8 lines

kernel/bpf/syscall.c static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key, · line 275

Quoted 3 lines
kernel/bpf/syscall.c +3 -0
+   off_arr[cnt].off = map->value_size;
+
+   sort(off_arr, cnt, sizeof(off_arr[0]), copy_map_value_cmp, NULL);

Ouch. sort every time we need to copy map value?
sort it once please. 88 bytes in a map are worth it. Especially since "slow" version will trigger with just 2 kptrs. (if I understand this correctly).

Quoted 3 lines

Ok, also think we can reduce the size of the 88 bytes down to 55 bytes (32-bit off + 8-bit size), and embed it in struct map. Then the shuffling needed for timer and spin lock should also be gone.

We can probably make this copy_map_value_slow to be the only function. I suspect

+       /* There is always at least one element */
+       memcpy(dst, src, off_arr[0].off);
+       /* Copy the rest, while skipping other regions */
+       for (i = 1; i < cnt; i++) {

+ u32 curr_off = off_arr[i - 1].off + off_arr[i - 1].sz;
+ u32 next_off = off_arr[i].off;
+
+ memcpy(dst + curr_off, src + curr_off, next_off - curr_off);
+ }
is faster than the dance we currently do in copy_map_value with a bunch of if-s.

#8 lore raw

Re: [PATCH bpf-next v1 03/15] bpf: Allow storing PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi
Quoted 7 lines · Kumar Kartikeya Dwivedi
Quoted 7 lines
Quoted 6 lines
kernel/bpf/btf.c +6 -0
+                   }
+                   btf_id_tag = true;
+           } else if (!strncmp("kernel.", __btf_name_by_offset(btf, t->name_off),
+                      sizeof("kernel.") - 1)) {
+                   /* TODO: Should we reject these when loading BTF? */
+                   /* Unavailable tag in reserved tag namespace */
Quoted 7 lines
Quoted 4 lines

I don't think we need to reserve the tag space. There is little risk to break progs with future tags. I would just drop this 'if'.

Fine with dropping, but what is the expected behavior when userspace has set a tag in map value BTF that we give some meaning in the kernel later?

All of these features fall into the unstable category.
kfuncs can disappear. kernel data structs can get renamed.
dtor, kptr_get functions not only can change, but can be removed. When bpf progs are so tightly interacting with the kernel they have to change and adjust.
Eventually we might bolt a bit of CO-RE like logic to kfunc and kptr to make things more portable, but it's too early to reserve a btf_tag prefix. Progs will change. We're not saving users any headache with reservation. Tracing bpf progs are not user space. That's the key.

#14 lore raw

Re: [PATCH bpf-next v1 04/15] bpf: Allow storing referenced PTR_TO_BTF_ID in map

In reply to Kumar Kartikeya Dwivedi
Quoted 72 lines · Kumar Kartikeya Dwivedi
Quoted 62 lines · Alexei Starovoitov
Quoted 52 lines · Kumar Kartikeya Dwivedi
Quoted 33 lines · Alexei Starovoitov

kernel/bpf/verifier.c static int check_stack_access_within_bounds( · line 4586

Quoted 5 lines · Kumar Kartikeya Dwivedi

static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
int off, int bpf_size, enum bpf_access_type t,

kernel/bpf/verifier.c +2 -1
-                       int value_regno, bool strict_alignment_once)
+                       int value_regno, bool strict_alignment_once,
+                       struct bpf_reg_state *atomic_load_reg)

No new side effects please.
value_regno is not pretty already.
At least its known ugliness that we need to clean up one day.

Quoted 22 lines

static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) {

kernel/bpf/verifier.c +4 -0
+   struct bpf_reg_state atomic_load_reg;
    int load_reg;
    int err;
 
+   __mark_reg_unknown(env, &atomic_load_reg);
+
    switch (insn->imm) {
    case BPF_ADD:
    case BPF_ADD | BPF_FETCH:
@@ -4813,6 +4894,7 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
            else
                    load_reg = insn->src_reg;
 
+           atomic_load_reg = *reg_state(env, load_reg);
            /* check and record load of old value */
            err = check_reg_arg(env, load_reg, DST_OP);
            if (err)
@@ -4825,20 +4907,21 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
    }
 
Quoted 16 lines

kernel/bpf/verifier.c static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i · line 4918

Quoted 13 lines
kernel/bpf/verifier.c +4 -3
    /* Check whether we can read the memory, with second call for fetch
-    * case to simulate the register fill.
+    * case to simulate the register fill, which also triggers checks
+    * for manipulation of BTF ID pointers embedded in BPF maps.
     */
    err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
-                          BPF_SIZE(insn->code), BPF_READ, -1, true);
+                          BPF_SIZE(insn->code), BPF_READ, -1, true, NULL);
    if (!err && load_reg >= 0)
            err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
                                   BPF_SIZE(insn->code), BPF_READ, load_reg,
-                                  true);
+                                  true, load_reg >= 0 ? &atomic_load_reg : NULL);

Special xchg logic should be down outside of check_mem_access() instead of hidden by layers of calls.

Quoted 7 lines

Right, it's ugly, but if we don't capture the reg state before that check_reg_arg(env, load_reg, DST_OP), it's not possible to see the actual PTR_TO_BTF_ID being moved into the map, since check_reg_arg will do a mark_reg_unknown for value_regno. Any other ideas on what I can do?

37086bfdc737 ("bpf: Propagate stack bounds to registers in atomics w/ BPF_FETCH") changed the order of check_mem_access and DST_OP check_reg_arg.

Quoted 7 lines

That highlights my point that side effects are bad. That commit tries to work around that behavior and makes things harder to extend like you found out with xchg logic. Another option would be to add bpf_kptr_xchg() helper instead of dealing with insn. It will be tiny bit slower, but it will work on all architectures. While xchg bpf jit is on x86,s390,mips so far.

Quoted 3 lines

Right, but kfunc is currently limited to x86, which is required to obtain a refcounted PTR_TO_BTF_ID that you can move into the map, so it wouldn't make much of a difference.

Well the patches to add trampoline support to powerpc were already posted.

Quoted 11 lines
Quoted 2 lines

We need to think more on how to refactor check_mem_acess without digging ourselves into an even bigger hole.

So I'm ok with working on untangling check_mem_access as a follow up, but for now should we go forward with how it is? Just looking at it yesterday makes me think it's going to require a fair amount of refactoring and discussion.

Also, do you have any ideas on how to change it? Do you want it to work like how is_valid_access callbacks work? So passing something like a bpf_insn_access_aux into the call, where it sets how it'd like to update the register, and then actual updates take place in caller context?

I don't like callbacks in general.
They're fine for walk_the_tree, for_each_elem accessors, but passing a callback into check_mem_access is not great. Do you mind going with a bpf_kptr_xchg() helper for now and optimizing into direct xchg insn later?
It's not clear whether it's going to be faster to be noticeable.

author maintainer
#15 lore raw

Re: [PATCH bpf-next v1 04/15] bpf: Allow storing referenced PTR_TO_BTF_ID in map

Quoted 87 lines · Alexei Starovoitov
Quoted 72 lines · Kumar Kartikeya Dwivedi
Quoted 62 lines · Alexei Starovoitov
Quoted 52 lines · Kumar Kartikeya Dwivedi
Quoted 33 lines · Alexei Starovoitov

kernel/bpf/verifier.c static int check_stack_access_within_bounds( · line 4586

Quoted 5 lines · Kumar Kartikeya Dwivedi

static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
int off, int bpf_size, enum bpf_access_type t,

kernel/bpf/verifier.c +2 -1
-                       int value_regno, bool strict_alignment_once)
+                       int value_regno, bool strict_alignment_once,
+                       struct bpf_reg_state *atomic_load_reg)

No new side effects please.
value_regno is not pretty already.
At least its known ugliness that we need to clean up one day.

Quoted 22 lines

static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) {

kernel/bpf/verifier.c +4 -0
+   struct bpf_reg_state atomic_load_reg;
    int load_reg;
    int err;
 
+   __mark_reg_unknown(env, &atomic_load_reg);
+
    switch (insn->imm) {
    case BPF_ADD:
    case BPF_ADD | BPF_FETCH:
@@ -4813,6 +4894,7 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
            else
                    load_reg = insn->src_reg;
 
+           atomic_load_reg = *reg_state(env, load_reg);
            /* check and record load of old value */
            err = check_reg_arg(env, load_reg, DST_OP);
            if (err)
@@ -4825,20 +4907,21 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i
    }
 
Quoted 16 lines

kernel/bpf/verifier.c static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i · line 4918

Quoted 13 lines
kernel/bpf/verifier.c +4 -3
    /* Check whether we can read the memory, with second call for fetch
-    * case to simulate the register fill.
+    * case to simulate the register fill, which also triggers checks
+    * for manipulation of BTF ID pointers embedded in BPF maps.
     */
    err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
-                          BPF_SIZE(insn->code), BPF_READ, -1, true);
+                          BPF_SIZE(insn->code), BPF_READ, -1, true, NULL);
    if (!err && load_reg >= 0)
            err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
                                   BPF_SIZE(insn->code), BPF_READ, load_reg,
-                                  true);
+                                  true, load_reg >= 0 ? &atomic_load_reg : NULL);

Special xchg logic should be down outside of check_mem_access() instead of hidden by layers of calls.

Quoted 7 lines

Right, it's ugly, but if we don't capture the reg state before that check_reg_arg(env, load_reg, DST_OP), it's not possible to see the actual PTR_TO_BTF_ID being moved into the map, since check_reg_arg will do a mark_reg_unknown for value_regno. Any other ideas on what I can do?

37086bfdc737 ("bpf: Propagate stack bounds to registers in atomics w/ BPF_FETCH") changed the order of check_mem_access and DST_OP check_reg_arg.

Quoted 7 lines

That highlights my point that side effects are bad. That commit tries to work around that behavior and makes things harder to extend like you found out with xchg logic. Another option would be to add bpf_kptr_xchg() helper instead of dealing with insn. It will be tiny bit slower, but it will work on all architectures. While xchg bpf jit is on x86,s390,mips so far.

Quoted 3 lines

Right, but kfunc is currently limited to x86, which is required to obtain a refcounted PTR_TO_BTF_ID that you can move into the map, so it wouldn't make much of a difference.

Well the patches to add trampoline support to powerpc were already posted.

Quoted 7 lines
Quoted 2 lines

We need to think more on how to refactor check_mem_acess without digging ourselves into an even bigger hole.

So I'm ok with working on untangling check_mem_access as a follow up, but for now should we go forward with how it is? Just looking at it yesterday makes me think it's going to require a fair amount of refactoring and discussion.

Quoted 8 lines
Quoted 4 lines

Also, do you have any ideas on how to change it? Do you want it to work like how is_valid_access callbacks work? So passing something like a bpf_insn_access_aux into the call, where it sets how it'd like to update the register, and then actual updates take place in caller context?

I don't like callbacks in general.
They're fine for walk_the_tree, for_each_elem accessors, but passing a callback into check_mem_access is not great.

I didn't mean passing a callback, I meant passing a struct like you mentioned in a previous comment to another patch (btf_field_info) where we can set state that must be updated for the register, and then updates are done by the caller, to separate the 'side effects' from the other checks. is_valid_access verifier callback receive a similar bpf_insn_access_aux parameter which is then used to update register state.

Quoted 2 lines

Do you mind going with a bpf_kptr_xchg() helper for now and optimizing into direct xchg insn later?

I don't have a problem with that. I just didn't see any advantages (except the wider architecture support that you pointed out). We still have to special case some places in check_helper_call (since it needs to transfer R1's btf_id to R0, and work with all PTR_TO_BTF_ID, not just 1), so the implementation is similar.

I guess for most usecases it wouldn't matter much.

Quoted 1 line

It's not clear whether it's going to be faster to be noticeable.

Just for curiosity, I measured a loop of 5000 xchg ops, one with bpf_xchg, one with bpf_kptr_xchg. This is the simple case (uncontended, raw cost of both operations) xchg insn is at ~4 nsecs, bpf_kptr_xchg is at ~8 nsecs (single socket 8 core Intel i5 @ 2.5GHz). I'm guessing in a complicated case spill/fill of caller saved regs will also come into play for the helper case.

--
Kartikeya