Skip to contentSkip to conversation

sched_ext: Use BTF_ID to resolve task_struct

PATCH Superseded Tianchen Ding 2 participants 4 messages Oct 16, 2024 – Oct 17, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_ext: Use BTF_ID to resolve task_struct

Save the searching time during bpf_scx_init.

---
 kernel/sched/ext.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
kernel/sched/ext.c +3 -9
index 609b9fb00d6f..1d11a96eefb8 100644
@@ -5343,7 +5343,7 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
 
 extern struct btf *btf_vmlinux;
 static const struct btf_type *task_struct_type;
-static u32 task_struct_type_id;
+BTF_ID_LIST_SINGLE(task_struct_btf_ids, struct, task_struct);
 
 static bool set_arg_maybe_null(const char *op, int arg_n, int off, int size,
 			       enum bpf_access_type type,
@@ -5395,7 +5395,7 @@ static bool set_arg_maybe_null(const char *op, int arg_n, int off, int size,
 		 */
 		info->reg_type = PTR_MAYBE_NULL | PTR_TO_BTF_ID | PTR_TRUSTED;
 		info->btf = btf_vmlinux;
-		info->btf_id = task_struct_type_id;
+		info->btf_id = task_struct_btf_ids[0];
 
 		return true;
 	}
@@ -5547,13 +5547,7 @@ static void bpf_scx_unreg(void *kdata, struct bpf_link *link)
 
 static int bpf_scx_init(struct btf *btf)
 {
-	s32 type_id;
-
-	type_id = btf_find_by_name_kind(btf, "task_struct", BTF_KIND_STRUCT);
-	if (type_id < 0)
-		return -EINVAL;
-	task_struct_type = btf_type_by_id(btf, type_id);
-	task_struct_type_id = type_id;
+	task_struct_type = btf_type_by_id(btf, task_struct_btf_ids[0]);
 
 	return 0;
 }
Signature · 1 line
2.39.3
#1 lore raw
Quoted 32 lines · Tianchen Ding

Save the searching time during bpf_scx_init.

---
 kernel/sched/ext.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
kernel/sched/ext.c +2 -2
index 609b9fb00d6f..1d11a96eefb8 100644
@@ -5343,7 +5343,7 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
 
 extern struct btf *btf_vmlinux;
 static const struct btf_type *task_struct_type;
-static u32 task_struct_type_id;
+BTF_ID_LIST_SINGLE(task_struct_btf_ids, struct, task_struct);
 
 static bool set_arg_maybe_null(const char *op, int arg_n, int off, int size,
                               enum bpf_access_type type,
@@ -5395,7 +5395,7 @@ static bool set_arg_maybe_null(const char *op, int arg_n, int off, int size,
                 */
                info->reg_type = PTR_MAYBE_NULL | PTR_TO_BTF_ID | PTR_TRUSTED;
                info->btf = btf_vmlinux;
-               info->btf_id = task_struct_type_id;
+               info->btf_id = task_struct_btf_ids[0];
 
                return true;
        }
@@ -5547,13 +5547,7 @@ static void bpf_scx_unreg(void *kdata, struct bpf_link *link)
 

kernel/sched/ext.c static void bpf_scx_unreg(void *kdata, struct bpf_link *link) · line 5550

Quoted 10 lines
kernel/sched/ext.c +1 -7
 static int bpf_scx_init(struct btf *btf)
 {
-       s32 type_id;
-
-       type_id = btf_find_by_name_kind(btf, "task_struct", BTF_KIND_STRUCT);
-       if (type_id < 0)
-               return -EINVAL;
-       task_struct_type = btf_type_by_id(btf, type_id);
-       task_struct_type_id = type_id;
+       task_struct_type = btf_type_by_id(btf, task_struct_btf_ids[0]);

Good optimization, but it's also unnecessary.

btf_id is already in btf_tracing_ids[BTF_TRACING_TYPE_TASK].

#2 lore raw
Quoted 46 lines · Alexei Starovoitov
Quoted 32 lines · Tianchen Ding

Save the searching time during bpf_scx_init.

---
  kernel/sched/ext.c | 12 +++---------
  1 file changed, 3 insertions(+), 9 deletions(-)
kernel/sched/ext.c +2 -2
index 609b9fb00d6f..1d11a96eefb8 100644
@@ -5343,7 +5343,7 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
 
  extern struct btf *btf_vmlinux;
  static const struct btf_type *task_struct_type;
-static u32 task_struct_type_id;
+BTF_ID_LIST_SINGLE(task_struct_btf_ids, struct, task_struct);
 
  static bool set_arg_maybe_null(const char *op, int arg_n, int off, int size,
                                enum bpf_access_type type,
@@ -5395,7 +5395,7 @@ static bool set_arg_maybe_null(const char *op, int arg_n, int off, int size,
                  */
                 info->reg_type = PTR_MAYBE_NULL | PTR_TO_BTF_ID | PTR_TRUSTED;
                 info->btf = btf_vmlinux;
-               info->btf_id = task_struct_type_id;
+               info->btf_id = task_struct_btf_ids[0];
 
                 return true;
         }
@@ -5547,13 +5547,7 @@ static void bpf_scx_unreg(void *kdata, struct bpf_link *link)
 

kernel/sched/ext.c static void bpf_scx_unreg(void *kdata, struct bpf_link *link) · line 5550

Quoted 10 lines
kernel/sched/ext.c +1 -7
  static int bpf_scx_init(struct btf *btf)
  {
-       s32 type_id;
-
-       type_id = btf_find_by_name_kind(btf, "task_struct", BTF_KIND_STRUCT);
-       if (type_id < 0)
-               return -EINVAL;
-       task_struct_type = btf_type_by_id(btf, type_id);
-       task_struct_type_id = type_id;
+       task_struct_type = btf_type_by_id(btf, task_struct_btf_ids[0]);

Good optimization, but it's also unnecessary.

Quoted 1 line

btf_id is already in btf_tracing_ids[BTF_TRACING_TYPE_TASK].

Get it. Thanks!

BTW, do you think we should add a zero check for btf_tracing_ids[BTF_TRACING_TYPE_TASK] here?
task_struct should always be valid. If something wrong, resolve_btfids will also throw a warning. I'm not sure whether to add a sanity check here.

#3 lore raw
Quoted 48 lines · Tianchen Ding
Quoted 46 lines · Alexei Starovoitov
Quoted 32 lines · Tianchen Ding

Save the searching time during bpf_scx_init.

---
  kernel/sched/ext.c | 12 +++---------
  1 file changed, 3 insertions(+), 9 deletions(-)
kernel/sched/ext.c +2 -2
index 609b9fb00d6f..1d11a96eefb8 100644
@@ -5343,7 +5343,7 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
 
  extern struct btf *btf_vmlinux;
  static const struct btf_type *task_struct_type;
-static u32 task_struct_type_id;
+BTF_ID_LIST_SINGLE(task_struct_btf_ids, struct, task_struct);
 
  static bool set_arg_maybe_null(const char *op, int arg_n, int off, int size,
                                enum bpf_access_type type,
@@ -5395,7 +5395,7 @@ static bool set_arg_maybe_null(const char *op, int arg_n, int off, int size,
                  */
                 info->reg_type = PTR_MAYBE_NULL | PTR_TO_BTF_ID | PTR_TRUSTED;
                 info->btf = btf_vmlinux;
-               info->btf_id = task_struct_type_id;
+               info->btf_id = task_struct_btf_ids[0];
 
                 return true;
         }
@@ -5547,13 +5547,7 @@ static void bpf_scx_unreg(void *kdata, struct bpf_link *link)
 

kernel/sched/ext.c static void bpf_scx_unreg(void *kdata, struct bpf_link *link) · line 5550

Quoted 10 lines
kernel/sched/ext.c +1 -7
  static int bpf_scx_init(struct btf *btf)
  {
-       s32 type_id;
-
-       type_id = btf_find_by_name_kind(btf, "task_struct", BTF_KIND_STRUCT);
-       if (type_id < 0)
-               return -EINVAL;
-       task_struct_type = btf_type_by_id(btf, type_id);
-       task_struct_type_id = type_id;
+       task_struct_type = btf_type_by_id(btf, task_struct_btf_ids[0]);

Good optimization, but it's also unnecessary.

Quoted 8 lines
Quoted 1 line

btf_id is already in btf_tracing_ids[BTF_TRACING_TYPE_TASK].

Get it. Thanks!

BTW, do you think we should add a zero check for btf_tracing_ids[BTF_TRACING_TYPE_TASK] here?
task_struct should always be valid. If something wrong, resolve_btfids will also throw a warning. I'm not sure whether to add a sanity check here.

Definitely shouldn't add run-time checks. Build check may work, but feels overkill at this point.