Skip to contentSkip to conversation

bpf: add bpf_for_each_map_elem() helper

PATCH Superseded Yonghong Song 3 participants 28 messages Feb 4, 2021 – Feb 9, 2021

Subsystems (10)
Kselftest BPF Tracing BPF selftests +6 more subsystems

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover bpf: add bpf_for_each_map_elem() helper

This patch set introduced bpf_for_each_map_elem() helper. The helper permits bpf program iterates through all elements for a particular map.

The work originally inspired by an internal discussion where firewall rules are kept in a map and bpf prog wants to check packet 5 tuples against all rules in the map. A bounded loop can be used but it has a few drawbacks. As the loop iteration goes up, verification time goes up too. For really large maps, verification may fail.
A helper which abstracts out the loop itself will not have verification time issue.

A recent discussion in [1] involves to iterate all hash map elements in bpf program. Currently iterating all hashmap elements in bpf program is not easy if key space is really big. Having a helper to abstract out the loop itself is even more meaningful.

The proposed helper signature looks like:
long bpf_for_each_map_elem(map, callback_fn, callback_ctx, flags)
where callback_fn is a static function and callback_ctx is a piece of data allocated on the caller stack which can be accessed by the callback_fn. The callback_fn signature might be different for different maps. For example, for hash/array maps, the signature might be
long callback_fn(map, key, val, callback_ctx)

In the rest of series, Patch 1 did some refactoring. Patch 2 implemented core kernel support for the helper. Patches 3 and 4 added hashmap and arraymap support. Patches 5 and 6 added libbpf and bpftool support. Patches 7 and 8 added selftests for hashmap and arraymap.

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

Yonghong Song (8):
bpf: refactor BPF_PSEUDO_CALL checking as a helper function
bpf: add bpf_for_each_map_elem() helper
bpf: add hashtab support for bpf_for_each_map_elem() helper
bpf: add arraymap support for bpf_for_each_map_elem() helper
libbpf: support local function pointer relocation
bpftool: print local function pointer properly selftests/bpf: add hashmap test for bpf_for_each_map_elem() helper selftests/bpf: add arraymap test for bpf_for_each_map_elem() helper

 include/linux/bpf.h                           |  18 ++
 include/linux/bpf_verifier.h                  |   3 +
 include/uapi/linux/bpf.h                      |  28 ++
 kernel/bpf/arraymap.c                         |  36 +++
 kernel/bpf/bpf_iter.c                         |  16 +
 kernel/bpf/hashtab.c                          |  57 ++++
 kernel/bpf/helpers.c                          |   2 +
 kernel/bpf/verifier.c                         | 299 ++++++++++++++++--
 kernel/trace/bpf_trace.c                      |   2 +
 tools/bpf/bpftool/xlated_dumper.c             |   3 +
 tools/include/uapi/linux/bpf.h                |  28 ++
 tools/lib/bpf/libbpf.c                        |  33 +-
 .../selftests/bpf/prog_tests/for_each.c       | 145 +++++++++
 .../bpf/progs/for_each_array_map_elem.c       |  71 +++++
 .../bpf/progs/for_each_hash_map_elem.c        | 103 ++++++
 15 files changed, 811 insertions(+), 33 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/for_each.c
 create mode 100644 tools/testing/selftests/bpf/progs/for_each_array_map_elem.c
 create mode 100644 tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c
Signature · 1 line
2.24.1
#1 lore raw

PATCH 1/8 bpf: refactor BPF_PSEUDO_CALL checking as a helper function

There is no functionality change. This refactoring intends to facilitate next patch change with BPF_PSEUDO_FUNC.

---
 kernel/bpf/verifier.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
kernel/bpf/verifier.c +13 -16
index 5e09632efddb..db294b75d03b 100644
@@ -228,6 +228,12 @@ static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state)
 			     (poisoned ? BPF_MAP_KEY_POISON : 0ULL);
 }
 
+static bool bpf_pseudo_call(const struct bpf_insn *insn)
+{
+	return insn->code == (BPF_JMP | BPF_CALL) &&
+	       insn->src_reg == BPF_PSEUDO_CALL;
+}
+
 struct bpf_call_arg_meta {
 	struct bpf_map *map_ptr;
 	bool raw_mode;
@@ -1486,9 +1492,7 @@ static int check_subprogs(struct bpf_verifier_env *env)
 
 	/* determine subprog starts. The end is one before the next starts */
 	for (i = 0; i < insn_cnt; i++) {
-		if (insn[i].code != (BPF_JMP | BPF_CALL))
-			continue;
-		if (insn[i].src_reg != BPF_PSEUDO_CALL)
+		if (!bpf_pseudo_call(insn + i))
 			continue;
 		if (!env->bpf_capable) {
 			verbose(env,
@@ -3074,9 +3078,7 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
 continue_func:
 	subprog_end = subprog[idx + 1].start;
 	for (; i < subprog_end; i++) {
-		if (insn[i].code != (BPF_JMP | BPF_CALL))
-			continue;
-		if (insn[i].src_reg != BPF_PSEUDO_CALL)
+		if (!bpf_pseudo_call(insn + i))
 			continue;
 		/* remember insn and function to return to */
 		ret_insn[frame] = i + 1;
@@ -10844,8 +10846,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 		return 0;
 
 	for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
-		if (insn->code != (BPF_JMP | BPF_CALL) ||
-		    insn->src_reg != BPF_PSEUDO_CALL)
+		if (!bpf_pseudo_call(insn))
 			continue;
 		/* Upon error here we cannot fall back to interpreter but
 		 * need a hard reject of the program. Thus -EFAULT is
@@ -10974,8 +10975,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 	for (i = 0; i < env->subprog_cnt; i++) {
 		insn = func[i]->insnsi;
 		for (j = 0; j < func[i]->len; j++, insn++) {
-			if (insn->code != (BPF_JMP | BPF_CALL) ||
-			    insn->src_reg != BPF_PSEUDO_CALL)
+			if (!bpf_pseudo_call(insn))
 				continue;
 			subprog = insn->off;
 			insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
@@ -11020,8 +11020,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 	 * later look the same as if they were interpreted only.
 	 */
 	for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
-		if (insn->code != (BPF_JMP | BPF_CALL) ||
-		    insn->src_reg != BPF_PSEUDO_CALL)
+		if (!bpf_pseudo_call(insn))
 			continue;
 		insn->off = env->insn_aux_data[i].call_imm;
 		subprog = find_subprog(env, i + insn->off + 1);
@@ -11050,8 +11049,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 	/* cleanup main prog to be interpreted */
 	prog->jit_requested = 0;
 	for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
-		if (insn->code != (BPF_JMP | BPF_CALL) ||
-		    insn->src_reg != BPF_PSEUDO_CALL)
+		if (!bpf_pseudo_call(insn))
 			continue;
 		insn->off = 0;
 		insn->imm = env->insn_aux_data[i].call_imm;
@@ -11086,8 +11084,7 @@ static int fixup_call_args(struct bpf_verifier_env *env)
 		return -EINVAL;
 	}
 	for (i = 0; i < prog->len; i++, insn++) {
-		if (insn->code != (BPF_JMP | BPF_CALL) ||
-		    insn->src_reg != BPF_PSEUDO_CALL)
+		if (!bpf_pseudo_call(insn))
 			continue;
 		depth = get_callee_stack_depth(env, insn, i);
 		if (depth < 0)
Signature · 1 line
2.24.1
#2 lore raw

Re: [PATCH bpf-next 1/8] bpf: refactor BPF_PSEUDO_CALL checking as a helper function

Quoted 26 lines · Yonghong Song

There is no functionality change. This refactoring intends to facilitate next patch change with BPF_PSEUDO_FUNC.

---
 kernel/bpf/verifier.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
kernel/bpf/verifier.c +6 -0
index 5e09632efddb..db294b75d03b 100644
@@ -228,6 +228,12 @@ static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state)
 			     (poisoned ? BPF_MAP_KEY_POISON : 0ULL);
 }
 
+static bool bpf_pseudo_call(const struct bpf_insn *insn)
+{
+	return insn->code == (BPF_JMP | BPF_CALL) &&
+	       insn->src_reg == BPF_PSEUDO_CALL;
+}
+
 struct bpf_call_arg_meta {
 	struct bpf_map *map_ptr;
 	bool raw_mode;

kernel/bpf/verifier.c static int check_subprogs(struct bpf_verifier_env *env) · line 1495

Quoted 7 lines
kernel/bpf/verifier.c +1 -3
@@ -1486,9 +1492,7 @@ static int check_subprogs(struct bpf_verifier_env *env)
 
 	/* determine subprog starts. The end is one before the next starts */
 	for (i = 0; i < insn_cnt; i++) {
-		if (insn[i].code != (BPF_JMP | BPF_CALL))
-			continue;
-		if (insn[i].src_reg != BPF_PSEUDO_CALL)
+		if (!bpf_pseudo_call(insn + i))

Nice cleanup! I've applied this patch from the set.

#3 lore raw

PATCH 2/8 bpf: add bpf_for_each_map_elem() helper

In reply to Yonghong Song

The bpf_for_each_map_elem() helper is introduced which iterates all map elements with a callback function. The helper signature looks like
long bpf_for_each_map_elem(map, callback_fn, callback_ctx, flags)
and for each map element, the callback_fn will be called. For example, like hashmap, the callback signature may look like
long callback_fn(map, key, val, callback_ctx)

There are two known use cases for this. One is from upstream ([1]) where a for_each_map_elem helper may help implement a timeout mechanism in a more generic way. Another is from our internal discussion for a firewall use case where a map contains all the rules. The packet data can be compared to all these rules to decide allow or deny the packet.

For array maps, users can already use a bounded loop to traverse elements. Using this helper can avoid using bounded loop. For other type of maps (e.g., hash maps) where bounded loop is hard or impossible to use, this helper provides a convenient way to operate on all elements.

For callback_fn, besides map and map element, a callback_ctx, allocated on caller stack, is also passed to the callback function. This callback_ctx argument can provide additional input and allow to write to caller stack for output.

If the callback_fn returns 0, the helper will iterate through next element if available. If the callback_fn returns 1, the helper will stop iterating and returns to the bpf program. Other return values are not used for now.

Currently, this helper is only available with jit. It is possible to make it work with interpreter with so effort but I leave it as the future work.

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

---
 include/linux/bpf.h            |  14 ++
 include/linux/bpf_verifier.h   |   3 +
 include/uapi/linux/bpf.h       |  28 ++++
 kernel/bpf/bpf_iter.c          |  16 +++
 kernel/bpf/helpers.c           |   2 +
 kernel/bpf/verifier.c          | 251 ++++++++++++++++++++++++++++++---
 kernel/trace/bpf_trace.c       |   2 +
 tools/include/uapi/linux/bpf.h |  28 ++++
 8 files changed, 328 insertions(+), 16 deletions(-)
include/linux/bpf.h +14 -0
index 321966fc35db..c8b72ae16cc5 100644
@@ -40,6 +40,7 @@ struct bpf_local_storage;
 struct bpf_local_storage_map;
 struct kobject;
 struct mem_cgroup;
+struct bpf_func_state;
 
 extern struct idr btf_idr;
 extern spinlock_t btf_idr_lock;
@@ -130,6 +131,13 @@ struct bpf_map_ops {
 	bool (*map_meta_equal)(const struct bpf_map *meta0,
 			       const struct bpf_map *meta1);
 
+
+	int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
+					      struct bpf_func_state *caller,
+					      struct bpf_func_state *callee);
+	int (*map_for_each_callback)(struct bpf_map *map, void *callback_fn,
+				     void *callback_ctx, u64 flags);
+
 	/* BTF name and id of struct allocated by map_alloc */
 	const char * const map_btf_name;
 	int *map_btf_id;
@@ -296,6 +304,8 @@ enum bpf_arg_type {
 	ARG_CONST_ALLOC_SIZE_OR_ZERO,	/* number of allocated bytes requested */
 	ARG_PTR_TO_BTF_ID_SOCK_COMMON,	/* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
 	ARG_PTR_TO_PERCPU_BTF_ID,	/* pointer to in-kernel percpu type */
+	ARG_PTR_TO_FUNC,	/* pointer to a bpf program function */
+	ARG_PTR_TO_STACK_OR_NULL,	/* pointer to stack or NULL */
 	__BPF_ARG_TYPE_MAX,
 };
 
@@ -412,6 +422,8 @@ enum bpf_reg_type {
 	PTR_TO_RDWR_BUF,	 /* reg points to a read/write buffer */
 	PTR_TO_RDWR_BUF_OR_NULL, /* reg points to a read/write buffer or NULL */
 	PTR_TO_PERCPU_BTF_ID,	 /* reg points to a percpu kernel variable */
+	PTR_TO_FUNC,		 /* reg points to a bpf program function */
+	PTR_TO_MAP_KEY,		 /* reg points to map element key */
 };
 
 /* The information passed from prog-specific *_is_valid_access
@@ -794,6 +806,7 @@ struct bpf_prog_aux {
 	bool func_proto_unreliable;
 	bool sleepable;
 	bool tail_call_reachable;
+	bool with_callback_fn;
 	enum bpf_tramp_prog_type trampoline_prog_type;
 	struct hlist_node tramp_hlist;
 	/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
@@ -1888,6 +1901,7 @@ extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
 extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
 extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
 extern const struct bpf_func_proto bpf_sock_from_file_proto;
+extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
 
 const struct bpf_func_proto *bpf_tracing_func_proto(
 	enum bpf_func_id func_id, const struct bpf_prog *prog);
include/linux/bpf_verifier.h +3 -0
index dfe6f85d97dd..c4366b3da342 100644
@@ -68,6 +68,8 @@ struct bpf_reg_state {
 			unsigned long raw1;
 			unsigned long raw2;
 		} raw;
+
+		u32 subprog; /* for PTR_TO_FUNC */
 	};
 	/* For PTR_TO_PACKET, used to find other pointers with the same variable
 	 * offset, so they can share range knowledge.
@@ -204,6 +206,7 @@ struct bpf_func_state {
 	int acquired_refs;
 	struct bpf_reference_state *refs;
 	int allocated_stack;
+	bool with_callback_fn;
 	struct bpf_stack_state *stack;
 };
 
include/uapi/linux/bpf.h +28 -0
index c001766adcbc..d55bd4557376 100644
@@ -393,6 +393,15 @@ enum bpf_link_type {
  *                   is struct/union.
  */
 #define BPF_PSEUDO_BTF_ID	3
+/* insn[0].src_reg:  BPF_PSEUDO_FUNC
+ * insn[0].imm:      insn offset to the func
+ * insn[1].imm:      0
+ * insn[0].off:      0
+ * insn[1].off:      0
+ * ldimm64 rewrite:  address of the function
+ * verifier type:    PTR_TO_FUNC.
+ */
+#define BPF_PSEUDO_FUNC		4
 
 /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative
  * offset to another bpf function
@@ -3836,6 +3845,24 @@ union bpf_attr {
  *	Return
  *		A pointer to a struct socket on success or NULL if the file is
  *		not a socket.
+ *
+ * long bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, void *callback_ctx, u64 flags)
+ *	Description
+ *		For each element in **map**, call **callback_fn** function with
+ *		**map**, **callback_ctx** and other map-specific parameters.
+ *		For example, for hash and array maps, the callback signature can
+ *		be `u64 callback_fn(map, map_key, map_value, callback_ctx)`.
+ *		The **callback_fn** should be a static function and
+ *		the **callback_ctx** should be a pointer to the stack.
+ *		The **flags** is used to control certain aspects of the helper.
+ *		Currently, the **flags** must be 0.
+ *
+ *		If **callback_fn** return 0, the helper will continue to the next
+ *		element. If return value is 1, the helper will skip the rest of
+ *		elements and return. Other return values are not used now.
+ *	Return
+ *		0 for success, **-EINVAL** for invalid **flags** or unsupported
+ *		**callback_fn** return value.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -4001,6 +4028,7 @@ union bpf_attr {
 	FN(ktime_get_coarse_ns),	\
 	FN(ima_inode_hash),		\
 	FN(sock_from_file),		\
+	FN(for_each_map_elem),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/bpf_iter.c +16 -0
index 5454161407f1..5187f49d3216 100644
@@ -675,3 +675,19 @@ int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx)
 	 */
 	return ret == 0 ? 0 : -EAGAIN;
 }
+
+BPF_CALL_4(bpf_for_each_map_elem, struct bpf_map *, map, void *, callback_fn,
+	   void *, callback_ctx, u64, flags)
+{
+	return map->ops->map_for_each_callback(map, callback_fn, callback_ctx, flags);
+}
+
+const struct bpf_func_proto bpf_for_each_map_elem_proto = {
+	.func		= bpf_for_each_map_elem,
+	.gpl_only	= false,
+	.ret_type	= RET_INTEGER,
+	.arg1_type	= ARG_CONST_MAP_PTR,
+	.arg2_type	= ARG_PTR_TO_FUNC,
+	.arg3_type	= ARG_PTR_TO_STACK_OR_NULL,
+	.arg4_type	= ARG_ANYTHING,
+};
kernel/bpf/helpers.c +2 -0
index 308427fe03a3..074800226327 100644
@@ -708,6 +708,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
 		return &bpf_ringbuf_discard_proto;
 	case BPF_FUNC_ringbuf_query:
 		return &bpf_ringbuf_query_proto;
+	case BPF_FUNC_for_each_map_elem:
+		return &bpf_for_each_map_elem_proto;
 	default:
 		break;
 	}
kernel/bpf/verifier.c +235 -16

488 lines not on this page · raw message

kernel/trace/bpf_trace.c +2 -0
index 6c0018abe68a..8338333bfeb0 100644
@@ -1366,6 +1366,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_per_cpu_ptr_proto;
 	case BPF_FUNC_this_cpu_ptr:
 		return &bpf_this_cpu_ptr_proto;
+	case BPF_FUNC_for_each_map_elem:
+		return &bpf_for_each_map_elem_proto;
 	default:
 		return NULL;
 	}
tools/include/uapi/linux/bpf.h +28 -0
index c001766adcbc..d55bd4557376 100644
@@ -393,6 +393,15 @@ enum bpf_link_type {
  *                   is struct/union.
  */
 #define BPF_PSEUDO_BTF_ID	3
+/* insn[0].src_reg:  BPF_PSEUDO_FUNC
+ * insn[0].imm:      insn offset to the func
+ * insn[1].imm:      0
+ * insn[0].off:      0
+ * insn[1].off:      0
+ * ldimm64 rewrite:  address of the function
+ * verifier type:    PTR_TO_FUNC.
+ */
+#define BPF_PSEUDO_FUNC		4
 
 /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative
  * offset to another bpf function
@@ -3836,6 +3845,24 @@ union bpf_attr {
  *	Return
  *		A pointer to a struct socket on success or NULL if the file is
  *		not a socket.
+ *
+ * long bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, void *callback_ctx, u64 flags)
+ *	Description
+ *		For each element in **map**, call **callback_fn** function with
+ *		**map**, **callback_ctx** and other map-specific parameters.
+ *		For example, for hash and array maps, the callback signature can
+ *		be `u64 callback_fn(map, map_key, map_value, callback_ctx)`.
+ *		The **callback_fn** should be a static function and
+ *		the **callback_ctx** should be a pointer to the stack.
+ *		The **flags** is used to control certain aspects of the helper.
+ *		Currently, the **flags** must be 0.
+ *
+ *		If **callback_fn** return 0, the helper will continue to the next
+ *		element. If return value is 1, the helper will skip the rest of
+ *		elements and return. Other return values are not used now.
+ *	Return
+ *		0 for success, **-EINVAL** for invalid **flags** or unsupported
+ *		**callback_fn** return value.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -4001,6 +4028,7 @@ union bpf_attr {
 	FN(ktime_get_coarse_ns),	\
 	FN(ima_inode_hash),		\
 	FN(sock_from_file),		\
+	FN(for_each_map_elem),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.24.1
#4 lore raw
Quoted 21 lines · Yonghong Song

The bpf_for_each_map_elem() helper is introduced which iterates all map elements with a callback function. The helper signature looks like
long bpf_for_each_map_elem(map, callback_fn, callback_ctx, flags)
and for each map element, the callback_fn will be called. For example, like hashmap, the callback signature may look like
long callback_fn(map, key, val, callback_ctx)

There are two known use cases for this. One is from upstream ([1]) where a for_each_map_elem helper may help implement a timeout mechanism in a more generic way. Another is from our internal discussion for a firewall use case where a map contains all the rules. The packet data can be compared to all these rules to decide allow or deny the packet.

For array maps, users can already use a bounded loop to traverse elements. Using this helper can avoid using bounded loop. For other type of maps (e.g., hash maps) where bounded loop is hard or impossible to use, this helper provides a convenient way to operate on all elements.

Quoted 4 lines

For callback_fn, besides map and map element, a callback_ctx, allocated on caller stack, is also passed to the callback function. This callback_ctx argument can provide additional input and allow to write to caller stack for output.

The approach and implementation look great!
Few ideas below:

Quoted 40 lines
kernel/bpf/verifier.c +40 -0
+static int check_map_elem_callback(struct bpf_verifier_env *env, int *insn_idx)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	struct bpf_prog_aux *aux = env->prog->aux;
+	struct bpf_func_state *caller, *callee;
+	struct bpf_map *map;
+	int err, subprog;
+
+	if (state->curframe + 1 >= MAX_CALL_FRAMES) {
+		verbose(env, "the call stack of %d frames is too deep\n",
+			state->curframe + 2);
+		return -E2BIG;
+	}
+
+	caller = state->frame[state->curframe];
+	if (state->frame[state->curframe + 1]) {
+		verbose(env, "verifier bug. Frame %d already allocated\n",
+			state->curframe + 1);
+		return -EFAULT;
+	}
+
+	caller->with_callback_fn = true;
+
+	callee = kzalloc(sizeof(*callee), GFP_KERNEL);
+	if (!callee)
+		return -ENOMEM;
+	state->frame[state->curframe + 1] = callee;
+
+	/* callee cannot access r0, r6 - r9 for reading and has to write
+	 * into its own stack before reading from it.
+	 * callee can read/write into caller's stack
+	 */
+	init_func_state(env, callee,
+			/* remember the callsite, it will be used by bpf_exit */
+			*insn_idx /* callsite */,
+			state->curframe + 1 /* frameno within this callchain */,
+			subprog /* subprog number within this prog */);
+
+	/* Transfer references to the callee */
+	err = transfer_reference_state(callee, caller);

kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 5039

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+	if (err)
+		return err;
+
+	subprog = caller->regs[BPF_REG_2].subprog;
+	if (aux->func_info && aux->func_info_aux[subprog].linkage != BTF_FUNC_STATIC) {
+		verbose(env, "callback function R2 not static\n");
+		return -EINVAL;
+	}
+
+	map = caller->regs[BPF_REG_1].map_ptr;

Take a look at for (i = 0; i < 5; i++) err = check_func_arg loop and record_func_map. It stores the map pointer into map_ptr_state and makes sure it's unique, so that program doesn't try to pass two different maps into the same 'call insn'. It can make this function a bit more generic.
There would be no need to hard code regs[BPF_REG_1].
The code would take it from map_ptr_state.
Also it will help later with optimizing
return map->ops->map_for_each_callback(map, callback_fn, callback_ctx, flags);
since the map pointer will be the same the optimization (that is applied to other map operations) can be applied for this callback as well.

The regs[BPF_REG_2] can be generalized a bit as well.
It think linkage != BTF_FUNC_STATIC can be moved to early check_ld_imm phase. While here the check_func_arg() loop can look for PTR_TO_FUNC type, remeber the subprog into meta (just like map_ptr_state) and ... continues below

Quoted 43 lines
kernel/bpf/verifier.c +31 -1
+	if (!map->ops->map_set_for_each_callback_args ||
+	    !map->ops->map_for_each_callback) {
+		verbose(env, "callback function not allowed for map R1\n");
+		return -ENOTSUPP;
+	}
+
+	/* the following is only for hashmap, different maps
+	 * can have different callback signatures.
+	 */
+	err = map->ops->map_set_for_each_callback_args(env, caller, callee);
+	if (err)
+		return err;
+
+	clear_caller_saved_regs(env, caller->regs);
+
+	/* only increment it after check_reg_arg() finished */
+	state->curframe++;
+
+	/* and go analyze first insn of the callee */
+	*insn_idx = env->subprog_info[subprog].start - 1;
+
+	if (env->log.level & BPF_LOG_LEVEL) {
+		verbose(env, "caller:\n");
+		print_verifier_state(env, caller);
+		verbose(env, "callee:\n");
+		print_verifier_state(env, callee);
+	}
+	return 0;
+}
+
 static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 {
 	struct bpf_verifier_state *state = env->cur_state;
 	struct bpf_func_state *caller, *callee;
 	struct bpf_reg_state *r0;
-	int err;
+	int i, err;
 
 	callee = state->frame[state->curframe];
 	r0 = &callee->regs[BPF_REG_0];
@@ -4955,7 +5090,17 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 	state->curframe--;
 	caller = state->frame[state->curframe];

kernel/bpf/verifier.c static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) · line 5100

Quoted 10 lines
kernel/bpf/verifier.c +8 -1
 	/* return to the caller whatever r0 had in the callee */
-	caller->regs[BPF_REG_0] = *r0;
+	if (caller->with_callback_fn) {
+		/* reset caller saved regs, the helper calling callback_fn
+		 * has RET_INTEGER return types.
+		 */
+		for (i = 0; i < CALLER_SAVED_REGS; i++)
+			mark_reg_not_init(env, caller->regs, caller_saved[i]);
+		caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
+		mark_reg_unknown(env, caller->regs, BPF_REG_0);

this part can stay in check_helper_call().

Quoted 40 lines
kernel/bpf/verifier.c +8 -4
+	} else {
+		caller->regs[BPF_REG_0] = *r0;
+	}
 
 	/* Transfer references to the caller */
 	err = transfer_reference_state(caller, callee);
@@ -5091,7 +5236,8 @@ static int check_reference_leak(struct bpf_verifier_env *env)
 	return state->acquired_refs ? -EINVAL : 0;
 }
 
-static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
+static int check_helper_call(struct bpf_verifier_env *env, int func_id, int *insn_idx,
+			     bool map_elem_callback)
 {
 	const struct bpf_func_proto *fn = NULL;
 	struct bpf_reg_state *regs;
@@ -5151,11 +5297,11 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
 			return err;
 	}
 
-	err = record_func_map(env, &meta, func_id, insn_idx);
+	err = record_func_map(env, &meta, func_id, *insn_idx);
 	if (err)
 		return err;
 
-	err = record_func_key(env, &meta, func_id, insn_idx);
+	err = record_func_key(env, &meta, func_id, *insn_idx);
 	if (err)
 		return err;
 
@@ -5163,7 +5309,7 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
 	 * is inferred from register state.
 	 */
 	for (i = 0; i < meta.access_size; i++) {
-		err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
+		err = check_mem_access(env, *insn_idx, meta.regno, i, BPF_B,
 				       BPF_WRITE, -1, false);
 		if (err)
 			return err;

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn · line 5346

Quoted 6 lines
kernel/bpf/verifier.c +3 -0
@@ -5195,6 +5341,11 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
 		return -EINVAL;
 	}
 
+	if (map_elem_callback) {
+		env->prog->aux->with_callback_fn = true;
+		return check_map_elem_callback(env, insn_idx);

Instead of returning early here.
The check_func_arg() loop can look for PTR_TO_FUNC type. The allocate new callee state,
do map_set_for_each_callback_args() here.
and then proceed further.

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn · line 5351

Quoted 5 lines
kernel/bpf/verifier.c +2 -0
+	}
+
 	/* reset caller saved regs */
 	for (i = 0; i < CALLER_SAVED_REGS; i++) {
 		mark_reg_not_init(env, regs, caller_saved[i]);

Instead of doing this loop in prepare_func_exit().
This code can just proceed here and clear caller regs. This loop can stay as-is. The transfer of caller->callee would happen already.

Then there are few lines here that diff didn't show.
They do regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG and mark_reg_unknown. No need to do them in prepare_func_exit().
This function can proceed further reusing this caller regs clearing loop and r0 marking.

Then before returning from check_helper_call()
it will do what you have in check_map_elem_callback() and it will adjust *insn_idx.

At this point caller would have regs cleared and r0=undef.
And callee would have regs setup the way map_set_for_each_callback_args callback meant to do it. The only thing prepare_func_exit would need to do is to make sure that assignment:
caller->regs[BPF_REG_0] = *r0
doesn't happen. caller's r0 was already set to undef.
To achieve that I think would be a bit cleaner to mark callee state instead of caller state. So instead of caller->with_callback_fn=true maybe callee->in_callback_fn=true ?

Quoted 33 lines
kernel/bpf/verifier.c +10 -1
@@ -5306,7 +5457,7 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
 		/* For release_reference() */
 		regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
 	} else if (is_acquire_function(func_id, meta.map_ptr)) {
-		int id = acquire_reference_state(env, insn_idx);
+		int id = acquire_reference_state(env, *insn_idx);
 
 		if (id < 0)
 			return id;
@@ -5448,6 +5599,14 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
 		else
 			*ptr_limit = -off;
 		return 0;
+	case PTR_TO_MAP_KEY:
+		if (mask_to_left) {
+			*ptr_limit = ptr_reg->umax_value + ptr_reg->off;
+		} else {
+			off = ptr_reg->smin_value + ptr_reg->off;
+			*ptr_limit = ptr_reg->map_ptr->key_size - off;
+		}
+		return 0;
 	case PTR_TO_MAP_VALUE:
 		if (mask_to_left) {
 			*ptr_limit = ptr_reg->umax_value + ptr_reg->off;
@@ -5614,6 +5773,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 		verbose(env, "R%d pointer arithmetic on %s prohibited\n",
 			dst, reg_type_str[ptr_reg->type]);
 		return -EACCES;
+	case PTR_TO_MAP_KEY:
 	case PTR_TO_MAP_VALUE:
 		if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
 			verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",

kernel/bpf/verifier.c static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) · line 7983

Quoted 6 lines
kernel/bpf/verifier.c +3 -0
@@ -7818,6 +7978,12 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
 		return 0;
 	}
 
+	if (insn->src_reg == BPF_PSEUDO_FUNC) {
+		dst_reg->type = PTR_TO_FUNC;
+		dst_reg->subprog = insn[1].imm;

Like here check for linkage==static can happen ?

Quoted 50 lines
kernel/bpf/verifier.c +21 -1
+		return 0;
+	}
+
 	map = env->used_maps[aux->map_index];
 	mark_reg_known_zero(env, regs, insn->dst_reg);
 	dst_reg->map_ptr = map;
@@ -8195,9 +8361,23 @@ static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env)
 
 	/* All non-branch instructions have a single fall-through edge. */
 	if (BPF_CLASS(insns[t].code) != BPF_JMP &&
-	    BPF_CLASS(insns[t].code) != BPF_JMP32)
+	    BPF_CLASS(insns[t].code) != BPF_JMP32 &&
+	    !bpf_pseudo_func(insns + t))
 		return push_insn(t, t + 1, FALLTHROUGH, env, false);
 
+	if (bpf_pseudo_func(insns + t)) {
+		ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
+		if (ret)
+			return ret;
+
+		if (t + 1 < insn_cnt)
+			init_explored_state(env, t + 1);
+		init_explored_state(env, t);
+		ret = push_insn(t, t + insns[t].imm + 1, BRANCH,
+				env, false);
+		return ret;
+	}
+
 	switch (BPF_OP(insns[t].code)) {
 	case BPF_EXIT:
 		return DONE_EXPLORING;
@@ -8819,6 +8999,7 @@ static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
 			 */
 			return false;
 		}
+	case PTR_TO_MAP_KEY:
 	case PTR_TO_MAP_VALUE:
 		/* If the new min/max/var_off satisfy the old ones and
 		 * everything else matches, we are OK.
@@ -9646,6 +9827,8 @@ static int do_check(struct bpf_verifier_env *env)
 
 			env->jmps_processed++;
 			if (opcode == BPF_CALL) {
+				bool map_elem_callback;
+
 				if (BPF_SRC(insn->code) != BPF_K ||
 				    insn->off != 0 ||
 				    (insn->src_reg != BPF_REG_0 &&
@@ -9662,13 +9845,15 @@ static int do_check(struct bpf_verifier_env *env)
 					verbose(env, "function calls are not allowed while holding a lock\n");

kernel/bpf/verifier.c static int do_check(struct bpf_verifier_env *env) · line 9854

Quoted 10 lines
kernel/bpf/verifier.c +4 -1
 					return -EINVAL;
 				}
+				map_elem_callback = insn->src_reg != BPF_PSEUDO_CALL &&
+						   insn->imm == BPF_FUNC_for_each_map_elem;
 				if (insn->src_reg == BPF_PSEUDO_CALL)
 					err = check_func_call(env, insn, &env->insn_idx);
 				else
-					err = check_helper_call(env, insn->imm, env->insn_idx);
+					err = check_helper_call(env, insn->imm, &env->insn_idx,
+								map_elem_callback);

then hopefully this extra 'map_elem_callback' boolean won't be needed. Only env->insn_idx into &env->insn_idx.
In that sense check_helper_call will become a superset of check_func_call. Maybe some code between them can be shared too.
Beyond bpf_for_each_map_elem() helper other helpers might use PTR_TO_FUNC. I hope with this approach all of them will be handled a bit more generically.

Quoted 75 lines
kernel/bpf/verifier.c +30 -4
 				if (err)
 					return err;
-
 			} else if (opcode == BPF_JA) {
 				if (BPF_SRC(insn->code) != BPF_K ||
 				    insn->imm != 0 ||
@@ -10090,6 +10275,12 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env)
 				goto next_insn;
 			}
 
+			if (insn[0].src_reg == BPF_PSEUDO_FUNC) {
+				aux = &env->insn_aux_data[i];
+				aux->ptr_type = PTR_TO_FUNC;
+				goto next_insn;
+			}
+
 			/* In final convert_pseudo_ld_imm64() step, this is
 			 * converted into regular 64-bit imm load insn.
 			 */
@@ -10222,9 +10413,13 @@ static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
 	int insn_cnt = env->prog->len;
 	int i;
 
-	for (i = 0; i < insn_cnt; i++, insn++)
-		if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
-			insn->src_reg = 0;
+	for (i = 0; i < insn_cnt; i++, insn++) {
+		if (insn->code != (BPF_LD | BPF_IMM | BPF_DW))
+			continue;
+		if (insn->src_reg == BPF_PSEUDO_FUNC)
+			continue;
+		insn->src_reg = 0;
+	}
 }
 
 /* single env->prog->insni[off] instruction was replaced with the range
@@ -10846,6 +11041,12 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 		return 0;
 
 	for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
+		if (bpf_pseudo_func(insn)) {
+			env->insn_aux_data[i].call_imm = insn->imm;
+			/* subprog is encoded in insn[1].imm */
+			continue;
+		}
+
 		if (!bpf_pseudo_call(insn))
 			continue;
 		/* Upon error here we cannot fall back to interpreter but
@@ -10975,6 +11176,12 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 	for (i = 0; i < env->subprog_cnt; i++) {
 		insn = func[i]->insnsi;
 		for (j = 0; j < func[i]->len; j++, insn++) {
+			if (bpf_pseudo_func(insn)) {
+				subprog = insn[1].imm;
+				insn[0].imm = (u32)(long)func[subprog]->bpf_func;
+				insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32;
+				continue;
+			}
 			if (!bpf_pseudo_call(insn))
 				continue;
 			subprog = insn->off;
@@ -11020,6 +11227,11 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 	 * later look the same as if they were interpreted only.
 	 */
 	for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
+		if (bpf_pseudo_func(insn)) {
+			insn[0].imm = env->insn_aux_data[i].call_imm;
+			insn[1].imm = find_subprog(env, i + insn[0].imm + 1);
+			continue;
+		}
 		if (!bpf_pseudo_call(insn))
 			continue;
 		insn->off = env->insn_aux_data[i].call_imm;

kernel/bpf/verifier.c static int fixup_call_args(struct bpf_verifier_env *env) · line 11298

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -11083,6 +11295,13 @@ static int fixup_call_args(struct bpf_verifier_env *env)
 		verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n");
 		return -EINVAL;
 	}
+	if (env->subprog_cnt > 1 && env->prog->aux->with_callback_fn) {

Does this bool really need to be be part of 'aux'?
There is a loop below that does if (!bpf_pseudo_call
to fixup insns for the interpreter.
May be add if (bpf_pseudo_func()) { return callbacks are not allowed in non-JITed } to the loop below as well?
It's a trade off between memory and few extra insn.

kernel/bpf/verifier.c static int fixup_call_args(struct bpf_verifier_env *env) · line 11307

Quoted 9 lines
kernel/bpf/verifier.c +6 -0
+		/* When JIT fails the progs with callback calls
+		 * have to be rejected, since interpreter doesn't support them yet.
+		 */
+		verbose(env, "callbacks are not allowed in non-JITed programs\n");
+		return -EINVAL;
+	}
 	for (i = 0; i < prog->len; i++, insn++) {
 		if (!bpf_pseudo_call(insn))
 			continue;

to this loop.

Thanks!

#5 lore raw
Quoted 83 lines · Alexei Starovoitov
Quoted 21 lines · Yonghong Song

The bpf_for_each_map_elem() helper is introduced which iterates all map elements with a callback function. The helper signature looks like
long bpf_for_each_map_elem(map, callback_fn, callback_ctx, flags)
and for each map element, the callback_fn will be called. For example, like hashmap, the callback signature may look like
long callback_fn(map, key, val, callback_ctx)

There are two known use cases for this. One is from upstream ([1]) where a for_each_map_elem helper may help implement a timeout mechanism in a more generic way. Another is from our internal discussion for a firewall use case where a map contains all the rules. The packet data can be compared to all these rules to decide allow or deny the packet.

For array maps, users can already use a bounded loop to traverse elements. Using this helper can avoid using bounded loop. For other type of maps (e.g., hash maps) where bounded loop is hard or impossible to use, this helper provides a convenient way to operate on all elements.

Quoted 4 lines

For callback_fn, besides map and map element, a callback_ctx, allocated on caller stack, is also passed to the callback function. This callback_ctx argument can provide additional input and allow to write to caller stack for output.

The approach and implementation look great!
Few ideas below:

Quoted 40 lines
kernel/bpf/verifier.c +40 -0
+static int check_map_elem_callback(struct bpf_verifier_env *env, int *insn_idx)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	struct bpf_prog_aux *aux = env->prog->aux;
+	struct bpf_func_state *caller, *callee;
+	struct bpf_map *map;
+	int err, subprog;
+
+	if (state->curframe + 1 >= MAX_CALL_FRAMES) {
+		verbose(env, "the call stack of %d frames is too deep\n",
+			state->curframe + 2);
+		return -E2BIG;
+	}
+
+	caller = state->frame[state->curframe];
+	if (state->frame[state->curframe + 1]) {
+		verbose(env, "verifier bug. Frame %d already allocated\n",
+			state->curframe + 1);
+		return -EFAULT;
+	}
+
+	caller->with_callback_fn = true;
+
+	callee = kzalloc(sizeof(*callee), GFP_KERNEL);
+	if (!callee)
+		return -ENOMEM;
+	state->frame[state->curframe + 1] = callee;
+
+	/* callee cannot access r0, r6 - r9 for reading and has to write
+	 * into its own stack before reading from it.
+	 * callee can read/write into caller's stack
+	 */
+	init_func_state(env, callee,
+			/* remember the callsite, it will be used by bpf_exit */
+			*insn_idx /* callsite */,
+			state->curframe + 1 /* frameno within this callchain */,
+			subprog /* subprog number within this prog */);
+
+	/* Transfer references to the callee */
+	err = transfer_reference_state(callee, caller);

kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 5039

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+	if (err)
+		return err;
+
+	subprog = caller->regs[BPF_REG_2].subprog;
+	if (aux->func_info && aux->func_info_aux[subprog].linkage != BTF_FUNC_STATIC) {
+		verbose(env, "callback function R2 not static\n");
+		return -EINVAL;
+	}
+
+	map = caller->regs[BPF_REG_1].map_ptr;

Take a look at for (i = 0; i < 5; i++) err = check_func_arg loop and record_func_map. It stores the map pointer into map_ptr_state and makes sure it's unique,

Quoted 8 lines

so that program doesn't try to pass two different maps into the same 'call insn'. It can make this function a bit more generic.
There would be no need to hard code regs[BPF_REG_1].
The code would take it from map_ptr_state.
Also it will help later with optimizing
return map->ops->map_for_each_callback(map, callback_fn, callback_ctx, flags);
since the map pointer will be the same the optimization (that is applied to other map operations) can be applied for this callback as well.

sounds good. will try this approach in the next revision.

Quoted 5 lines

The regs[BPF_REG_2] can be generalized a bit as well.
It think linkage != BTF_FUNC_STATIC can be moved to early check_ld_imm phase. While here the check_func_arg() loop can look for PTR_TO_FUNC type, remeber the subprog into meta (just like map_ptr_state) and ... continues below

Yes, PTR_TO_FUNC might be used for future helpers like bpf_mod_timer() or bpf_for_each_task() etc. Make it more general do make sense.

Quoted 48 lines
Quoted 48 lines
kernel/bpf/verifier.c +33 -2
+	if (!map->ops->map_set_for_each_callback_args ||
+	    !map->ops->map_for_each_callback) {
+		verbose(env, "callback function not allowed for map R1\n");
+		return -ENOTSUPP;
+	}
+
+	/* the following is only for hashmap, different maps
+	 * can have different callback signatures.
+	 */
+	err = map->ops->map_set_for_each_callback_args(env, caller, callee);
+	if (err)
+		return err;
+
+	clear_caller_saved_regs(env, caller->regs);
+
+	/* only increment it after check_reg_arg() finished */
+	state->curframe++;
+
+	/* and go analyze first insn of the callee */
+	*insn_idx = env->subprog_info[subprog].start - 1;
+
+	if (env->log.level & BPF_LOG_LEVEL) {
+		verbose(env, "caller:\n");
+		print_verifier_state(env, caller);
+		verbose(env, "callee:\n");
+		print_verifier_state(env, callee);
+	}
+	return 0;
+}
+
  static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
  {
  	struct bpf_verifier_state *state = env->cur_state;
  	struct bpf_func_state *caller, *callee;
  	struct bpf_reg_state *r0;
-	int err;
+	int i, err;
 
  	callee = state->frame[state->curframe];
  	r0 = &callee->regs[BPF_REG_0];
@@ -4955,7 +5090,17 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
  	state->curframe--;
  	caller = state->frame[state->curframe];
  	/* return to the caller whatever r0 had in the callee */
-	caller->regs[BPF_REG_0] = *r0;
+	if (caller->with_callback_fn) {
+		/* reset caller saved regs, the helper calling callback_fn
Quoted 8 lines

kernel/bpf/verifier.c static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) · line 5100

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+		 * has RET_INTEGER return types.
+		 */
+		for (i = 0; i < CALLER_SAVED_REGS; i++)
+			mark_reg_not_init(env, caller->regs, caller_saved[i]);
+		caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
+		mark_reg_unknown(env, caller->regs, BPF_REG_0);

this part can stay in check_helper_call().

Yes, to verify the callback function from the helper is the most complex part in my patch set. Your above suggestions should make it more streamlined as a better fit with existing infrastruture. Will give a try.

Quoted 44 lines
Quoted 44 lines
kernel/bpf/verifier.c +8 -4
+	} else {
+		caller->regs[BPF_REG_0] = *r0;
+	}
 
  	/* Transfer references to the caller */
  	err = transfer_reference_state(caller, callee);
@@ -5091,7 +5236,8 @@ static int check_reference_leak(struct bpf_verifier_env *env)
  	return state->acquired_refs ? -EINVAL : 0;
  }
 
-static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
+static int check_helper_call(struct bpf_verifier_env *env, int func_id, int *insn_idx,
+			     bool map_elem_callback)
  {
  	const struct bpf_func_proto *fn = NULL;
  	struct bpf_reg_state *regs;
@@ -5151,11 +5297,11 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
  			return err;
  	}
 
-	err = record_func_map(env, &meta, func_id, insn_idx);
+	err = record_func_map(env, &meta, func_id, *insn_idx);
  	if (err)
  		return err;
 
-	err = record_func_key(env, &meta, func_id, insn_idx);
+	err = record_func_key(env, &meta, func_id, *insn_idx);
  	if (err)
  		return err;
 
@@ -5163,7 +5309,7 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
  	 * is inferred from register state.
  	 */
  	for (i = 0; i < meta.access_size; i++) {
-		err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
+		err = check_mem_access(env, *insn_idx, meta.regno, i, BPF_B,
  				       BPF_WRITE, -1, false);
  		if (err)
  			return err;
@@ -5195,6 +5341,11 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
  		return -EINVAL;
  	}
 
Quoted 9 lines

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn · line 5346

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+	if (map_elem_callback) {
+		env->prog->aux->with_callback_fn = true;
+		return check_map_elem_callback(env, insn_idx);

Instead of returning early here.
The check_func_arg() loop can look for PTR_TO_FUNC type. The allocate new callee state,
do map_set_for_each_callback_args() here.
and then proceed further.

ditto. Will re-organize to avoid early return here.

Quoted 19 lines

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn · line 5351

Quoted 6 lines
kernel/bpf/verifier.c +2 -0
+	}
+
  	/* reset caller saved regs */
  	for (i = 0; i < CALLER_SAVED_REGS; i++) {
  		mark_reg_not_init(env, regs, caller_saved[i]);

Instead of doing this loop in prepare_func_exit().
This code can just proceed here and clear caller regs. This loop can stay as-is. The transfer of caller->callee would happen already.

Then there are few lines here that diff didn't show.
They do regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG and mark_reg_unknown. No need to do them in prepare_func_exit().
This function can proceed further reusing this caller regs clearing loop and r0 marking.

Then before returning from check_helper_call()
it will do what you have in check_map_elem_callback() and it will adjust *insn_idx.

Quoted 7 lines

At this point caller would have regs cleared and r0=undef.
And callee would have regs setup the way map_set_for_each_callback_args callback meant to do it. The only thing prepare_func_exit would need to do is to make sure that assignment:
caller->regs[BPF_REG_0] = *r0
doesn't happen. caller's r0 was already set to undef.
To achieve that I think would be a bit cleaner to mark callee state instead of caller state. So instead of caller->with_callback_fn=true maybe callee->in_callback_fn=true ?

Agree. Will try to do normal helper return verification here instead of a cut-down version in prepare_func_exit().

Quoted 37 lines
Quoted 37 lines
kernel/bpf/verifier.c +10 -1
@@ -5306,7 +5457,7 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
  		/* For release_reference() */
  		regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
  	} else if (is_acquire_function(func_id, meta.map_ptr)) {
-		int id = acquire_reference_state(env, insn_idx);
+		int id = acquire_reference_state(env, *insn_idx);
 
  		if (id < 0)
  			return id;
@@ -5448,6 +5599,14 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
  		else
  			*ptr_limit = -off;
  		return 0;
+	case PTR_TO_MAP_KEY:
+		if (mask_to_left) {
+			*ptr_limit = ptr_reg->umax_value + ptr_reg->off;
+		} else {
+			off = ptr_reg->smin_value + ptr_reg->off;
+			*ptr_limit = ptr_reg->map_ptr->key_size - off;
+		}
+		return 0;
  	case PTR_TO_MAP_VALUE:
  		if (mask_to_left) {
  			*ptr_limit = ptr_reg->umax_value + ptr_reg->off;
@@ -5614,6 +5773,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
  		verbose(env, "R%d pointer arithmetic on %s prohibited\n",
  			dst, reg_type_str[ptr_reg->type]);
  		return -EACCES;
+	case PTR_TO_MAP_KEY:
  	case PTR_TO_MAP_VALUE:
  		if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
  			verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
@@ -7818,6 +7978,12 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
  		return 0;
  	}
 
Quoted 5 lines

kernel/bpf/verifier.c static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) · line 7983

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+	if (insn->src_reg == BPF_PSEUDO_FUNC) {
+		dst_reg->type = PTR_TO_FUNC;
+		dst_reg->subprog = insn[1].imm;

Like here check for linkage==static can happen ?

will do.

Quoted 60 lines
Quoted 60 lines
kernel/bpf/verifier.c +24 -2
+		return 0;
+	}
+
  	map = env->used_maps[aux->map_index];
  	mark_reg_known_zero(env, regs, insn->dst_reg);
  	dst_reg->map_ptr = map;
@@ -8195,9 +8361,23 @@ static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env)
 
  	/* All non-branch instructions have a single fall-through edge. */
  	if (BPF_CLASS(insns[t].code) != BPF_JMP &&
-	    BPF_CLASS(insns[t].code) != BPF_JMP32)
+	    BPF_CLASS(insns[t].code) != BPF_JMP32 &&
+	    !bpf_pseudo_func(insns + t))
  		return push_insn(t, t + 1, FALLTHROUGH, env, false);
 
+	if (bpf_pseudo_func(insns + t)) {
+		ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
+		if (ret)
+			return ret;
+
+		if (t + 1 < insn_cnt)
+			init_explored_state(env, t + 1);
+		init_explored_state(env, t);
+		ret = push_insn(t, t + insns[t].imm + 1, BRANCH,
+				env, false);
+		return ret;
+	}
+
  	switch (BPF_OP(insns[t].code)) {
  	case BPF_EXIT:
  		return DONE_EXPLORING;
@@ -8819,6 +8999,7 @@ static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
  			 */
  			return false;
  		}
+	case PTR_TO_MAP_KEY:
  	case PTR_TO_MAP_VALUE:
  		/* If the new min/max/var_off satisfy the old ones and
  		 * everything else matches, we are OK.
@@ -9646,6 +9827,8 @@ static int do_check(struct bpf_verifier_env *env)
 
  			env->jmps_processed++;
  			if (opcode == BPF_CALL) {
+				bool map_elem_callback;
+
  				if (BPF_SRC(insn->code) != BPF_K ||
  				    insn->off != 0 ||
  				    (insn->src_reg != BPF_REG_0 &&
@@ -9662,13 +9845,15 @@ static int do_check(struct bpf_verifier_env *env)
  					verbose(env, "function calls are not allowed while holding a lock\n");
  					return -EINVAL;
  				}
+				map_elem_callback = insn->src_reg != BPF_PSEUDO_CALL &&
+						   insn->imm == BPF_FUNC_for_each_map_elem;
  				if (insn->src_reg == BPF_PSEUDO_CALL)
  					err = check_func_call(env, insn, &env->insn_idx);
  				else
-					err = check_helper_call(env, insn->imm, env->insn_idx);
+					err = check_helper_call(env, insn->imm, &env->insn_idx,
Quoted 8 lines

kernel/bpf/verifier.c static int do_check(struct bpf_verifier_env *env) · line 9854

Quoted 1 line
kernel/bpf/verifier.c +1 -0
+								map_elem_callback);

then hopefully this extra 'map_elem_callback' boolean won't be needed. Only env->insn_idx into &env->insn_idx.
In that sense check_helper_call will become a superset of check_func_call. Maybe some code between them can be shared too.
Beyond bpf_for_each_map_elem() helper other helpers might use PTR_TO_FUNC. I hope with this approach all of them will be handled a bit more generically.

We shouldn't use this since we have insn->imm as the helper id. Will remove it.

Quoted 79 lines
Quoted 79 lines
kernel/bpf/verifier.c +30 -4
  				if (err)
  					return err;
-
  			} else if (opcode == BPF_JA) {
  				if (BPF_SRC(insn->code) != BPF_K ||
  				    insn->imm != 0 ||
@@ -10090,6 +10275,12 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env)
  				goto next_insn;
  			}
 
+			if (insn[0].src_reg == BPF_PSEUDO_FUNC) {
+				aux = &env->insn_aux_data[i];
+				aux->ptr_type = PTR_TO_FUNC;
+				goto next_insn;
+			}
+
  			/* In final convert_pseudo_ld_imm64() step, this is
  			 * converted into regular 64-bit imm load insn.
  			 */
@@ -10222,9 +10413,13 @@ static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
  	int insn_cnt = env->prog->len;
  	int i;
 
-	for (i = 0; i < insn_cnt; i++, insn++)
-		if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
-			insn->src_reg = 0;
+	for (i = 0; i < insn_cnt; i++, insn++) {
+		if (insn->code != (BPF_LD | BPF_IMM | BPF_DW))
+			continue;
+		if (insn->src_reg == BPF_PSEUDO_FUNC)
+			continue;
+		insn->src_reg = 0;
+	}
  }
 
  /* single env->prog->insni[off] instruction was replaced with the range
@@ -10846,6 +11041,12 @@ static int jit_subprogs(struct bpf_verifier_env *env)
  		return 0;
 
  	for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
+		if (bpf_pseudo_func(insn)) {
+			env->insn_aux_data[i].call_imm = insn->imm;
+			/* subprog is encoded in insn[1].imm */
+			continue;
+		}
+
  		if (!bpf_pseudo_call(insn))
  			continue;
  		/* Upon error here we cannot fall back to interpreter but
@@ -10975,6 +11176,12 @@ static int jit_subprogs(struct bpf_verifier_env *env)
  	for (i = 0; i < env->subprog_cnt; i++) {
  		insn = func[i]->insnsi;
  		for (j = 0; j < func[i]->len; j++, insn++) {
+			if (bpf_pseudo_func(insn)) {
+				subprog = insn[1].imm;
+				insn[0].imm = (u32)(long)func[subprog]->bpf_func;
+				insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32;
+				continue;
+			}
  			if (!bpf_pseudo_call(insn))
  				continue;
  			subprog = insn->off;
@@ -11020,6 +11227,11 @@ static int jit_subprogs(struct bpf_verifier_env *env)
  	 * later look the same as if they were interpreted only.
  	 */
  	for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
+		if (bpf_pseudo_func(insn)) {
+			insn[0].imm = env->insn_aux_data[i].call_imm;
+			insn[1].imm = find_subprog(env, i + insn[0].imm + 1);
+			continue;
+		}
  		if (!bpf_pseudo_call(insn))
  			continue;
  		insn->off = env->insn_aux_data[i].call_imm;
@@ -11083,6 +11295,13 @@ static int fixup_call_args(struct bpf_verifier_env *env)
  		verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n");
  		return -EINVAL;
  	}
Quoted 8 lines

kernel/bpf/verifier.c static int fixup_call_args(struct bpf_verifier_env *env) · line 11298

Quoted 1 line
kernel/bpf/verifier.c +1 -0
+	if (env->subprog_cnt > 1 && env->prog->aux->with_callback_fn) {

Does this bool really need to be be part of 'aux'?
There is a loop below that does if (!bpf_pseudo_call
to fixup insns for the interpreter.
May be add if (bpf_pseudo_func()) { return callbacks are not allowed in non-JITed } to the loop below as well?
It's a trade off between memory and few extra insn.

I use this bit similar to tailcall. But agree with you that we can the check in below loop.

Quoted 15 lines

kernel/bpf/verifier.c static int fixup_call_args(struct bpf_verifier_env *env) · line 11307

Quoted 10 lines
kernel/bpf/verifier.c +6 -0
+		/* When JIT fails the progs with callback calls
+		 * have to be rejected, since interpreter doesn't support them yet.
+		 */
+		verbose(env, "callbacks are not allowed in non-JITed programs\n");
+		return -EINVAL;
+	}
  	for (i = 0; i < prog->len; i++, insn++) {
  		if (!bpf_pseudo_call(insn))
  			continue;

to this loop.

Thanks!

#6 lore raw
In reply to Yonghong Song
Quoted 32 lines · Yonghong Song

The bpf_for_each_map_elem() helper is introduced which iterates all map elements with a callback function. The helper signature looks like
long bpf_for_each_map_elem(map, callback_fn, callback_ctx, flags)
and for each map element, the callback_fn will be called. For example, like hashmap, the callback signature may look like
long callback_fn(map, key, val, callback_ctx)

There are two known use cases for this. One is from upstream ([1]) where a for_each_map_elem helper may help implement a timeout mechanism in a more generic way. Another is from our internal discussion for a firewall use case where a map contains all the rules. The packet data can be compared to all these rules to decide allow or deny the packet.

For array maps, users can already use a bounded loop to traverse elements. Using this helper can avoid using bounded loop. For other type of maps (e.g., hash maps) where bounded loop is hard or impossible to use, this helper provides a convenient way to operate on all elements.

For callback_fn, besides map and map element, a callback_ctx, allocated on caller stack, is also passed to the callback function. This callback_ctx argument can provide additional input and allow to write to caller stack for output.

If the callback_fn returns 0, the helper will iterate through next element if available. If the callback_fn returns 1, the helper will stop iterating and returns to the bpf program. Other return values are not used for now.

Quoted 8 lines

Currently, this helper is only available with jit. It is possible to make it work with interpreter with so effort but I leave it as the future work.

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

---

This is a great feature! Few questions and nits below.

Quoted 10 lines
 include/linux/bpf.h            |  14 ++
 include/linux/bpf_verifier.h   |   3 +
 include/uapi/linux/bpf.h       |  28 ++++
 kernel/bpf/bpf_iter.c          |  16 +++
 kernel/bpf/helpers.c           |   2 +
 kernel/bpf/verifier.c          | 251 ++++++++++++++++++++++++++++++---
 kernel/trace/bpf_trace.c       |   2 +
 tools/include/uapi/linux/bpf.h |  28 ++++
 8 files changed, 328 insertions(+), 16 deletions(-)

[...]

include/linux/bpf_verifier.h struct bpf_reg_state { · line 72

Quoted 12 lines

const struct bpf_func_proto *bpf_tracing_func_proto(

        enum bpf_func_id func_id, const struct bpf_prog *prog);
include/linux/bpf_verifier.h +2 -0
index dfe6f85d97dd..c4366b3da342 100644
@@ -68,6 +68,8 @@ struct bpf_reg_state {
                        unsigned long raw1;
                        unsigned long raw2;
                } raw;
+
+               u32 subprog; /* for PTR_TO_FUNC */

is it offset to subprog (in bytes or instructions?) or it's subprog index? Let's make it clear with a better name or at least a comment.

Quoted 32 lines
include/linux/bpf_verifier.h +1 -0
        };
        /* For PTR_TO_PACKET, used to find other pointers with the same variable
         * offset, so they can share range knowledge.
@@ -204,6 +206,7 @@ struct bpf_func_state {
        int acquired_refs;
        struct bpf_reference_state *refs;
        int allocated_stack;
+       bool with_callback_fn;
        struct bpf_stack_state *stack;
 };
 
include/uapi/linux/bpf.h +9 -0
index c001766adcbc..d55bd4557376 100644
@@ -393,6 +393,15 @@ enum bpf_link_type {
  *                   is struct/union.
  */
 #define BPF_PSEUDO_BTF_ID      3
+/* insn[0].src_reg:  BPF_PSEUDO_FUNC
+ * insn[0].imm:      insn offset to the func
+ * insn[1].imm:      0
+ * insn[0].off:      0
+ * insn[1].off:      0
+ * ldimm64 rewrite:  address of the function
+ * verifier type:    PTR_TO_FUNC.
+ */
+#define BPF_PSEUDO_FUNC                4
 
 /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative
  * offset to another bpf function

include/uapi/linux/bpf.h union bpf_attr { · line 3849

Quoted 5 lines
include/uapi/linux/bpf.h +2 -0
@@ -3836,6 +3845,24 @@ union bpf_attr {
  *     Return
  *             A pointer to a struct socket on success or NULL if the file is
  *             not a socket.
+ *
+ * long bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, void *callback_ctx, u64 flags)

struct bpf_map * here might be problematic. In other instances where we pass map (bpf_map_update_elem, for example) we specify this as (void *). Let's do that instead here?

Quoted 6 lines
include/uapi/linux/bpf.h +6 -0
+ *     Description
+ *             For each element in **map**, call **callback_fn** function with
+ *             **map**, **callback_ctx** and other map-specific parameters.
+ *             For example, for hash and array maps, the callback signature can
+ *             be `u64 callback_fn(map, map_key, map_value, callback_ctx)`.
+ *             The **callback_fn** should be a static function and

include/uapi/linux/bpf.h union bpf_attr { · line 3865

Quoted 10 lines
include/uapi/linux/bpf.h +10 -0
+ *             the **callback_ctx** should be a pointer to the stack.
+ *             The **flags** is used to control certain aspects of the helper.
+ *             Currently, the **flags** must be 0.
+ *
+ *             If **callback_fn** return 0, the helper will continue to the next
+ *             element. If return value is 1, the helper will skip the rest of
+ *             elements and return. Other return values are not used now.
+ *     Return
+ *             0 for success, **-EINVAL** for invalid **flags** or unsupported
+ *             **callback_fn** return value.

just a thought: returning the number of elements *actually* iterated seems useful (even though I don't have a specific use case right now).

include/uapi/linux/bpf.h union bpf_attr { · line 4031

Quoted 8 lines
include/uapi/linux/bpf.h +1 -0
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -4001,6 +4028,7 @@ union bpf_attr {
        FN(ktime_get_coarse_ns),        \
        FN(ima_inode_hash),             \
        FN(sock_from_file),             \
+       FN(for_each_map_elem),          \

to be more in sync with other map operations, can we call this bpf_map_for_each_elem? I think it makes sense and doesn't read backwards at all.

Quoted 15 lines
include/uapi/linux/bpf.h +0 -0
        /* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/bpf_iter.c +4 -0
index 5454161407f1..5187f49d3216 100644
@@ -675,3 +675,19 @@ int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx)
         */
        return ret == 0 ? 0 : -EAGAIN;
 }
+
+BPF_CALL_4(bpf_for_each_map_elem, struct bpf_map *, map, void *, callback_fn,
+          void *, callback_ctx, u64, flags)
+{

kernel/bpf/bpf_iter.c int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx) · line 691

Quoted 10 lines
kernel/bpf/bpf_iter.c +10 -0
+       return map->ops->map_for_each_callback(map, callback_fn, callback_ctx, flags);
+}
+
+const struct bpf_func_proto bpf_for_each_map_elem_proto = {
+       .func           = bpf_for_each_map_elem,
+       .gpl_only       = false,
+       .ret_type       = RET_INTEGER,
+       .arg1_type      = ARG_CONST_MAP_PTR,
+       .arg2_type      = ARG_PTR_TO_FUNC,
+       .arg3_type      = ARG_PTR_TO_STACK_OR_NULL,

I looked through this code just once but haven't noticed anything that would strictly require that pointer is specifically to stack. Can this be made into a pointer to any allocated memory? E.g., why can't we allow passing a pointer to a ringbuf sample, for instance? Or MAP_VALUE?

Quoted 23 lines
kernel/bpf/bpf_iter.c +2 -0
+       .arg4_type      = ARG_ANYTHING,
+};
kernel/bpf/helpers.c +2 -0
index 308427fe03a3..074800226327 100644
@@ -708,6 +708,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
                return &bpf_ringbuf_discard_proto;
        case BPF_FUNC_ringbuf_query:
                return &bpf_ringbuf_query_proto;
+       case BPF_FUNC_for_each_map_elem:
+               return &bpf_for_each_map_elem_proto;
        default:
                break;
        }
kernel/bpf/verifier.c +0 -0
index db294b75d03b..050b067a0be6 100644
@@ -234,6 +234,12 @@ static bool bpf_pseudo_call(const struct bpf_insn *insn)
               insn->src_reg == BPF_PSEUDO_CALL;
 }
 

[...]

kernel/bpf/verifier.c static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) · line 8368

Quoted 13 lines
        map = env->used_maps[aux->map_index];
        mark_reg_known_zero(env, regs, insn->dst_reg);
        dst_reg->map_ptr = map;
kernel/bpf/verifier.c +3 -1
@@ -8195,9 +8361,23 @@ static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env)
 
        /* All non-branch instructions have a single fall-through edge. */
        if (BPF_CLASS(insns[t].code) != BPF_JMP &&
-           BPF_CLASS(insns[t].code) != BPF_JMP32)
+           BPF_CLASS(insns[t].code) != BPF_JMP32 &&
+           !bpf_pseudo_func(insns + t))
                return push_insn(t, t + 1, FALLTHROUGH, env, false);
 
