Re: [PATCH bpf-next v3 1/7] bpf: Allow struct argument in trampoline based programs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 8/30/22 5:11 AM, Jiri Olsa wrote:
On Sat, Aug 27, 2022 at 07:54:43PM -0700, Yonghong Song wrote:

SNIP

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 903719b89238..4a081bfb4c8a 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -5328,6 +5328,31 @@ static bool is_int_ptr(struct btf *btf, const struct btf_type *t)
  	return btf_type_is_int(t);
  }
+static u32 get_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto,
+			   int off)
+{
+	const struct btf_param *args;
+	const struct btf_type *t;
+	u32 offset = 0, nr_args;
+	int i;
+
+	nr_args = btf_type_vlen(func_proto);
+	args = (const struct btf_param *)(func_proto + 1);
+	for (i = 0; i < nr_args; i++) {
+		t = btf_type_skip_modifiers(btf, args[i].type, NULL);
+		offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8);
+		if (off < offset)
+			return i;
+	}
+
+	t = btf_type_skip_modifiers(btf, func_proto->type, NULL);
+	offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8);
+	if (off < offset)
+		return nr_args;
+
+	return nr_args + 1;
+}
+
  bool btf_ctx_access(int off, int size, enum bpf_access_type type,
  		    const struct bpf_prog *prog,
  		    struct bpf_insn_access_aux *info)
@@ -5347,7 +5372,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
  			tname, off);
  		return false;
  	}
-	arg = off / 8;
+	arg = t == NULL ? (off / 8) :  get_ctx_arg_idx(btf, t, off);

is the t == NULL check needed? we relied on t being defined later in the code

t is defined earlier in
   const struct btf_type *t = prog->aux->attach_func_proto;
so we should be fine.

I guess I will fold
	if (!t)
		return off / 8;
inside get_ctx_arg_idx() to make interface cleaner.
Will do this in the next revision.


jirka

  	args = (const struct btf_param *)(t + 1);
  	/* if (t == NULL) Fall back to default BPF prog with
  	 * MAX_BPF_FUNC_REG_ARGS u64 arguments.
@@ -5417,7 +5442,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
  	/* skip modifiers */
  	while (btf_type_is_modifier(t))
  		t = btf_type_by_id(btf, t->type);
-	if (btf_type_is_small_int(t) || btf_is_any_enum(t))
+	if (btf_type_is_small_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t))
  		/* accessing a scalar */
  		return true;
  	if (!btf_type_is_ptr(t)) {
@@ -5881,7 +5906,7 @@ static int __get_type_size(struct btf *btf, u32 btf_id,
  	if (btf_type_is_ptr(t))
  		/* kernel size of pointer. Not BPF's size of pointer*/
  		return sizeof(void *);
-	if (btf_type_is_int(t) || btf_is_any_enum(t))
+	if (btf_type_is_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t))
  		return t->size;
  	return -EINVAL;

SNIP



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux