netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bpf: Allow small structs to be type of function argument
@ 2020-06-16 17:35 Jiri Olsa
  2020-06-17 23:20 ` John Fastabend
  0 siblings, 1 reply; 12+ messages in thread
From: Jiri Olsa @ 2020-06-16 17:35 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann
  Cc: netdev, bpf, Yonghong Song, Martin KaFai Lau, Jakub Kicinski,
	David Miller, John Fastabend, Jesper Dangaard Brouer,
	Andrii Nakryiko, KP Singh, Masanori Misono

This way we can have trampoline on function
that has arguments with types like:

  kuid_t uid
  kgid_t gid

which unwind into small structs like:

  typedef struct {
        uid_t val;
  } kuid_t;

  typedef struct {
        gid_t val;
  } kgid_t;

And we can use them in bpftrace like:
(assuming d_path changes are in)

  # bpftrace -e 'lsm:path_chown { printf("uid %d, gid %d\n", args->uid, args->gid) }'
  Attaching 1 probe...
  uid 0, gid 0
  uid 1000, gid 1000
  ...

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 kernel/bpf/btf.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 58c9af1d4808..f8fee5833684 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -362,6 +362,14 @@ static bool btf_type_is_struct(const struct btf_type *t)
 	return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
 }
 
+/* type is struct and its size is within 8 bytes
+ * and it can be value of function argument
+ */
+static bool btf_type_is_struct_arg(const struct btf_type *t)
+{
+	return btf_type_is_struct(t) && (t->size <= sizeof(u64));
+}
+
 static bool __btf_type_is_struct(const struct btf_type *t)
 {
 	return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
@@ -3768,7 +3776,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_int(t) || btf_type_is_enum(t))
+	if (btf_type_is_int(t) || btf_type_is_enum(t) || btf_type_is_struct_arg(t))
 		/* accessing a scalar */
 		return true;
 	if (!btf_type_is_ptr(t)) {
@@ -4161,6 +4169,8 @@ static int __get_type_size(struct btf *btf, u32 btf_id,
 		return sizeof(void *);
 	if (btf_type_is_int(t) || btf_type_is_enum(t))
 		return t->size;
+	if (btf_type_is_struct_arg(t))
+		return t->size;
 	*bad_type = t;
 	return -EINVAL;
 }
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-06-19 18:56 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-16 17:35 [PATCH] bpf: Allow small structs to be type of function argument Jiri Olsa
2020-06-17 23:20 ` John Fastabend
2020-06-18 11:48   ` Jiri Olsa
2020-06-18 22:05     ` Alexei Starovoitov
2020-06-19  8:50       ` Jiri Olsa
2020-06-18 22:06     ` John Fastabend
2020-06-18 23:59       ` Andrii Nakryiko
2020-06-19  0:25         ` John Fastabend
2020-06-19  2:04           ` Alexei Starovoitov
2020-06-19  5:39             ` Yonghong Song
2020-06-19 17:44               ` John Fastabend
2020-06-19 18:56                 ` Yonghong Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).