+       if (bpf_pseudo_func(insns + t)) {

if you check this before above JMP|JMP32 check, you won't need to do !bpf_pseudo_func, right? I think it's cleaner.

Quoted 15 lines
kernel/bpf/verifier.c +12 -0
+               ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
+               if (ret)
+                       return ret;
+
+               if (t + 1 < insn_cnt)
+                       init_explored_state(env, t + 1);
+               init_explored_state(env, t);
+               ret = push_insn(t, t + insns[t].imm + 1, BRANCH,
+                               env, false);
+               return ret;
+       }
+
        switch (BPF_OP(insns[t].code)) {
        case BPF_EXIT:
                return DONE_EXPLORING;

[...]

#7 lore raw
Quoted 57 lines · Andrii Nakryiko
Quoted 32 lines · Yonghong Song

The bpf_for_each_map_elem() helper is introduced which iterates all map elements with a callback function. The helper signature looks like
long bpf_for_each_map_elem(map, callback_fn, callback_ctx, flags)
and for each map element, the callback_fn will be called. For example, like hashmap, the callback signature may look like
long callback_fn(map, key, val, callback_ctx)

There are two known use cases for this. One is from upstream ([1]) where a for_each_map_elem helper may help implement a timeout mechanism in a more generic way. Another is from our internal discussion for a firewall use case where a map contains all the rules. The packet data can be compared to all these rules to decide allow or deny the packet.

For array maps, users can already use a bounded loop to traverse elements. Using this helper can avoid using bounded loop. For other type of maps (e.g., hash maps) where bounded loop is hard or impossible to use, this helper provides a convenient way to operate on all elements.

For callback_fn, besides map and map element, a callback_ctx, allocated on caller stack, is also passed to the callback function. This callback_ctx argument can provide additional input and allow to write to caller stack for output.

If the callback_fn returns 0, the helper will iterate through next element if available. If the callback_fn returns 1, the helper will stop iterating and returns to the bpf program. Other return values are not used for now.

Quoted 8 lines

Currently, this helper is only available with jit. It is possible to make it work with interpreter with so effort but I leave it as the future work.

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

---

This is a great feature! Few questions and nits below.

Quoted 10 lines
  include/linux/bpf.h            |  14 ++
  include/linux/bpf_verifier.h   |   3 +
  include/uapi/linux/bpf.h       |  28 ++++
  kernel/bpf/bpf_iter.c          |  16 +++
  kernel/bpf/helpers.c           |   2 +
  kernel/bpf/verifier.c          | 251 ++++++++++++++++++++++++++++++---
  kernel/trace/bpf_trace.c       |   2 +
  tools/include/uapi/linux/bpf.h |  28 ++++
  8 files changed, 328 insertions(+), 16 deletions(-)

[...]

Quoted 15 lines

include/linux/bpf_verifier.h struct bpf_reg_state { · line 72

Quoted 12 lines

const struct bpf_func_proto *bpf_tracing_func_proto(

         enum bpf_func_id func_id, const struct bpf_prog *prog);
include/linux/bpf_verifier.h +2 -0
index dfe6f85d97dd..c4366b3da342 100644
@@ -68,6 +68,8 @@ struct bpf_reg_state {
                         unsigned long raw1;
                         unsigned long raw2;
                 } raw;
+
+               u32 subprog; /* for PTR_TO_FUNC */

is it offset to subprog (in bytes or instructions?) or it's subprog index? Let's make it clear with a better name or at least a comment.

This is for subprog number (or index in some subprog related arrays). In verifier.c, subprog or subprogno is used to represent the subprog number. I will use subprogno in the next revision.

Quoted 30 lines
Quoted 30 lines
include/linux/bpf_verifier.h +1 -0
         };
         /* For PTR_TO_PACKET, used to find other pointers with the same variable
          * offset, so they can share range knowledge.
@@ -204,6 +206,7 @@ struct bpf_func_state {
         int acquired_refs;
         struct bpf_reference_state *refs;
         int allocated_stack;
+       bool with_callback_fn;
         struct bpf_stack_state *stack;
  };
 
include/uapi/linux/bpf.h +9 -0
index c001766adcbc..d55bd4557376 100644
@@ -393,6 +393,15 @@ enum bpf_link_type {
   *                   is struct/union.
   */
  #define BPF_PSEUDO_BTF_ID      3
+/* insn[0].src_reg:  BPF_PSEUDO_FUNC
+ * insn[0].imm:      insn offset to the func
+ * insn[1].imm:      0
+ * insn[0].off:      0
+ * insn[1].off:      0
+ * ldimm64 rewrite:  address of the function
+ * verifier type:    PTR_TO_FUNC.
+ */
+#define BPF_PSEUDO_FUNC                4
 
Quoted 12 lines

include/uapi/linux/bpf.h union bpf_attr { · line 3849

Quoted 8 lines
include/uapi/linux/bpf.h +2 -0
  /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative
   * offset to another bpf function
@@ -3836,6 +3845,24 @@ union bpf_attr {
   *     Return
   *             A pointer to a struct socket on success or NULL if the file is
   *             not a socket.
+ *
+ * long bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, void *callback_ctx, u64 flags)

struct bpf_map * here might be problematic. In other instances where we pass map (bpf_map_update_elem, for example) we specify this as (void *). Let's do that instead here?

We should be fine here. bpf_map_lookup_elem etc. all have "struct bpf_map *map", it is rewritten by bpf_helpers_doc.py to "void *map".

Quoted 18 lines
Quoted 7 lines
include/uapi/linux/bpf.h +6 -0
+ *     Description
+ *             For each element in **map**, call **callback_fn** function with
+ *             **map**, **callback_ctx** and other map-specific parameters.
+ *             For example, for hash and array maps, the callback signature can
+ *             be `u64 callback_fn(map, map_key, map_value, callback_ctx)`.
+ *             The **callback_fn** should be a static function and

include/uapi/linux/bpf.h union bpf_attr { · line 3865

Quoted 10 lines
include/uapi/linux/bpf.h +10 -0
+ *             the **callback_ctx** should be a pointer to the stack.
+ *             The **flags** is used to control certain aspects of the helper.
+ *             Currently, the **flags** must be 0.
+ *
+ *             If **callback_fn** return 0, the helper will continue to the next
+ *             element. If return value is 1, the helper will skip the rest of
+ *             elements and return. Other return values are not used now.
+ *     Return
+ *             0 for success, **-EINVAL** for invalid **flags** or unsupported
+ *             **callback_fn** return value.
Quoted 2 lines

just a thought: returning the number of elements *actually* iterated seems useful (even though I don't have a specific use case right now).

Good idea. Will change to this in the next revision.

Quoted 13 lines

include/uapi/linux/bpf.h union bpf_attr { · line 4031

Quoted 9 lines
include/uapi/linux/bpf.h +1 -0
   */
  #define __BPF_FUNC_MAPPER(FN)          \
         FN(unspec),                     \
@@ -4001,6 +4028,7 @@ union bpf_attr {
         FN(ktime_get_coarse_ns),        \
         FN(ima_inode_hash),             \
         FN(sock_from_file),             \
+       FN(for_each_map_elem),          \

to be more in sync with other map operations, can we call this bpf_map_for_each_elem? I think it makes sense and doesn't read backwards at all.

I am using for_each prefix as in the future I (or others) may add more for_each_* helpers, e.g., for_each_task, for_each_hlist_rcu, etc. This represents a family of helpers with callback functions. So I would like to stick with for_each_* names.

Quoted 24 lines
Quoted 24 lines
include/uapi/linux/bpf.h +0 -0
         /* */
 
  /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/bpf_iter.c +12 -0
index 5454161407f1..5187f49d3216 100644
@@ -675,3 +675,19 @@ int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx)
          */
         return ret == 0 ? 0 : -EAGAIN;
  }
+
+BPF_CALL_4(bpf_for_each_map_elem, struct bpf_map *, map, void *, callback_fn,
+          void *, callback_ctx, u64, flags)
+{
+       return map->ops->map_for_each_callback(map, callback_fn, callback_ctx, flags);
+}
+
+const struct bpf_func_proto bpf_for_each_map_elem_proto = {
+       .func           = bpf_for_each_map_elem,
+       .gpl_only       = false,
+       .ret_type       = RET_INTEGER,
+       .arg1_type      = ARG_CONST_MAP_PTR,
Quoted 8 lines

kernel/bpf/bpf_iter.c int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx) · line 691

Quoted 2 lines
kernel/bpf/bpf_iter.c +2 -0
+       .arg2_type      = ARG_PTR_TO_FUNC,
+       .arg3_type      = ARG_PTR_TO_STACK_OR_NULL,

I looked through this code just once but haven't noticed anything that would strictly require that pointer is specifically to stack. Can this be made into a pointer to any allocated memory? E.g., why can't we allow passing a pointer to a ringbuf sample, for instance? Or MAP_VALUE?

ARG_PTR_TO_STACK_OR_NULL in the most flexible one. For example, if you want to pass map_value or ringbuf sample, you can assign these values to the stack like

   struct ctx_t {
      struct map_value_t *map_value;
      char *ringbuf_mem;

} tmp;
tmp.map_value = ...;
tmp.ringbuf_mem = ...; bpf_for_each_map_elem(map, callback_fn, &tmp, flags);
and callback_fn will be able to access map_value/ringbuf_mem with their original register types.

This does not allow to pass ringbuf/map_value etc. as the first class citizen. But I think this is a good compromise to permit greater flexibility.

Quoted 39 lines
Quoted 24 lines
kernel/bpf/bpf_iter.c +2 -0
+       .arg4_type      = ARG_ANYTHING,
+};
kernel/bpf/helpers.c +2 -0
index 308427fe03a3..074800226327 100644
@@ -708,6 +708,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
                 return &bpf_ringbuf_discard_proto;
         case BPF_FUNC_ringbuf_query:
                 return &bpf_ringbuf_query_proto;
+       case BPF_FUNC_for_each_map_elem:
+               return &bpf_for_each_map_elem_proto;
         default:
                 break;
         }
kernel/bpf/verifier.c +0 -0
index db294b75d03b..050b067a0be6 100644
@@ -234,6 +234,12 @@ static bool bpf_pseudo_call(const struct bpf_insn *insn)
                insn->src_reg == BPF_PSEUDO_CALL;
  }
 

[...]

Quoted 12 lines
         map = env->used_maps[aux->map_index];
         mark_reg_known_zero(env, regs, insn->dst_reg);
         dst_reg->map_ptr = map;
kernel/bpf/verifier.c +2 -1
@@ -8195,9 +8361,23 @@ static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env)
 
         /* All non-branch instructions have a single fall-through edge. */
         if (BPF_CLASS(insns[t].code) != BPF_JMP &&
-           BPF_CLASS(insns[t].code) != BPF_JMP32)
+           BPF_CLASS(insns[t].code) != BPF_JMP32 &&
+           !bpf_pseudo_func(insns + t))
                 return push_insn(t, t + 1, FALLTHROUGH, env, false);
 
Quoted 5 lines

kernel/bpf/verifier.c static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) · line 8368

Quoted 1 line
kernel/bpf/verifier.c +1 -0
+       if (bpf_pseudo_func(insns + t)) {

if you check this before above JMP|JMP32 check, you won't need to do !bpf_pseudo_func, right? I think it's cleaner.

Agree. will change in v2.

Quoted 19 lines
Quoted 16 lines
kernel/bpf/verifier.c +12 -0
+               ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
+               if (ret)
+                       return ret;
+
+               if (t + 1 < insn_cnt)
+                       init_explored_state(env, t + 1);
+               init_explored_state(env, t);
+               ret = push_insn(t, t + insns[t].imm + 1, BRANCH,
+                               env, false);
+               return ret;
+       }
+
         switch (BPF_OP(insns[t].code)) {
         case BPF_EXIT:
                 return DONE_EXPLORING;

[...]

#8 lore raw
Quoted 77 lines · Yonghong Song
Quoted 57 lines · Andrii Nakryiko
Quoted 32 lines · Yonghong Song

The bpf_for_each_map_elem() helper is introduced which iterates all map elements with a callback function. The helper signature looks like
long bpf_for_each_map_elem(map, callback_fn, callback_ctx, flags)
and for each map element, the callback_fn will be called. For example, like hashmap, the callback signature may look like
long callback_fn(map, key, val, callback_ctx)

There are two known use cases for this. One is from upstream ([1]) where a for_each_map_elem helper may help implement a timeout mechanism in a more generic way. Another is from our internal discussion for a firewall use case where a map contains all the rules. The packet data can be compared to all these rules to decide allow or deny the packet.

For array maps, users can already use a bounded loop to traverse elements. Using this helper can avoid using bounded loop. For other type of maps (e.g., hash maps) where bounded loop is hard or impossible to use, this helper provides a convenient way to operate on all elements.

For callback_fn, besides map and map element, a callback_ctx, allocated on caller stack, is also passed to the callback function. This callback_ctx argument can provide additional input and allow to write to caller stack for output.

If the callback_fn returns 0, the helper will iterate through next element if available. If the callback_fn returns 1, the helper will stop iterating and returns to the bpf program. Other return values are not used for now.

Quoted 8 lines

Currently, this helper is only available with jit. It is possible to make it work with interpreter with so effort but I leave it as the future work.

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

---

This is a great feature! Few questions and nits below.

Quoted 10 lines
  include/linux/bpf.h            |  14 ++
  include/linux/bpf_verifier.h   |   3 +
  include/uapi/linux/bpf.h       |  28 ++++
  kernel/bpf/bpf_iter.c          |  16 +++
  kernel/bpf/helpers.c           |   2 +
  kernel/bpf/verifier.c          | 251 ++++++++++++++++++++++++++++++---
  kernel/trace/bpf_trace.c       |   2 +
  tools/include/uapi/linux/bpf.h |  28 ++++
  8 files changed, 328 insertions(+), 16 deletions(-)

[...]

Quoted 15 lines

include/linux/bpf_verifier.h struct bpf_reg_state { · line 72

Quoted 12 lines

const struct bpf_func_proto *bpf_tracing_func_proto(

         enum bpf_func_id func_id, const struct bpf_prog *prog);
include/linux/bpf_verifier.h +2 -0
index dfe6f85d97dd..c4366b3da342 100644
@@ -68,6 +68,8 @@ struct bpf_reg_state {
                         unsigned long raw1;
                         unsigned long raw2;
                 } raw;
+
+               u32 subprog; /* for PTR_TO_FUNC */

is it offset to subprog (in bytes or instructions?) or it's subprog index? Let's make it clear with a better name or at least a comment.

Quoted 4 lines

This is for subprog number (or index in some subprog related arrays). In verifier.c, subprog or subprogno is used to represent the subprog number. I will use subprogno in the next revision.

yeah, that's more clear

Quoted 43 lines
Quoted 30 lines
Quoted 30 lines
include/linux/bpf_verifier.h +1 -0
         };
         /* For PTR_TO_PACKET, used to find other pointers with the same variable
          * offset, so they can share range knowledge.
@@ -204,6 +206,7 @@ struct bpf_func_state {
         int acquired_refs;
         struct bpf_reference_state *refs;
         int allocated_stack;
+       bool with_callback_fn;
         struct bpf_stack_state *stack;
  };
 
include/uapi/linux/bpf.h +9 -0
index c001766adcbc..d55bd4557376 100644
@@ -393,6 +393,15 @@ enum bpf_link_type {
   *                   is struct/union.
   */
  #define BPF_PSEUDO_BTF_ID      3
+/* insn[0].src_reg:  BPF_PSEUDO_FUNC
+ * insn[0].imm:      insn offset to the func
+ * insn[1].imm:      0
+ * insn[0].off:      0
+ * insn[1].off:      0
+ * ldimm64 rewrite:  address of the function
+ * verifier type:    PTR_TO_FUNC.
+ */
+#define BPF_PSEUDO_FUNC                4
 
Quoted 12 lines

include/uapi/linux/bpf.h union bpf_attr { · line 3849

Quoted 8 lines
include/uapi/linux/bpf.h +2 -0
  /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative
   * offset to another bpf function
@@ -3836,6 +3845,24 @@ union bpf_attr {
   *     Return
   *             A pointer to a struct socket on success or NULL if the file is
   *             not a socket.
+ *
+ * long bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, void *callback_ctx, u64 flags)

struct bpf_map * here might be problematic. In other instances where we pass map (bpf_map_update_elem, for example) we specify this as (void *). Let's do that instead here?

Quoted 2 lines

We should be fine here. bpf_map_lookup_elem etc. all have "struct bpf_map *map", it is rewritten by bpf_helpers_doc.py to "void *map".

ok, cool

Quoted 38 lines
Quoted 19 lines
Quoted 8 lines
include/uapi/linux/bpf.h +6 -0
+ *     Description
+ *             For each element in **map**, call **callback_fn** function with
+ *             **map**, **callback_ctx** and other map-specific parameters.
+ *             For example, for hash and array maps, the callback signature can
+ *             be `u64 callback_fn(map, map_key, map_value, callback_ctx)`.
+ *             The **callback_fn** should be a static function and

include/uapi/linux/bpf.h union bpf_attr { · line 3865

Quoted 10 lines
include/uapi/linux/bpf.h +10 -0
+ *             the **callback_ctx** should be a pointer to the stack.
+ *             The **flags** is used to control certain aspects of the helper.
+ *             Currently, the **flags** must be 0.
+ *
+ *             If **callback_fn** return 0, the helper will continue to the next
+ *             element. If return value is 1, the helper will skip the rest of
+ *             elements and return. Other return values are not used now.
+ *     Return
+ *             0 for success, **-EINVAL** for invalid **flags** or unsupported
+ *             **callback_fn** return value.
Quoted 2 lines

just a thought: returning the number of elements *actually* iterated seems useful (even though I don't have a specific use case right now).

Good idea. Will change to this in the next revision.

Quoted 13 lines

include/uapi/linux/bpf.h union bpf_attr { · line 4031

Quoted 9 lines
include/uapi/linux/bpf.h +1 -0
   */
  #define __BPF_FUNC_MAPPER(FN)          \
         FN(unspec),                     \
@@ -4001,6 +4028,7 @@ union bpf_attr {
         FN(ktime_get_coarse_ns),        \
         FN(ima_inode_hash),             \
         FN(sock_from_file),             \
+       FN(for_each_map_elem),          \

to be more in sync with other map operations, can we call this bpf_map_for_each_elem? I think it makes sense and doesn't read backwards at all.

Quoted 5 lines

I am using for_each prefix as in the future I (or others) may add more for_each_* helpers, e.g., for_each_task, for_each_hlist_rcu, etc. This represents a family of helpers with callback functions. So I would like to stick with for_each_* names.

fair enough, not a big deal

Quoted 43 lines
Quoted 24 lines
Quoted 24 lines
include/uapi/linux/bpf.h +0 -0
         /* */
 
  /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/bpf_iter.c +12 -0
index 5454161407f1..5187f49d3216 100644
@@ -675,3 +675,19 @@ int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx)
          */
         return ret == 0 ? 0 : -EAGAIN;
  }
+
+BPF_CALL_4(bpf_for_each_map_elem, struct bpf_map *, map, void *, callback_fn,
+          void *, callback_ctx, u64, flags)
+{
+       return map->ops->map_for_each_callback(map, callback_fn, callback_ctx, flags);
+}
+
+const struct bpf_func_proto bpf_for_each_map_elem_proto = {
+       .func           = bpf_for_each_map_elem,
+       .gpl_only       = false,
+       .ret_type       = RET_INTEGER,
+       .arg1_type      = ARG_CONST_MAP_PTR,
Quoted 8 lines

kernel/bpf/bpf_iter.c int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx) · line 691

Quoted 2 lines
kernel/bpf/bpf_iter.c +2 -0
+       .arg2_type      = ARG_PTR_TO_FUNC,
+       .arg3_type      = ARG_PTR_TO_STACK_OR_NULL,

I looked through this code just once but haven't noticed anything that would strictly require that pointer is specifically to stack. Can this be made into a pointer to any allocated memory? E.g., why can't we allow passing a pointer to a ringbuf sample, for instance? Or MAP_VALUE?

ARG_PTR_TO_STACK_OR_NULL in the most flexible one. For example, if you want to pass map_value or ringbuf sample, you can assign these values to the stack like

    struct ctx_t {
       struct map_value_t *map_value;
       char *ringbuf_mem;
    } tmp;
    tmp.map_value = ...;
    tmp.ringbuf_mem = ...;
    bpf_for_each_map_elem(map, callback_fn, &tmp, flags);
Quoted 6 lines

and callback_fn will be able to access map_value/ringbuf_mem with their original register types.

This does not allow to pass ringbuf/map_value etc. as the first class citizen. But I think this is a good compromise to permit greater flexibility.

Yeah, thanks for the explanation.

Quoted 67 lines
Quoted 40 lines
Quoted 25 lines
kernel/bpf/bpf_iter.c +2 -0
+       .arg4_type      = ARG_ANYTHING,
+};
kernel/bpf/helpers.c +2 -0
index 308427fe03a3..074800226327 100644
@@ -708,6 +708,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
                 return &bpf_ringbuf_discard_proto;
         case BPF_FUNC_ringbuf_query:
                 return &bpf_ringbuf_query_proto;
+       case BPF_FUNC_for_each_map_elem:
+               return &bpf_for_each_map_elem_proto;
         default:
                 break;
         }
kernel/bpf/verifier.c +0 -0
index db294b75d03b..050b067a0be6 100644
@@ -234,6 +234,12 @@ static bool bpf_pseudo_call(const struct bpf_insn *insn)
                insn->src_reg == BPF_PSEUDO_CALL;
  }
 

[...]

Quoted 12 lines
         map = env->used_maps[aux->map_index];
         mark_reg_known_zero(env, regs, insn->dst_reg);
         dst_reg->map_ptr = map;
kernel/bpf/verifier.c +2 -1
@@ -8195,9 +8361,23 @@ static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env)
 
         /* All non-branch instructions have a single fall-through edge. */
         if (BPF_CLASS(insns[t].code) != BPF_JMP &&
-           BPF_CLASS(insns[t].code) != BPF_JMP32)
+           BPF_CLASS(insns[t].code) != BPF_JMP32 &&
+           !bpf_pseudo_func(insns + t))
                 return push_insn(t, t + 1, FALLTHROUGH, env, false);
 
Quoted 5 lines

kernel/bpf/verifier.c static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) · line 8368

Quoted 1 line
kernel/bpf/verifier.c +1 -0
+       if (bpf_pseudo_func(insns + t)) {

if you check this before above JMP|JMP32 check, you won't need to do !bpf_pseudo_func, right? I think it's cleaner.

Agree. will change in v2.

Quoted 19 lines
Quoted 16 lines
kernel/bpf/verifier.c +12 -0
+               ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
+               if (ret)
+                       return ret;
+
+               if (t + 1 < insn_cnt)
+                       init_explored_state(env, t + 1);
+               init_explored_state(env, t);
+               ret = push_insn(t, t + insns[t].imm + 1, BRANCH,
+                               env, false);
+               return ret;
+       }
+
         switch (BPF_OP(insns[t].code)) {
         case BPF_EXIT:
                 return DONE_EXPLORING;

[...]

#9 lore raw

PATCH 3/8 bpf: add hashtab support for bpf_for_each_map_elem() helper

In reply to Yonghong Song

This patch added support for hashmap, percpu hashmap, lru hashmap and percpu lru hashmap.

---
 include/linux/bpf.h   |  4 +++
 kernel/bpf/hashtab.c  | 57 +++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c | 23 +++++++++++++++++
 3 files changed, 84 insertions(+)
include/linux/bpf.h +4 -0
index c8b72ae16cc5..31e0447cadd8 100644
@@ -1396,6 +1396,10 @@ void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
 				struct bpf_link_info *info);
 
+int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_func_state *caller,
+				   struct bpf_func_state *callee);
+
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
kernel/bpf/hashtab.c +57 -0

87 lines not on this page · raw message

kernel/bpf/verifier.c +23 -0
index 050b067a0be6..32c8dcc27da8 100644
@@ -4987,6 +4987,29 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	return 0;
 }
 
+int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_func_state *caller,
+				   struct bpf_func_state *callee)
+{
+	/* pointer to map */
+	callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1];
+
+	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
+	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
+	callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr;
+
+	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
+	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
+	callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr;
+
+	/* pointer to stack or null */
+	callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3];
+
+	/* unused */
+	__mark_reg_unknown(env, &callee->regs[BPF_REG_5]);
+	return 0;
+}
+
 static int check_map_elem_callback(struct bpf_verifier_env *env, int *insn_idx)
 {
 	struct bpf_verifier_state *state = env->cur_state;
Signature · 1 line
2.24.1
#10 lore raw

Re: [PATCH bpf-next 3/8] bpf: add hashtab support for bpf_for_each_map_elem() helper

Quoted 63 lines · Yonghong Song

This patch added support for hashmap, percpu hashmap, lru hashmap and percpu lru hashmap.

---
 include/linux/bpf.h   |  4 +++
 kernel/bpf/hashtab.c  | 57 +++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c | 23 +++++++++++++++++
 3 files changed, 84 insertions(+)
include/linux/bpf.h +4 -0
index c8b72ae16cc5..31e0447cadd8 100644
@@ -1396,6 +1396,10 @@ void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
 				struct bpf_link_info *info);
 
+int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_func_state *caller,
+				   struct bpf_func_state *callee);
+
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
kernel/bpf/hashtab.c +30 -0
index c1ac7f964bc9..40f5404cfb01 100644
@@ -1869,6 +1869,55 @@ static const struct bpf_iter_seq_info iter_seq_info = {
 	.seq_priv_size		= sizeof(struct bpf_iter_seq_hash_map_info),
 };
 
+static int bpf_for_each_hash_elem(struct bpf_map *map, void *callback_fn,
+				  void *callback_ctx, u64 flags)
+{
+	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
+	struct hlist_nulls_head *head;
+	struct hlist_nulls_node *n;
+	struct htab_elem *elem;
+	u32 roundup_key_size;
+	void __percpu *pptr;
+	struct bucket *b;
+	void *key, *val;
+	bool is_percpu;
+	long ret = 0;
+	int i;
+
+	if (flags != 0)
+		return -EINVAL;
+
+	is_percpu = htab_is_percpu(htab);
+
+	roundup_key_size = round_up(map->key_size, 8);
+	for (i = 0; i < htab->n_buckets; i++) {
+		b = &htab->buckets[i];
+		rcu_read_lock();
+		head = &b->head;
+		hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) {
+			key = elem->key;
+			if (!is_percpu) {
+				val = elem->key + roundup_key_size;
+			} else {

kernel/bpf/hashtab.c static const struct bpf_iter_seq_info iter_seq_info = { · line 1911

Quoted 10 lines
kernel/bpf/hashtab.c +10 -0
+				/* current cpu value for percpu map */
+				pptr = htab_elem_get_ptr(elem, map->key_size);
+				val = this_cpu_ptr(pptr);
+			}
+			ret = BPF_CAST_CALL(callback_fn)((u64)(long)map,
+					(u64)(long)key, (u64)(long)val,
+					(u64)(long)callback_ctx, 0);
+			if (ret) {
+				rcu_read_unlock();
+				ret = (ret == 1) ? 0 : -EINVAL;

one more thing that I should have mentioned in patch 2. In prepare_func_exit would be good to add:
if (callee->in_callback_fn)
check that r0 is readable and in tnum_range(0, 1).
and then don't assign r0 reg_state anywhere.

kernel/bpf/hashtab.c static const struct bpf_iter_seq_info iter_seq_info = { · line 1915

Quoted 4 lines
kernel/bpf/hashtab.c +4 -0
+				goto out;
+			}
+		}
+		rcu_read_unlock();

Sleepable progs can do cond_resched here.
How about adding migrate_disable before for() loop
to make sure that for_each(per_cpu_map,...) is still meaningful and if (!in_atomic())
cond_resched();
here.
Since this helper is called from bpf progs only the in_atomic check (whether prog was sleepable or not) is accurate.

kernel/bpf/hashtab.c static const struct bpf_iter_seq_info iter_seq_info = { · line 1916

Quoted 1 line
kernel/bpf/hashtab.c +1 -0
+	}

migrate_enable() here after the loop.

Quoted 58 lines
kernel/bpf/hashtab.c +12 -0
+out:
+	return ret;
+}
+
 static int htab_map_btf_id;
 const struct bpf_map_ops htab_map_ops = {
 	.map_meta_equal = bpf_map_meta_equal,
@@ -1881,6 +1930,8 @@ const struct bpf_map_ops htab_map_ops = {
 	.map_delete_elem = htab_map_delete_elem,
 	.map_gen_lookup = htab_map_gen_lookup,
 	.map_seq_show_elem = htab_map_seq_show_elem,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_hash_elem,
 	BATCH_OPS(htab),
 	.map_btf_name = "bpf_htab",
 	.map_btf_id = &htab_map_btf_id,
@@ -1900,6 +1951,8 @@ const struct bpf_map_ops htab_lru_map_ops = {
 	.map_delete_elem = htab_lru_map_delete_elem,
 	.map_gen_lookup = htab_lru_map_gen_lookup,
 	.map_seq_show_elem = htab_map_seq_show_elem,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_hash_elem,
 	BATCH_OPS(htab_lru),
 	.map_btf_name = "bpf_htab",
 	.map_btf_id = &htab_lru_map_btf_id,
@@ -2019,6 +2072,8 @@ const struct bpf_map_ops htab_percpu_map_ops = {
 	.map_update_elem = htab_percpu_map_update_elem,
 	.map_delete_elem = htab_map_delete_elem,
 	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_hash_elem,
 	BATCH_OPS(htab_percpu),
 	.map_btf_name = "bpf_htab",
 	.map_btf_id = &htab_percpu_map_btf_id,
@@ -2036,6 +2091,8 @@ const struct bpf_map_ops htab_lru_percpu_map_ops = {
 	.map_update_elem = htab_lru_percpu_map_update_elem,
 	.map_delete_elem = htab_lru_map_delete_elem,
 	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_hash_elem,
 	BATCH_OPS(htab_lru_percpu),
 	.map_btf_name = "bpf_htab",
 	.map_btf_id = &htab_lru_percpu_map_btf_id,
kernel/bpf/verifier.c +7 -0
index 050b067a0be6..32c8dcc27da8 100644
@@ -4987,6 +4987,29 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	return 0;
 }
 
+int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_func_state *caller,
+				   struct bpf_func_state *callee)
+{
+	/* pointer to map */
+	callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1];
+

kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 5006

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
+	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
+	callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr;
+
+	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
+	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
+	callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr;
+
+	/* pointer to stack or null */
+	callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3];

This hard coding of regs 1 through 4 makes sense.
May be add a comment with bpf_for_each_map_elem and callback_fn prototypes, so it's more obvious what's going on.

kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 5009

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+
+	/* unused */
+	__mark_reg_unknown(env, &callee->regs[BPF_REG_5]);

I think it should be __mark_reg_not_init to make sure that callback_fn doesn't use r5.
That will help with future extensions via new flags arg that is passed into bpf_for_each_map_elem.

#11 lore raw

Re: [PATCH bpf-next 3/8] bpf: add hashtab support for bpf_for_each_map_elem() helper

Quoted 72 lines · Alexei Starovoitov
Quoted 71 lines · Yonghong Song

This patch added support for hashmap, percpu hashmap, lru hashmap and percpu lru hashmap.

---
  include/linux/bpf.h   |  4 +++
  kernel/bpf/hashtab.c  | 57 +++++++++++++++++++++++++++++++++++++++++++
  kernel/bpf/verifier.c | 23 +++++++++++++++++
  3 files changed, 84 insertions(+)
include/linux/bpf.h +4 -0
index c8b72ae16cc5..31e0447cadd8 100644
@@ -1396,6 +1396,10 @@ void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
  int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
  				struct bpf_link_info *info);
 
+int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_func_state *caller,
+				   struct bpf_func_state *callee);
+
  int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
  int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
  int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
kernel/bpf/hashtab.c +38 -0
index c1ac7f964bc9..40f5404cfb01 100644
@@ -1869,6 +1869,55 @@ static const struct bpf_iter_seq_info iter_seq_info = {
  	.seq_priv_size		= sizeof(struct bpf_iter_seq_hash_map_info),
  };
 
+static int bpf_for_each_hash_elem(struct bpf_map *map, void *callback_fn,
+				  void *callback_ctx, u64 flags)
+{
+	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
+	struct hlist_nulls_head *head;
+	struct hlist_nulls_node *n;
+	struct htab_elem *elem;
+	u32 roundup_key_size;
+	void __percpu *pptr;
+	struct bucket *b;
+	void *key, *val;
+	bool is_percpu;
+	long ret = 0;
+	int i;
+
+	if (flags != 0)
+		return -EINVAL;
+
+	is_percpu = htab_is_percpu(htab);
+
+	roundup_key_size = round_up(map->key_size, 8);
+	for (i = 0; i < htab->n_buckets; i++) {
+		b = &htab->buckets[i];
+		rcu_read_lock();
+		head = &b->head;
+		hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) {
+			key = elem->key;
+			if (!is_percpu) {
+				val = elem->key + roundup_key_size;
+			} else {
+				/* current cpu value for percpu map */
+				pptr = htab_elem_get_ptr(elem, map->key_size);
+				val = this_cpu_ptr(pptr);
+			}
+			ret = BPF_CAST_CALL(callback_fn)((u64)(long)map,
+					(u64)(long)key, (u64)(long)val,
+					(u64)(long)callback_ctx, 0);
+			if (ret) {
Quoted 8 lines

kernel/bpf/hashtab.c static const struct bpf_iter_seq_info iter_seq_info = { · line 1911

Quoted 2 lines
kernel/bpf/hashtab.c +2 -0
+				rcu_read_unlock();
+				ret = (ret == 1) ? 0 : -EINVAL;

one more thing that I should have mentioned in patch 2. In prepare_func_exit would be good to add:
if (callee->in_callback_fn)
check that r0 is readable and in tnum_range(0, 1).
and then don't assign r0 reg_state anywhere.

Yes, indeed. I added the constraint of return value 0/1 in the
last minute. My previous constraint is 0 for continue and non-0 for return. I changed it as I feel it is not extensible.

Will add contraint for r0 readable and tnum_range(0, 1) in the next revision.

Quoted 10 lines

kernel/bpf/hashtab.c static const struct bpf_iter_seq_info iter_seq_info = { · line 1915

Quoted 5 lines
kernel/bpf/hashtab.c +4 -0
+				goto out;
+			}
+		}
+		rcu_read_unlock();

Sleepable progs can do cond_resched here.
How about adding migrate_disable before for() loop
to make sure that for_each(per_cpu_map,...) is still meaningful and if (!in_atomic())

Quoted 8 lines
    cond_resched();

here.
Since this helper is called from bpf progs only the in_atomic check (whether prog was sleepable or not) is accurate.

kernel/bpf/hashtab.c static const struct bpf_iter_seq_info iter_seq_info = { · line 1916

Quoted 1 line
kernel/bpf/hashtab.c +1 -0
+	}

migrate_enable() here after the loop.

will do. I have not thought about sleepable program much yet. I suspect I may miss some additional checking somewhere.

Quoted 65 lines
Quoted 65 lines
kernel/bpf/hashtab.c +12 -0
+out:
+	return ret;
+}
+
  static int htab_map_btf_id;
  const struct bpf_map_ops htab_map_ops = {
  	.map_meta_equal = bpf_map_meta_equal,
@@ -1881,6 +1930,8 @@ const struct bpf_map_ops htab_map_ops = {
  	.map_delete_elem = htab_map_delete_elem,
  	.map_gen_lookup = htab_map_gen_lookup,
  	.map_seq_show_elem = htab_map_seq_show_elem,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_hash_elem,
  	BATCH_OPS(htab),
  	.map_btf_name = "bpf_htab",
  	.map_btf_id = &htab_map_btf_id,
@@ -1900,6 +1951,8 @@ const struct bpf_map_ops htab_lru_map_ops = {
  	.map_delete_elem = htab_lru_map_delete_elem,
  	.map_gen_lookup = htab_lru_map_gen_lookup,
  	.map_seq_show_elem = htab_map_seq_show_elem,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_hash_elem,
  	BATCH_OPS(htab_lru),
  	.map_btf_name = "bpf_htab",
  	.map_btf_id = &htab_lru_map_btf_id,
@@ -2019,6 +2072,8 @@ const struct bpf_map_ops htab_percpu_map_ops = {
  	.map_update_elem = htab_percpu_map_update_elem,
  	.map_delete_elem = htab_map_delete_elem,
  	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_hash_elem,
  	BATCH_OPS(htab_percpu),
  	.map_btf_name = "bpf_htab",
  	.map_btf_id = &htab_percpu_map_btf_id,
@@ -2036,6 +2091,8 @@ const struct bpf_map_ops htab_lru_percpu_map_ops = {
  	.map_update_elem = htab_lru_percpu_map_update_elem,
  	.map_delete_elem = htab_lru_map_delete_elem,
  	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_hash_elem,
  	BATCH_OPS(htab_lru_percpu),
  	.map_btf_name = "bpf_htab",
  	.map_btf_id = &htab_lru_percpu_map_btf_id,
kernel/bpf/verifier.c +13 -0
index 050b067a0be6..32c8dcc27da8 100644
@@ -4987,6 +4987,29 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
  	return 0;
  }
 
+int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_func_state *caller,
+				   struct bpf_func_state *callee)
+{
+	/* pointer to map */
+	callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1];
+
+	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
+	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
+	callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr;
+
+	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
+	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
Quoted 8 lines

kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 5006

Quoted 4 lines
kernel/bpf/verifier.c +4 -0
+	callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr;
+
+	/* pointer to stack or null */
+	callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3];

This hard coding of regs 1 through 4 makes sense.
May be add a comment with bpf_for_each_map_elem and callback_fn prototypes, so it's more obvious what's going on.

Yes. an explicit callback_fn prototype comment makes sense.

Quoted 9 lines

kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 5009

Quoted 4 lines
kernel/bpf/verifier.c +3 -0
+
+	/* unused */
+	__mark_reg_unknown(env, &callee->regs[BPF_REG_5]);

I think it should be __mark_reg_not_init to make sure that callback_fn doesn't use r5.
That will help with future extensions via new flags arg that is passed into bpf_for_each_map_elem.

Will do.

#12 lore raw

PATCH 4/8 bpf: add arraymap support for bpf_for_each_map_elem() helper

In reply to Yonghong Song

This patch added support for arraymap and percpu arraymap.

---
 kernel/bpf/arraymap.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
kernel/bpf/arraymap.c +36 -0
index 1f8453343bf2..af0496b9981a 100644
@@ -625,6 +625,38 @@ static const struct bpf_iter_seq_info iter_seq_info = {
 	.seq_priv_size		= sizeof(struct bpf_iter_seq_array_map_info),
 };
 
+static int bpf_for_each_array_elem(struct bpf_map *map, void *callback_fn,
+				   void *callback_ctx, u64 flags)
+{
+	struct bpf_array *array;
+	bool is_percpu;
+	u32 i, index;
+	long ret = 0;
+	void *val;
+
+	if (flags != 0)
+		return -EINVAL;
+
+	is_percpu = map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
+	array = container_of(map, struct bpf_array, map);
+	for (i = 0; i < map->max_entries; i++) {
+		index = i & array->index_mask;
+		if (is_percpu)
+			val = this_cpu_ptr(array->pptrs[i]);
+		else
+			val = array->value + array->elem_size * i;
+		ret = BPF_CAST_CALL(callback_fn)((u64)(long)map,
+					(u64)(long)&index, (u64)(long)val,
+					(u64)(long)callback_ctx, 0);
+		if (ret) {
+			ret = (ret == 1) ? 0 : -EINVAL;
+			break;
+		}
+	}
+
+	return ret;
+}
+
 static int array_map_btf_id;
 const struct bpf_map_ops array_map_ops = {
 	.map_meta_equal = array_map_meta_equal,
@@ -643,6 +675,8 @@ const struct bpf_map_ops array_map_ops = {
 	.map_check_btf = array_map_check_btf,
 	.map_lookup_batch = generic_map_lookup_batch,
 	.map_update_batch = generic_map_update_batch,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_array_elem,
 	.map_btf_name = "bpf_array",
 	.map_btf_id = &array_map_btf_id,
 	.iter_seq_info = &iter_seq_info,
@@ -660,6 +694,8 @@ const struct bpf_map_ops percpu_array_map_ops = {
 	.map_delete_elem = array_map_delete_elem,
 	.map_seq_show_elem = percpu_array_map_seq_show_elem,
 	.map_check_btf = array_map_check_btf,
+	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_for_each_callback = bpf_for_each_array_elem,
 	.map_btf_name = "bpf_array",
 	.map_btf_id = &percpu_array_map_btf_id,
 	.iter_seq_info = &iter_seq_info,
Signature · 1 line
2.24.1
#13 lore raw

PATCH 5/8 libbpf: support local function pointer relocation

In reply to Yonghong Song

A new relocation RELO_LOCAL_FUNC is added to capture local (static) function pointers loaded with ld_imm64 insns. Such ld_imm64 insns are marked with BPF_PSEUDO_FUNC and will be passed to kernel so kernel can replace them with proper actual jited func addresses.

---
 tools/lib/bpf/libbpf.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +30 -3
index 2abbc3800568..a5146c9e3e06 100644
@@ -188,6 +188,7 @@ enum reloc_type {
 	RELO_CALL,
 	RELO_DATA,
 	RELO_EXTERN,
+	RELO_LOCAL_FUNC,
 };
 
 struct reloc_desc {
@@ -574,6 +575,12 @@ static bool insn_is_subprog_call(const struct bpf_insn *insn)
 	       insn->off == 0;
 }
 
+static bool insn_is_pseudo_func(const struct bpf_insn *insn)
+{
+	return insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&
+	       insn->src_reg == BPF_PSEUDO_FUNC;
+}
+
 static int
 bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
 		      const char *name, size_t sec_idx, const char *sec_name,
@@ -3395,6 +3402,16 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
 		return 0;
 	}
 
+	if (insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&
+	    GELF_ST_BIND(sym->st_info) == STB_LOCAL &&
+	    GELF_ST_TYPE(sym->st_info) == STT_SECTION &&
+	    shdr_idx == obj->efile.text_shndx) {
+		reloc_desc->type = RELO_LOCAL_FUNC;
+		reloc_desc->insn_idx = insn_idx;
+		reloc_desc->sym_off = sym->st_value;
+		return 0;
+	}
+
 	if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
 		pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
 			prog->name, sym_name, insn_idx, insn->code);
@@ -6172,6 +6189,9 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
 			}
 			relo->processed = true;
 			break;
+		case RELO_LOCAL_FUNC:
+			insn[0].src_reg = BPF_PSEUDO_FUNC;
+			/* fallthrough */
 		case RELO_CALL:
 			/* will be handled as a follow up pass */
 			break;
@@ -6358,11 +6378,11 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
 
 	for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
 		insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
-		if (!insn_is_subprog_call(insn))
+		if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn))
 			continue;
 
 		relo = find_prog_insn_relo(prog, insn_idx);
-		if (relo && relo->type != RELO_CALL) {
+		if (relo && relo->type != RELO_CALL && relo->type != RELO_LOCAL_FUNC) {
 			pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
 				prog->name, insn_idx, relo->type);
 			return -LIBBPF_ERRNO__RELOC;
@@ -6374,8 +6394,15 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
 			 * call always has imm = -1, but for static functions
 			 * relocation is against STT_SECTION and insn->imm
 			 * points to a start of a static function
+			 *
+			 * for local func relocation, the imm field encodes
+			 * the byte offset in the corresponding section.
 			 */
-			sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
+			if (relo->type == RELO_CALL)
+				sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
+			else
+				sub_insn_idx = relo->sym_off / BPF_INSN_SZ +
+					       insn->imm / BPF_INSN_SZ + 1;
 		} else {
 			/* if subprogram call is to a static function within
 			 * the same ELF section, there won't be any relocation
Signature · 1 line
2.24.1
#14 lore raw

Re: [PATCH bpf-next 5/8] libbpf: support local function pointer relocation

Quoted 18 lines · Yonghong Song

A new relocation RELO_LOCAL_FUNC is added to capture local (static) function pointers loaded with ld_imm64 insns. Such ld_imm64 insns are marked with BPF_PSEUDO_FUNC and will be passed to kernel so kernel can replace them with proper actual jited func addresses.

---
 tools/lib/bpf/libbpf.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index 2abbc3800568..a5146c9e3e06 100644

tools/lib/bpf/libbpf.c enum reloc_type { · line 191

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -188,6 +188,7 @@ enum reloc_type {
        RELO_CALL,
        RELO_DATA,
        RELO_EXTERN,
+       RELO_LOCAL_FUNC,

libbpf internally is using SUBPROG notation. I think "LOCAL" part is confusing, so I'd drop it. How about just RELO_SUBPROG? We can separately refactor these names to distinguish RELO_CALL from the new one. It would be more clear if RELO_CALL was called RELO_SUBPROG_CALL, and the new one either RELO_SUBPROG_ADDR or RELO_SUBPROG_REF (as in subprog reference)

tools/lib/bpf/libbpf.c static bool insn_is_subprog_call(const struct bpf_insn *insn) · line 580

Quoted 10 lines
tools/lib/bpf/libbpf.c +3 -0
 };
 
 struct reloc_desc {
@@ -574,6 +575,12 @@ static bool insn_is_subprog_call(const struct bpf_insn *insn)
               insn->off == 0;
 }
 
+static bool insn_is_pseudo_func(const struct bpf_insn *insn)
+{
+       return insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&

there is is_ldimm64() function for this check (just move it up here, it's a single-liner)

tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 3405

Quoted 11 lines
tools/lib/bpf/libbpf.c +4 -0
+              insn->src_reg == BPF_PSEUDO_FUNC;
+}
+
 static int
 bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
                      const char *name, size_t sec_idx, const char *sec_name,
@@ -3395,6 +3402,16 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
                return 0;
        }
 
+       if (insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&

just move this check below the next if that checks !is_ldimm64, no need to do it here early.

tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 3408

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+           GELF_ST_BIND(sym->st_info) == STB_LOCAL &&
+           GELF_ST_TYPE(sym->st_info) == STT_SECTION &&
+           shdr_idx == obj->efile.text_shndx) {

see above how RELO_CALL is handled: shdr_idx != 0 check is missing. We also validate that sym->st_value is multiple of BPF_INSN_SZ.

tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 3415

Quoted 7 lines
tools/lib/bpf/libbpf.c +6 -0
+               reloc_desc->type = RELO_LOCAL_FUNC;
+               reloc_desc->insn_idx = insn_idx;
+               reloc_desc->sym_off = sym->st_value;
+               return 0;
+       }
+
        if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {

feel free to use is_ldimm64 here as well, thanks!

tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 6194

Quoted 9 lines
tools/lib/bpf/libbpf.c +3 -0
                pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
                        prog->name, sym_name, insn_idx, insn->code);
@@ -6172,6 +6189,9 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
                        }
                        relo->processed = true;
                        break;
+               case RELO_LOCAL_FUNC:
+                       insn[0].src_reg = BPF_PSEUDO_FUNC;
+                       /* fallthrough */

fallthrough into an empty break clause seems a bit weird... just break and leave the same comment as below?

Quoted 21 lines
tools/lib/bpf/libbpf.c +2 -2
                case RELO_CALL:
                        /* will be handled as a follow up pass */
                        break;
@@ -6358,11 +6378,11 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
 
        for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
                insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
-               if (!insn_is_subprog_call(insn))
+               if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn))
                        continue;
 
                relo = find_prog_insn_relo(prog, insn_idx);
-               if (relo && relo->type != RELO_CALL) {
+               if (relo && relo->type != RELO_CALL && relo->type != RELO_LOCAL_FUNC) {
                        pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
                                prog->name, insn_idx, relo->type);
                        return -LIBBPF_ERRNO__RELOC;
@@ -6374,8 +6394,15 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
                         * call always has imm = -1, but for static functions
                         * relocation is against STT_SECTION and insn->imm
                         * points to a start of a static function

tools/lib/bpf/libbpf.c bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog, · line 6405

Quoted 10 lines
tools/lib/bpf/libbpf.c +8 -1
+                        *
+                        * for local func relocation, the imm field encodes
+                        * the byte offset in the corresponding section.
                         */
-                       sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
+                       if (relo->type == RELO_CALL)
+                               sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
+                       else
+                               sub_insn_idx = relo->sym_off / BPF_INSN_SZ +
+                                              insn->imm / BPF_INSN_SZ + 1;

nit: keep it on a single line, it still fits within 100 characters and is easier to visually compare to RELO_CALL case.

tools/lib/bpf/libbpf.c bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog, · line 6408

Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0
                } else {
                        /* if subprogram call is to a static function within
                         * the same ELF section, there won't be any relocation

don't we have to adjust insn->imm for this case as well? Let's add selftests to make sure this works.

Quoted 3 lines

--
2.24.1

#15 lore raw

Re: [PATCH bpf-next 5/8] libbpf: support local function pointer relocation

Quoted 14 lines · Andrii Nakryiko
Quoted 13 lines · Yonghong Song

A new relocation RELO_LOCAL_FUNC is added to capture local (static) function pointers loaded with ld_imm64 insns. Such ld_imm64 insns are marked with BPF_PSEUDO_FUNC and will be passed to kernel so kernel can replace them with proper actual jited func addresses.

---
  tools/lib/bpf/libbpf.c | 33 ++++++++++++++++++++++++++++++---
  1 file changed, 30 insertions(+), 3 deletions(-)
Quoted 16 lines

tools/lib/bpf/libbpf.c enum reloc_type { · line 191

Quoted 9 lines
tools/lib/bpf/libbpf.c +1 -0
index 2abbc3800568..a5146c9e3e06 100644
@@ -188,6 +188,7 @@ enum reloc_type {
         RELO_CALL,
         RELO_DATA,
         RELO_EXTERN,
+       RELO_LOCAL_FUNC,

libbpf internally is using SUBPROG notation. I think "LOCAL" part is confusing, so I'd drop it. How about just RELO_SUBPROG? We can separately refactor these names to distinguish RELO_CALL from the new one. It would be more clear if RELO_CALL was called RELO_SUBPROG_CALL, and the new one either RELO_SUBPROG_ADDR or RELO_SUBPROG_REF (as in subprog reference)

Yes, we can use RELO_SUBPROG_ADDR.

Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/libbpf.c +0 -0
  };
 
  struct reloc_desc {
@@ -574,6 +575,12 @@ static bool insn_is_subprog_call(const struct bpf_insn *insn)
                insn->off == 0;
  }
 
Quoted 6 lines

tools/lib/bpf/libbpf.c static bool insn_is_subprog_call(const struct bpf_insn *insn) · line 580

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+static bool insn_is_pseudo_func(const struct bpf_insn *insn)
+{
+       return insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&

there is is_ldimm64() function for this check (just move it up here, it's a single-liner)

I did not know it. Will use in the next revision.

Quoted 11 lines
Quoted 11 lines
tools/lib/bpf/libbpf.c +3 -0
+              insn->src_reg == BPF_PSEUDO_FUNC;
+}
+
  static int
  bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
                       const char *name, size_t sec_idx, const char *sec_name,
@@ -3395,6 +3402,16 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
                 return 0;
         }
 
Quoted 4 lines

tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 3405

Quoted 1 line
tools/lib/bpf/libbpf.c +1 -0
+       if (insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&

just move this check below the next if that checks !is_ldimm64, no need to do it here early.

Okay.

Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 3408

Quoted 4 lines
tools/lib/bpf/libbpf.c +3 -0
+           GELF_ST_BIND(sym->st_info) == STB_LOCAL &&
+           GELF_ST_TYPE(sym->st_info) == STT_SECTION &&
+           shdr_idx == obj->efile.text_shndx) {

see above how RELO_CALL is handled: shdr_idx != 0 check is missing. We also validate that sym->st_value is multiple of BPF_INSN_SZ.

Okay. Will add additional checking.

Quoted 11 lines

tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 3415

Quoted 8 lines
tools/lib/bpf/libbpf.c +6 -0
+               reloc_desc->type = RELO_LOCAL_FUNC;
+               reloc_desc->insn_idx = insn_idx;
+               reloc_desc->sym_off = sym->st_value;
+               return 0;
+       }
+
         if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {

feel free to use is_ldimm64 here as well, thanks!

Quoted 12 lines

tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 6194

Quoted 9 lines
tools/lib/bpf/libbpf.c +3 -0
                 pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
                         prog->name, sym_name, insn_idx, insn->code);
@@ -6172,6 +6189,9 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
                         }
                         relo->processed = true;
                         break;
+               case RELO_LOCAL_FUNC:
+                       insn[0].src_reg = BPF_PSEUDO_FUNC;
+                       /* fallthrough */

fallthrough into an empty break clause seems a bit weird... just break and leave the same comment as below?

Yes, "break" seems cleaner.

Quoted 27 lines
Quoted 27 lines
tools/lib/bpf/libbpf.c +5 -3
                 case RELO_CALL:
                         /* will be handled as a follow up pass */
                         break;
@@ -6358,11 +6378,11 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
 
         for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
                 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
-               if (!insn_is_subprog_call(insn))
+               if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn))
                         continue;
 
                 relo = find_prog_insn_relo(prog, insn_idx);
-               if (relo && relo->type != RELO_CALL) {
+               if (relo && relo->type != RELO_CALL && relo->type != RELO_LOCAL_FUNC) {
                         pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
                                 prog->name, insn_idx, relo->type);
                         return -LIBBPF_ERRNO__RELOC;
@@ -6374,8 +6394,15 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
                          * call always has imm = -1, but for static functions
                          * relocation is against STT_SECTION and insn->imm
                          * points to a start of a static function
+                        *
+                        * for local func relocation, the imm field encodes
+                        * the byte offset in the corresponding section.
                          */
-                       sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
Quoted 8 lines

tools/lib/bpf/libbpf.c bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog, · line 6405

Quoted 5 lines
tools/lib/bpf/libbpf.c +5 -0
+                       if (relo->type == RELO_CALL)
+                               sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
+                       else
+                               sub_insn_idx = relo->sym_off / BPF_INSN_SZ +
+                                              insn->imm / BPF_INSN_SZ + 1;

nit: keep it on a single line, it still fits within 100 characters and is easier to visually compare to RELO_CALL case.

Okay.

Quoted 7 lines

tools/lib/bpf/libbpf.c bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog, · line 6408

Quoted 4 lines
tools/lib/bpf/libbpf.c +0 -0
                 } else {
                         /* if subprogram call is to a static function within
                          * the same ELF section, there won't be any relocation

don't we have to adjust insn->imm for this case as well? Let's add selftests to make sure this works.

This is for relo == NULL. I think my code (RELO_LOCAL_FUNC or RELO_SUBPROG_ADDR) won't hit this since there always relocations. That is why I didn't do anything here.

Quoted 4 lines
Quoted 4 lines

--
2.24.1

#16 lore raw

Re: [PATCH bpf-next 5/8] libbpf: support local function pointer relocation

Quoted 151 lines · Yonghong Song
Quoted 14 lines · Andrii Nakryiko
Quoted 13 lines · Yonghong Song

A new relocation RELO_LOCAL_FUNC is added to capture local (static) function pointers loaded with ld_imm64 insns. Such ld_imm64 insns are marked with BPF_PSEUDO_FUNC and will be passed to kernel so kernel can replace them with proper actual jited func addresses.

---
  tools/lib/bpf/libbpf.c | 33 ++++++++++++++++++++++++++++++---
  1 file changed, 30 insertions(+), 3 deletions(-)
Quoted 16 lines

tools/lib/bpf/libbpf.c enum reloc_type { · line 191

Quoted 9 lines
tools/lib/bpf/libbpf.c +1 -0
index 2abbc3800568..a5146c9e3e06 100644
@@ -188,6 +188,7 @@ enum reloc_type {
         RELO_CALL,
         RELO_DATA,
         RELO_EXTERN,
+       RELO_LOCAL_FUNC,

libbpf internally is using SUBPROG notation. I think "LOCAL" part is confusing, so I'd drop it. How about just RELO_SUBPROG? We can separately refactor these names to distinguish RELO_CALL from the new one. It would be more clear if RELO_CALL was called RELO_SUBPROG_CALL, and the new one either RELO_SUBPROG_ADDR or RELO_SUBPROG_REF (as in subprog reference)

Yes, we can use RELO_SUBPROG_ADDR.

Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/libbpf.c +0 -0
  };
 
  struct reloc_desc {
@@ -574,6 +575,12 @@ static bool insn_is_subprog_call(const struct bpf_insn *insn)
                insn->off == 0;
  }
 
Quoted 6 lines

tools/lib/bpf/libbpf.c static bool insn_is_subprog_call(const struct bpf_insn *insn) · line 580

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+static bool insn_is_pseudo_func(const struct bpf_insn *insn)
+{
+       return insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&

there is is_ldimm64() function for this check (just move it up here, it's a single-liner)

I did not know it. Will use in the next revision.

Quoted 11 lines
Quoted 11 lines
tools/lib/bpf/libbpf.c +3 -0
+              insn->src_reg == BPF_PSEUDO_FUNC;
+}
+
  static int
  bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
                       const char *name, size_t sec_idx, const char *sec_name,
@@ -3395,6 +3402,16 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
                 return 0;
         }
 
Quoted 4 lines

tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 3405

Quoted 1 line
tools/lib/bpf/libbpf.c +1 -0
+       if (insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&

just move this check below the next if that checks !is_ldimm64, no need to do it here early.

Okay.

Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 3408

Quoted 4 lines
tools/lib/bpf/libbpf.c +3 -0
+           GELF_ST_BIND(sym->st_info) == STB_LOCAL &&
+           GELF_ST_TYPE(sym->st_info) == STT_SECTION &&
+           shdr_idx == obj->efile.text_shndx) {

see above how RELO_CALL is handled: shdr_idx != 0 check is missing. We also validate that sym->st_value is multiple of BPF_INSN_SZ.

Okay. Will add additional checking.

Quoted 11 lines

tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 3415

Quoted 8 lines
tools/lib/bpf/libbpf.c +6 -0
+               reloc_desc->type = RELO_LOCAL_FUNC;
+               reloc_desc->insn_idx = insn_idx;
+               reloc_desc->sym_off = sym->st_value;
+               return 0;
+       }
+
         if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {

feel free to use is_ldimm64 here as well, thanks!

Quoted 12 lines

tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 6194

Quoted 9 lines
tools/lib/bpf/libbpf.c +3 -0
                 pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
                         prog->name, sym_name, insn_idx, insn->code);
@@ -6172,6 +6189,9 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
                         }
                         relo->processed = true;
                         break;
+               case RELO_LOCAL_FUNC:
+                       insn[0].src_reg = BPF_PSEUDO_FUNC;
+                       /* fallthrough */

fallthrough into an empty break clause seems a bit weird... just break and leave the same comment as below?

Yes, "break" seems cleaner.

Quoted 27 lines
Quoted 27 lines
tools/lib/bpf/libbpf.c +5 -3
                 case RELO_CALL:
                         /* will be handled as a follow up pass */
                         break;
@@ -6358,11 +6378,11 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
 
         for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
                 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
-               if (!insn_is_subprog_call(insn))
+               if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn))
                         continue;
 
                 relo = find_prog_insn_relo(prog, insn_idx);
-               if (relo && relo->type != RELO_CALL) {
+               if (relo && relo->type != RELO_CALL && relo->type != RELO_LOCAL_FUNC) {
                         pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
                                 prog->name, insn_idx, relo->type);
                         return -LIBBPF_ERRNO__RELOC;
@@ -6374,8 +6394,15 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
                          * call always has imm = -1, but for static functions
                          * relocation is against STT_SECTION and insn->imm
                          * points to a start of a static function
+                        *
+                        * for local func relocation, the imm field encodes
+                        * the byte offset in the corresponding section.
                          */
-                       sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
Quoted 8 lines

tools/lib/bpf/libbpf.c bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog, · line 6405

Quoted 5 lines
tools/lib/bpf/libbpf.c +5 -0
+                       if (relo->type == RELO_CALL)
+                               sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
+                       else
+                               sub_insn_idx = relo->sym_off / BPF_INSN_SZ +
+                                              insn->imm / BPF_INSN_SZ + 1;

nit: keep it on a single line, it still fits within 100 characters and is easier to visually compare to RELO_CALL case.

Okay.

Quoted 5 lines
Quoted 4 lines
tools/lib/bpf/libbpf.c +0 -0
                 } else {
                         /* if subprogram call is to a static function within
                          * the same ELF section, there won't be any relocation
Quoted 6 lines
Quoted 2 lines

don't we have to adjust insn->imm for this case as well? Let's add selftests to make sure this works.

This is for relo == NULL. I think my code (RELO_LOCAL_FUNC or RELO_SUBPROG_ADDR) won't hit this since there always relocations. That is why I didn't do anything here.

Hm.. is it handled differently from calls then? When there is a call from one static subprog to another static subprog, there is no relocation (because the compiler can guarantee that both functions are in the same section and their relative location won't change, presumably). Why is this not the case for getting the address of a function here?

Regardless, if this can't happen, then let's check and error out, instead of silently producing invalid values.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

--
2.24.1

#17 lore raw

PATCH 6/8 bpftool: print local function pointer properly

In reply to Yonghong Song

With later hashmap example, using bpftool xlated output may look like:
int dump_task(struct bpf_iter__task * ctx):

  ; struct task_struct *task = ctx->task;
     0: (79) r2 = *(u64 *)(r1 +8)
  ; if (task == (void *)0 || called > 0)
  ...
    19: (18) r2 = subprog[+18]
    30: (18) r2 = subprog[+26]
  ...
  36: (95) exit
  __u64 check_hash_elem(struct bpf_map * map, __u32 * key, __u64 * val,
                        struct callback_ctx * data):
  ; struct bpf_iter__task *ctx = data->ctx;
    37: (79) r5 = *(u64 *)(r4 +0)
  ...
    55: (95) exit
  __u64 check_percpu_elem(struct bpf_map * map, __u32 * key,
                          __u64 * val, void * unused):
  ; check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val, void *unused)
    56: (bf) r6 = r3
  ...
    83: (18) r2 = subprog[+-46]
---
 tools/bpf/bpftool/xlated_dumper.c | 3 +++
 1 file changed, 3 insertions(+)
tools/bpf/bpftool/xlated_dumper.c +3 -0
index 8608cd68cdd0..7bdd90503727 100644
@@ -196,6 +196,9 @@ static const char *print_imm(void *private_data,
 	else if (insn->src_reg == BPF_PSEUDO_MAP_VALUE)
 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
 			 "map[id:%u][0]+%u", insn->imm, (insn + 1)->imm);
+	else if (insn->src_reg == BPF_PSEUDO_FUNC)
+		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
+			 "subprog[+%d]", insn->imm + 1);
 	else
 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
 			 "0x%llx", (unsigned long long)full_imm);
Signature · 1 line
2.24.1
#18 lore raw

Re: [PATCH bpf-next 6/8] bpftool: print local function pointer properly

Quoted 16 lines · Yonghong Song

With later hashmap example, using bpftool xlated output may look like:
int dump_task(struct bpf_iter__task * ctx):

  ; struct task_struct *task = ctx->task;
     0: (79) r2 = *(u64 *)(r1 +8)
  ; if (task == (void *)0 || called > 0)
  ...
    19: (18) r2 = subprog[+18]
    30: (18) r2 = subprog[+26]
  ...
  36: (95) exit
  __u64 check_hash_elem(struct bpf_map * map, __u32 * key, __u64 * val,
                        struct callback_ctx * data):
  ; struct bpf_iter__task *ctx = data->ctx;
    37: (79) r5 = *(u64 *)(r4 +0)
Quoted 8 lines

...

    55: (95) exit
  __u64 check_percpu_elem(struct bpf_map * map, __u32 * key,
                          __u64 * val, void * unused):
  ; check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val, void *unused)
    56: (bf) r6 = r3
  ...
    83: (18) r2 = subprog[+-46]

this +-46 looks very confusing...

Quoted 11 lines
---
 tools/bpf/bpftool/xlated_dumper.c | 3 +++
 1 file changed, 3 insertions(+)
tools/bpf/bpftool/xlated_dumper.c +0 -0
index 8608cd68cdd0..7bdd90503727 100644

tools/bpf/bpftool/xlated_dumper.c static const char *print_imm(void *private_data, · line 201

Quoted 6 lines
tools/bpf/bpftool/xlated_dumper.c +3 -0
@@ -196,6 +196,9 @@ static const char *print_imm(void *private_data,
        else if (insn->src_reg == BPF_PSEUDO_MAP_VALUE)
                snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
                         "map[id:%u][0]+%u", insn->imm, (insn + 1)->imm);
+       else if (insn->src_reg == BPF_PSEUDO_FUNC)
+               snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
+                        "subprog[+%d]", insn->imm + 1);

why not subprog[%+d] instead (see above about confusing output)

Quoted 6 lines
tools/bpf/bpftool/xlated_dumper.c +0 -1
        else
                snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
                         "0x%llx", (unsigned long long)full_imm);
--

2.24.1

#19 lore raw

Re: [PATCH bpf-next 6/8] bpftool: print local function pointer properly

Quoted 26 lines · Andrii Nakryiko
Quoted 16 lines · Yonghong Song

With later hashmap example, using bpftool xlated output may look like:

   int dump_task(struct bpf_iter__task * ctx):
   ; struct task_struct *task = ctx->task;
      0: (79) r2 = *(u64 *)(r1 +8)
   ; if (task == (void *)0 || called > 0)
   ...
     19: (18) r2 = subprog[+18]
     30: (18) r2 = subprog[+26]
   ...
   36: (95) exit
   __u64 check_hash_elem(struct bpf_map * map, __u32 * key, __u64 * val,
                         struct callback_ctx * data):
   ; struct bpf_iter__task *ctx = data->ctx;
     37: (79) r5 = *(u64 *)(r4 +0)
Quoted 8 lines
   ...
     55: (95) exit
   __u64 check_percpu_elem(struct bpf_map * map, __u32 * key,
                           __u64 * val, void * unused):
   ; check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val, void *unused)
     56: (bf) r6 = r3
   ...
     83: (18) r2 = subprog[+-46]
Quoted 1 line

this +-46 looks very confusing...

Make sense, will use %+d to have either +46 or -46.

Quoted 27 lines
Quoted 12 lines
---
  tools/bpf/bpftool/xlated_dumper.c | 3 +++
  1 file changed, 3 insertions(+)
tools/bpf/bpftool/xlated_dumper.c +0 -0
index 8608cd68cdd0..7bdd90503727 100644

tools/bpf/bpftool/xlated_dumper.c static const char *print_imm(void *private_data, · line 201

Quoted 6 lines
tools/bpf/bpftool/xlated_dumper.c +3 -0
@@ -196,6 +196,9 @@ static const char *print_imm(void *private_data,
         else if (insn->src_reg == BPF_PSEUDO_MAP_VALUE)
                 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
                          "map[id:%u][0]+%u", insn->imm, (insn + 1)->imm);
+       else if (insn->src_reg == BPF_PSEUDO_FUNC)
+               snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
+                        "subprog[+%d]", insn->imm + 1);

why not subprog[%+d] instead (see above about confusing output)

Quoted 6 lines
tools/bpf/bpftool/xlated_dumper.c +0 -1
         else
                 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
                          "0x%llx", (unsigned long long)full_imm);
--

2.24.1

#20 lore raw

PATCH 7/8 selftests/bpf: add hashmap test for bpf_for_each_map_elem() helper

In reply to Yonghong Song

A test case is added for hashmap and percpu hashmap. The test also exercises nested bpf_for_each_map_elem() calls like

    bpf_prog:
      bpf_for_each_map_elem(func1)
    func1:
      bpf_for_each_map_elem(func2)
    func2:
  $ ./test_progs -n 44
  #44/1 hash_map:OK
  #44 for_each:OK
  Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
---
 .../selftests/bpf/prog_tests/for_each.c       |  91 ++++++++++++++++
 .../bpf/progs/for_each_hash_map_elem.c        | 103 ++++++++++++++++++
 2 files changed, 194 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/for_each.c
 create mode 100644 tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c
tools/testing/selftests/bpf/prog_tests/for_each.c new file +91 -0
new file mode 100644
index 000000000000..7a399fbc89a4
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+#include <test_progs.h>
+#include "for_each_hash_map_elem.skel.h"
+
+static int duration;
+
+static void do_dummy_read(struct bpf_program *prog)
+{
+	struct bpf_link *link;
+	char buf[16] = {};
+	int iter_fd, len;
+
+	link = bpf_program__attach_iter(prog, NULL);
+	if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n"))
+		return;
+
+	iter_fd = bpf_iter_create(bpf_link__fd(link));
+	if (CHECK(iter_fd < 0, "create_iter", "create_iter failed\n"))
+		goto free_link;
+
+	/* not check contents, but ensure read() ends without error */
+	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
+		;
+	CHECK(len < 0, "read", "read failed: %s\n", strerror(errno));
+
+	close(iter_fd);
+
+free_link:
+	bpf_link__destroy(link);
+}
+
+static void test_hash_map(void)
+{
+	int i, hashmap_fd, percpu_map_fd, err;
+	struct for_each_hash_map_elem *skel;
+	__u64 *percpu_valbuf = NULL;
+	__u32 key, num_cpus;
+	__u64 val;
+
+	skel = for_each_hash_map_elem__open_and_load();
+	if (CHECK(!skel, "for_each_hash_map_elem__open_and_load",
+		  "skeleton open_and_load failed\n"))
+		return;
+
+	hashmap_fd = bpf_map__fd(skel->maps.hashmap);
+	for (i = 0; i < bpf_map__max_entries(skel->maps.hashmap); i++) {
+		key = i;
+		val = i + 1;
+		err = bpf_map_update_elem(hashmap_fd, &key, &val, BPF_ANY);
+		if (CHECK(err, "map_update", "map_update failed\n"))
+			goto out;
+	}
+
+	num_cpus = bpf_num_possible_cpus();
+	percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);
+	percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
+	if (CHECK_FAIL(!percpu_valbuf))
+		goto out;
+
+	key = 1;
+	for (i = 0; i < num_cpus; i++)
+		percpu_valbuf[i] = i + 1;
+	err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
+	if (CHECK(err, "percpu_map_update", "map_update failed\n"))
+		goto out;
+
+	do_dummy_read(skel->progs.dump_task);
+
+	ASSERT_EQ(skel->bss->called, 1, "called");
+	ASSERT_EQ(skel->bss->hashmap_output, 4, "output_val");
+
+	key = 1;
+	err = bpf_map_lookup_elem(hashmap_fd, &key, &val);
+	ASSERT_ERR(err, "hashmap_lookup");
+
+	ASSERT_EQ(skel->bss->percpu_called, 1, "percpu_called");
+	CHECK_FAIL(skel->bss->cpu >= num_cpus);
+	ASSERT_EQ(skel->bss->percpu_key, 1, "percpu_key");
+	ASSERT_EQ(skel->bss->percpu_val, skel->bss->cpu + 1, "percpu_val");
+	ASSERT_EQ(skel->bss->percpu_output, 100, "percpu_output");
+out:
+	free(percpu_valbuf);
+	for_each_hash_map_elem__destroy(skel);
+}
+
+void test_for_each(void)
+{
+	if (test__start_subtest("hash_map"))
+		test_hash_map();
+}
tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c new file +103 -0

103 lines not on this page · raw message

Signature · 1 line
2.24.1
#21 lore raw

Re: [PATCH bpf-next 7/8] selftests/bpf: add hashmap test for bpf_for_each_map_elem() helper

Quoted 22 lines · Yonghong Song

A test case is added for hashmap and percpu hashmap. The test also exercises nested bpf_for_each_map_elem() calls like

    bpf_prog:
      bpf_for_each_map_elem(func1)
    func1:
      bpf_for_each_map_elem(func2)
    func2:
  $ ./test_progs -n 44
  #44/1 hash_map:OK
  #44 for_each:OK
  Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
---
 .../selftests/bpf/prog_tests/for_each.c       |  91 ++++++++++++++++
 .../bpf/progs/for_each_hash_map_elem.c        | 103 ++++++++++++++++++
 2 files changed, 194 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/for_each.c
 create mode 100644 tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c

[...]

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +4 -0
+       num_cpus = bpf_num_possible_cpus();
+       percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);
+       percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
+       if (CHECK_FAIL(!percpu_valbuf))

tools/testing/selftests/bpf/prog_tests/for_each.c line 68

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +10 -0
+               goto out;
+
+       key = 1;
+       for (i = 0; i < num_cpus; i++)
+               percpu_valbuf[i] = i + 1;
+       err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
+       if (CHECK(err, "percpu_map_update", "map_update failed\n"))
+               goto out;
+
+       do_dummy_read(skel->progs.dump_task);

why use iter/task programs to trigger your test BPF code? This test doesn't seem to rely on anything iter-specific, so it's much simpler (and less code) to just use the typical sys_enter approach with usleep(1) as a trigger function, no?

tools/testing/selftests/bpf/prog_tests/for_each.c line 78

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +10 -0
+
+       ASSERT_EQ(skel->bss->called, 1, "called");
+       ASSERT_EQ(skel->bss->hashmap_output, 4, "output_val");
+
+       key = 1;
+       err = bpf_map_lookup_elem(hashmap_fd, &key, &val);
+       ASSERT_ERR(err, "hashmap_lookup");
+
+       ASSERT_EQ(skel->bss->percpu_called, 1, "percpu_called");
+       CHECK_FAIL(skel->bss->cpu >= num_cpus);

please don't use CHECK_FAIL: use CHECK or one of ASSERT_xxx variants

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +13 -0
+       ASSERT_EQ(skel->bss->percpu_key, 1, "percpu_key");
+       ASSERT_EQ(skel->bss->percpu_val, skel->bss->cpu + 1, "percpu_val");
+       ASSERT_EQ(skel->bss->percpu_output, 100, "percpu_output");
+out:
+       free(percpu_valbuf);
+       for_each_hash_map_elem__destroy(skel);
+}
+
+void test_for_each(void)
+{
+       if (test__start_subtest("hash_map"))
+               test_hash_map();
+}

[...]

tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c line 61

Quoted 12 lines
tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c new file +12 -0
+
+__u32 cpu = 0;
+__u32 percpu_called = 0;
+__u32 percpu_key = 0;
+__u64 percpu_val = 0;
+
+static __u64
+check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val,
+                 struct callback_ctx *data)
+{
+       percpu_called++;
+       cpu = bpf_get_smp_processor_id();

It's a bit counter-intuitive (at least I was confused initially) that for a per-cpu array for_each() will iterate only current CPU's elements. I think it's worthwhile to emphasize this in bpf_for_each_map_elem() documentation (at least), and call out in corresponding patches adding per-cpu array/hash iteration support.

Quoted 7 lines
tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c new file +7 -0
+       percpu_key = *key;
+       percpu_val = *val;
+
+       bpf_for_each_map_elem(&hashmap, check_hash_elem, data, 0);
+       return 0;
+}
+

[...]

#22 lore raw

Re: [PATCH bpf-next 7/8] selftests/bpf: add hashmap test for bpf_for_each_map_elem() helper

Quoted 37 lines · Andrii Nakryiko
Quoted 22 lines · Yonghong Song

A test case is added for hashmap and percpu hashmap. The test also exercises nested bpf_for_each_map_elem() calls like

     bpf_prog:
       bpf_for_each_map_elem(func1)
     func1:
       bpf_for_each_map_elem(func2)
     func2:
   $ ./test_progs -n 44
   #44/1 hash_map:OK
   #44 for_each:OK
   Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
---
  .../selftests/bpf/prog_tests/for_each.c       |  91 ++++++++++++++++
  .../bpf/progs/for_each_hash_map_elem.c        | 103 ++++++++++++++++++
  2 files changed, 194 insertions(+)

create mode 100644 tools/testing/selftests/bpf/prog_tests/for_each.c create mode 100644 tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c

[...]

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +11 -0
+       num_cpus = bpf_num_possible_cpus();
+       percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);
+       percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
+       if (CHECK_FAIL(!percpu_valbuf))
+               goto out;
+
+       key = 1;
+       for (i = 0; i < num_cpus; i++)
+               percpu_valbuf[i] = i + 1;
+       err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
+       if (CHECK(err, "percpu_map_update", "map_update failed\n"))
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/for_each.c line 68

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +3 -0
+               goto out;
+
+       do_dummy_read(skel->progs.dump_task);

why use iter/task programs to trigger your test BPF code? This test doesn't seem to rely on anything iter-specific, so it's much simpler (and less code) to just use the typical sys_enter approach with usleep(1) as a trigger function, no?

I am aware of this. I did not change this in v1 mainly wanting to get some comments on API and verifier change etc. for v1. I will use bpf_prog_test_run() to call the program in v2.

Quoted 13 lines

tools/testing/selftests/bpf/prog_tests/for_each.c line 78

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +10 -0
+
+       ASSERT_EQ(skel->bss->called, 1, "called");
+       ASSERT_EQ(skel->bss->hashmap_output, 4, "output_val");
+
+       key = 1;
+       err = bpf_map_lookup_elem(hashmap_fd, &key, &val);
+       ASSERT_ERR(err, "hashmap_lookup");
+
+       ASSERT_EQ(skel->bss->percpu_called, 1, "percpu_called");
+       CHECK_FAIL(skel->bss->cpu >= num_cpus);

please don't use CHECK_FAIL: use CHECK or one of ASSERT_xxx variants

We do not have ASSERT_GE, I may invent one.

Quoted 27 lines
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +13 -0
+       ASSERT_EQ(skel->bss->percpu_key, 1, "percpu_key");
+       ASSERT_EQ(skel->bss->percpu_val, skel->bss->cpu + 1, "percpu_val");
+       ASSERT_EQ(skel->bss->percpu_output, 100, "percpu_output");
+out:
+       free(percpu_valbuf);
+       for_each_hash_map_elem__destroy(skel);
+}
+
+void test_for_each(void)
+{
+       if (test__start_subtest("hash_map"))
+               test_hash_map();
+}

[...]

Quoted 10 lines
tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c new file +10 -0
+
+__u32 cpu = 0;
+__u32 percpu_called = 0;
+__u32 percpu_key = 0;
+__u64 percpu_val = 0;
+
+static __u64
+check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val,
+                 struct callback_ctx *data)
+{
Quoted 8 lines

tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c line 61

Quoted 2 lines
tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c new file +2 -0
+       percpu_called++;
+       cpu = bpf_get_smp_processor_id();

It's a bit counter-intuitive (at least I was confused initially) that for a per-cpu array for_each() will iterate only current CPU's elements. I think it's worthwhile to emphasize this in bpf_for_each_map_elem() documentation (at least), and call out in corresponding patches adding per-cpu array/hash iteration support.

Right. Will emphasize this in uapi bpf.h and also comments in the code.

Quoted 11 lines
Quoted 8 lines
tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c new file +7 -0
+       percpu_key = *key;
+       percpu_val = *val;
+
+       bpf_for_each_map_elem(&hashmap, check_hash_elem, data, 0);
+       return 0;
+}
+

[...]

#23 lore raw

Re: [PATCH bpf-next 7/8] selftests/bpf: add hashmap test for bpf_for_each_map_elem() helper

Quoted 68 lines · Yonghong Song
Quoted 37 lines · Andrii Nakryiko
Quoted 22 lines · Yonghong Song

A test case is added for hashmap and percpu hashmap. The test also exercises nested bpf_for_each_map_elem() calls like

     bpf_prog:
       bpf_for_each_map_elem(func1)
     func1:
       bpf_for_each_map_elem(func2)
     func2:
   $ ./test_progs -n 44
   #44/1 hash_map:OK
   #44 for_each:OK
   Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
---
  .../selftests/bpf/prog_tests/for_each.c       |  91 ++++++++++++++++
  .../bpf/progs/for_each_hash_map_elem.c        | 103 ++++++++++++++++++
  2 files changed, 194 insertions(+)

create mode 100644 tools/testing/selftests/bpf/prog_tests/for_each.c create mode 100644 tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c

[...]

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +11 -0
+       num_cpus = bpf_num_possible_cpus();
+       percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);
+       percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
+       if (CHECK_FAIL(!percpu_valbuf))
+               goto out;
+
+       key = 1;
+       for (i = 0; i < num_cpus; i++)
+               percpu_valbuf[i] = i + 1;
+       err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
+       if (CHECK(err, "percpu_map_update", "map_update failed\n"))
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/for_each.c line 68

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +3 -0
+               goto out;
+
+       do_dummy_read(skel->progs.dump_task);

why use iter/task programs to trigger your test BPF code? This test doesn't seem to rely on anything iter-specific, so it's much simpler (and less code) to just use the typical sys_enter approach with usleep(1) as a trigger function, no?

I am aware of this. I did not change this in v1 mainly wanting to get some comments on API and verifier change etc. for v1. I will use bpf_prog_test_run() to call the program in v2.

Quoted 13 lines

tools/testing/selftests/bpf/prog_tests/for_each.c line 78

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +10 -0
+
+       ASSERT_EQ(skel->bss->called, 1, "called");
+       ASSERT_EQ(skel->bss->hashmap_output, 4, "output_val");
+
+       key = 1;
+       err = bpf_map_lookup_elem(hashmap_fd, &key, &val);
+       ASSERT_ERR(err, "hashmap_lookup");
+
+       ASSERT_EQ(skel->bss->percpu_called, 1, "percpu_called");
+       CHECK_FAIL(skel->bss->cpu >= num_cpus);

please don't use CHECK_FAIL: use CHECK or one of ASSERT_xxx variants

Quoted 1 line

We do not have ASSERT_GE, I may invent one.

Yeah, it has come up multiple times now. It would be great to have all the inequality variants so that we can stop adding new CHECK()s which has notoriously confusing semantics. Thanks!

Quoted 50 lines
Quoted 28 lines
Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/for_each.c new file +13 -0
+       ASSERT_EQ(skel->bss->percpu_key, 1, "percpu_key");
+       ASSERT_EQ(skel->bss->percpu_val, skel->bss->cpu + 1, "percpu_val");
+       ASSERT_EQ(skel->bss->percpu_output, 100, "percpu_output");
+out:
+       free(percpu_valbuf);
+       for_each_hash_map_elem__destroy(skel);
+}
+
+void test_for_each(void)
+{
+       if (test__start_subtest("hash_map"))
+               test_hash_map();
+}

[...]

Quoted 10 lines
tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c new file +10 -0
+
+__u32 cpu = 0;
+__u32 percpu_called = 0;
+__u32 percpu_key = 0;
+__u64 percpu_val = 0;
+
+static __u64
+check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val,
+                 struct callback_ctx *data)
+{
Quoted 8 lines

tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c line 61

Quoted 2 lines
tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c new file +2 -0
+       percpu_called++;
+       cpu = bpf_get_smp_processor_id();

It's a bit counter-intuitive (at least I was confused initially) that for a per-cpu array for_each() will iterate only current CPU's elements. I think it's worthwhile to emphasize this in bpf_for_each_map_elem() documentation (at least), and call out in corresponding patches adding per-cpu array/hash iteration support.

Right. Will emphasize this in uapi bpf.h and also comments in the code.

Quoted 11 lines
Quoted 8 lines
tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c new file +7 -0
+       percpu_key = *key;
+       percpu_val = *val;
+
+       bpf_for_each_map_elem(&hashmap, check_hash_elem, data, 0);
+       return 0;
+}
+

[...]

#24 lore raw

PATCH 8/8 selftests/bpf: add arraymap test for bpf_for_each_map_elem() helper

In reply to Yonghong Song

A test is added for arraymap and percpu arraymap. The test also exercises the early return for the helper which does not traverse all elements.

    $ ./test_progs -n 44
    #44/1 hash_map:OK
    #44/2 array_map:OK
    #44 for_each:OK
    Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED
---
 .../selftests/bpf/prog_tests/for_each.c       | 54 ++++++++++++++
 .../bpf/progs/for_each_array_map_elem.c       | 71 +++++++++++++++++++
 2 files changed, 125 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/for_each_array_map_elem.c
tools/testing/selftests/bpf/prog_tests/for_each.c +54 -0
index 7a399fbc89a4..58074212875b 100644
@@ -2,6 +2,7 @@
 /* Copyright (c) 2021 Facebook */
 #include <test_progs.h>
 #include "for_each_hash_map_elem.skel.h"
+#include "for_each_array_map_elem.skel.h"
 
 static int duration;
 
@@ -84,8 +85,61 @@ static void test_hash_map(void)
 	for_each_hash_map_elem__destroy(skel);
 }
 
+static void test_array_map(void)
+{
+	int i, arraymap_fd, percpu_map_fd, err;
+	struct for_each_array_map_elem *skel;
+	__u32 key, num_cpus, max_entries;
+	__u64 *percpu_valbuf = NULL;
+	__u64 val, expected_total;
+
+	skel = for_each_array_map_elem__open_and_load();
+	if (CHECK(!skel, "for_each_array_map_elem__open_and_load",
+		  "skeleton open_and_load failed\n"))
+		return;
+
+	arraymap_fd = bpf_map__fd(skel->maps.arraymap);
+	expected_total = 0;
+	max_entries = bpf_map__max_entries(skel->maps.arraymap);
+	for (i = 0; i < max_entries; i++) {
+		key = i;
+		val = i + 1;
+		/* skip the last iteration for expected total */
+		if (i != max_entries - 1)
+			expected_total += val;
+		err = bpf_map_update_elem(arraymap_fd, &key, &val, BPF_ANY);
+		if (CHECK(err, "map_update", "map_update failed\n"))
+			goto out;
+	}
+
+	num_cpus = bpf_num_possible_cpus();
+        percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);
+        percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
+        if (CHECK_FAIL(!percpu_valbuf))
+                goto out;
+
+	key = 0;
+        for (i = 0; i < num_cpus; i++)
+                percpu_valbuf[i] = i + 1;
+	err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
+	if (CHECK(err, "percpu_map_update", "map_update failed\n"))
+		goto out;
+
+	do_dummy_read(skel->progs.dump_task);
+
+	ASSERT_EQ(skel->bss->called, 1, "called");
+	ASSERT_EQ(skel->bss->arraymap_output, expected_total, "array_output");
+	ASSERT_EQ(skel->bss->cpu + 1, skel->bss->percpu_val, "percpu_val");
+
+out:
+	free(percpu_valbuf);
+	for_each_array_map_elem__destroy(skel);
+}
+
 void test_for_each(void)
 {
 	if (test__start_subtest("hash_map"))
 		test_hash_map();
+	if (test__start_subtest("array_map"))
+		test_array_map();
 }
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.24.1
#25 lore raw

Re: [PATCH bpf-next 8/8] selftests/bpf: add arraymap test for bpf_for_each_map_elem() helper

Quoted 17 lines · Yonghong Song

A test is added for arraymap and percpu arraymap. The test also exercises the early return for the helper which does not traverse all elements.

    $ ./test_progs -n 44
    #44/1 hash_map:OK
    #44/2 array_map:OK
    #44 for_each:OK
    Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED
---
 .../selftests/bpf/prog_tests/for_each.c       | 54 ++++++++++++++
 .../bpf/progs/for_each_array_map_elem.c       | 71 +++++++++++++++++++
 2 files changed, 125 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/for_each_array_map_elem.c

[...]

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +7 -0
+
+       arraymap_fd = bpf_map__fd(skel->maps.arraymap);
+       expected_total = 0;
+       max_entries = bpf_map__max_entries(skel->maps.arraymap);
+       for (i = 0; i < max_entries; i++) {
+               key = i;
+               val = i + 1;

tools/testing/selftests/bpf/prog_tests/for_each.c static void test_hash_map(void) · line 116

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +10 -0
+               /* skip the last iteration for expected total */
+               if (i != max_entries - 1)
+                       expected_total += val;
+               err = bpf_map_update_elem(arraymap_fd, &key, &val, BPF_ANY);
+               if (CHECK(err, "map_update", "map_update failed\n"))
+                       goto out;
+       }
+
+       num_cpus = bpf_num_possible_cpus();
+        percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);

formatting is off, please check with checkfile.pl

tools/testing/selftests/bpf/prog_tests/for_each.c static void test_hash_map(void) · line 118

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +2 -0
+        percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
+        if (CHECK_FAIL(!percpu_valbuf))

please don't use CHECK_FAIL, ASSERT_PTR_OK would work nicely here

tools/testing/selftests/bpf/prog_tests/for_each.c static void test_hash_map(void) · line 128

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +10 -0
+                goto out;
+
+       key = 0;
+        for (i = 0; i < num_cpus; i++)
+                percpu_valbuf[i] = i + 1;
+       err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
+       if (CHECK(err, "percpu_map_update", "map_update failed\n"))
+               goto out;
+
+       do_dummy_read(skel->progs.dump_task);

see previous patch, iter/tasks seems like an overkill for these tests

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +10 -0
+
+       ASSERT_EQ(skel->bss->called, 1, "called");
+       ASSERT_EQ(skel->bss->arraymap_output, expected_total, "array_output");
+       ASSERT_EQ(skel->bss->cpu + 1, skel->bss->percpu_val, "percpu_val");
+
+out:
+       free(percpu_valbuf);
+       for_each_array_map_elem__destroy(skel);
+}
+

[...]

Quoted 8 lines
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +8 -0
+SEC("iter/task")
+int dump_task(struct bpf_iter__task *ctx)
+{
+       struct seq_file *seq =  ctx->meta->seq;
+       struct task_struct *task = ctx->task;
+       struct callback_ctx data;
+
+       /* only call once */

tools/testing/selftests/bpf/progs/for_each_array_map_elem.c line 68

Quoted 10 lines
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +10 -0
+       if (called > 0)
+               return 0;
+
+       called++;
+
+       data.output = 0;
+       bpf_for_each_map_elem(&arraymap, check_array_elem, &data, 0);
+       arraymap_output = data.output;
+
+       bpf_for_each_map_elem(&percpu_map, check_percpu_elem, 0, 0);

nit: NULL, 0 ?

Quoted 6 lines
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +3 -1
+
+       return 0;
+}
--

2.24.1

#26 lore raw

Re: [PATCH bpf-next 8/8] selftests/bpf: add arraymap test for bpf_for_each_map_elem() helper

Quoted 32 lines · Andrii Nakryiko
Quoted 17 lines · Yonghong Song

A test is added for arraymap and percpu arraymap. The test also exercises the early return for the helper which does not traverse all elements.

     $ ./test_progs -n 44
     #44/1 hash_map:OK
     #44/2 array_map:OK
     #44 for_each:OK
     Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED
---
  .../selftests/bpf/prog_tests/for_each.c       | 54 ++++++++++++++
  .../bpf/progs/for_each_array_map_elem.c       | 71 +++++++++++++++++++
  2 files changed, 125 insertions(+)

create mode 100644 tools/testing/selftests/bpf/progs/for_each_array_map_elem.c

[...]

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +11 -0
+
+       arraymap_fd = bpf_map__fd(skel->maps.arraymap);
+       expected_total = 0;
+       max_entries = bpf_map__max_entries(skel->maps.arraymap);
+       for (i = 0; i < max_entries; i++) {
+               key = i;
+               val = i + 1;
+               /* skip the last iteration for expected total */
+               if (i != max_entries - 1)
+                       expected_total += val;
+               err = bpf_map_update_elem(arraymap_fd, &key, &val, BPF_ANY);
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/for_each.c static void test_hash_map(void) · line 116

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +6 -0
+               if (CHECK(err, "map_update", "map_update failed\n"))
+                       goto out;
+       }
+
+       num_cpus = bpf_num_possible_cpus();
+        percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);

formatting is off, please check with checkfile.pl

Sure. will do.

Quoted 6 lines

tools/testing/selftests/bpf/prog_tests/for_each.c static void test_hash_map(void) · line 118

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +2 -0
+        percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
+        if (CHECK_FAIL(!percpu_valbuf))

please don't use CHECK_FAIL, ASSERT_PTR_OK would work nicely here

Okay.

Quoted 13 lines

tools/testing/selftests/bpf/prog_tests/for_each.c static void test_hash_map(void) · line 128

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +10 -0
+                goto out;
+
+       key = 0;
+        for (i = 0; i < num_cpus; i++)
+                percpu_valbuf[i] = i + 1;
+       err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
+       if (CHECK(err, "percpu_map_update", "map_update failed\n"))
+               goto out;
+
+       do_dummy_read(skel->progs.dump_task);

see previous patch, iter/tasks seems like an overkill for these tests

Yes, will use bpf_prog_test_run() in v2.

Quoted 26 lines
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +10 -0
+
+       ASSERT_EQ(skel->bss->called, 1, "called");
+       ASSERT_EQ(skel->bss->arraymap_output, expected_total, "array_output");
+       ASSERT_EQ(skel->bss->cpu + 1, skel->bss->percpu_val, "percpu_val");
+
+out:
+       free(percpu_valbuf);
+       for_each_array_map_elem__destroy(skel);
+}
+

[...]

Quoted 12 lines
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +12 -0
+SEC("iter/task")
+int dump_task(struct bpf_iter__task *ctx)
+{
+       struct seq_file *seq =  ctx->meta->seq;
+       struct task_struct *task = ctx->task;
+       struct callback_ctx data;
+
+       /* only call once */
+       if (called > 0)
+               return 0;
+
+       called++;
Quoted 8 lines

tools/testing/selftests/bpf/progs/for_each_array_map_elem.c line 68

Quoted 6 lines
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +6 -0
+
+       data.output = 0;
+       bpf_for_each_map_elem(&arraymap, check_array_elem, &data, 0);
+       arraymap_output = data.output;
+
+       bpf_for_each_map_elem(&percpu_map, check_percpu_elem, 0, 0);

nit: NULL, 0 ?

We do not NULL defined in vmlinux.h or bpf_helpers.h. Hence I am using 0, I should at least use (void *)0 here, I think.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +3 -1
+
+       return 0;
+}
--

2.24.1

#27 lore raw

Re: [PATCH bpf-next 8/8] selftests/bpf: add arraymap test for bpf_for_each_map_elem() helper

Quoted 107 lines · Yonghong Song
Quoted 32 lines · Andrii Nakryiko
Quoted 17 lines · Yonghong Song

A test is added for arraymap and percpu arraymap. The test also exercises the early return for the helper which does not traverse all elements.

     $ ./test_progs -n 44
     #44/1 hash_map:OK
     #44/2 array_map:OK
     #44 for_each:OK
     Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED
---
  .../selftests/bpf/prog_tests/for_each.c       | 54 ++++++++++++++
  .../bpf/progs/for_each_array_map_elem.c       | 71 +++++++++++++++++++
  2 files changed, 125 insertions(+)

create mode 100644 tools/testing/selftests/bpf/progs/for_each_array_map_elem.c

[...]

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +11 -0
+
+       arraymap_fd = bpf_map__fd(skel->maps.arraymap);
+       expected_total = 0;
+       max_entries = bpf_map__max_entries(skel->maps.arraymap);
+       for (i = 0; i < max_entries; i++) {
+               key = i;
+               val = i + 1;
+               /* skip the last iteration for expected total */
+               if (i != max_entries - 1)
+                       expected_total += val;
+               err = bpf_map_update_elem(arraymap_fd, &key, &val, BPF_ANY);
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/for_each.c static void test_hash_map(void) · line 116

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +6 -0
+               if (CHECK(err, "map_update", "map_update failed\n"))
+                       goto out;
+       }
+
+       num_cpus = bpf_num_possible_cpus();
+        percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);

formatting is off, please check with checkfile.pl

Sure. will do.

Quoted 6 lines

tools/testing/selftests/bpf/prog_tests/for_each.c static void test_hash_map(void) · line 118

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +2 -0
+        percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
+        if (CHECK_FAIL(!percpu_valbuf))

please don't use CHECK_FAIL, ASSERT_PTR_OK would work nicely here

Okay.

Quoted 13 lines

tools/testing/selftests/bpf/prog_tests/for_each.c static void test_hash_map(void) · line 128

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +10 -0
+                goto out;
+
+       key = 0;
+        for (i = 0; i < num_cpus; i++)
+                percpu_valbuf[i] = i + 1;
+       err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
+       if (CHECK(err, "percpu_map_update", "map_update failed\n"))
+               goto out;
+
+       do_dummy_read(skel->progs.dump_task);

see previous patch, iter/tasks seems like an overkill for these tests

Yes, will use bpf_prog_test_run() in v2.

Quoted 26 lines
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/for_each.c +10 -0
+
+       ASSERT_EQ(skel->bss->called, 1, "called");
+       ASSERT_EQ(skel->bss->arraymap_output, expected_total, "array_output");
+       ASSERT_EQ(skel->bss->cpu + 1, skel->bss->percpu_val, "percpu_val");
+
+out:
+       free(percpu_valbuf);
+       for_each_array_map_elem__destroy(skel);
+}
+

[...]

Quoted 12 lines
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +12 -0
+SEC("iter/task")
+int dump_task(struct bpf_iter__task *ctx)
+{
+       struct seq_file *seq =  ctx->meta->seq;
+       struct task_struct *task = ctx->task;
+       struct callback_ctx data;
+
+       /* only call once */
+       if (called > 0)
+               return 0;
+
+       called++;
Quoted 8 lines

tools/testing/selftests/bpf/progs/for_each_array_map_elem.c line 68

Quoted 6 lines
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +6 -0
+
+       data.output = 0;
+       bpf_for_each_map_elem(&arraymap, check_array_elem, &data, 0);
+       arraymap_output = data.output;
+
+       bpf_for_each_map_elem(&percpu_map, check_percpu_elem, 0, 0);

nit: NULL, 0 ?

Quoted 2 lines

We do not NULL defined in vmlinux.h or bpf_helpers.h. Hence I am using 0, I should at least use (void *)0 here, I think.

yeah, this NULL problem is annoying. Let's just add NULL to vmlinux.h, you are not the first one to complain. We can do that separately from this patch set, of course.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c new file +3 -1
+
+       return 0;
+}
--

2.24.1