public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] sched_ext: Use BTF_ID to resolve task_struct
@ 2024-10-16  2:41 Tianchen Ding
  2024-10-16 16:57 ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Tianchen Ding @ 2024-10-16  2:41 UTC (permalink / raw)
  To: linux-kernel; +Cc: Tejun Heo, David Vernet, Peter Zijlstra, bpf

Save the searching time during bpf_scx_init.

Signed-off-by: Tianchen Ding <dtcccc@linux.alibaba.com>
---
 kernel/sched/ext.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 609b9fb00d6f..1d11a96eefb8 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -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;
 }
-- 
2.39.3


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

* Re: [PATCH] sched_ext: Use BTF_ID to resolve task_struct
  2024-10-16  2:41 [PATCH] sched_ext: Use BTF_ID to resolve task_struct Tianchen Ding
@ 2024-10-16 16:57 ` Alexei Starovoitov
  2024-10-17  1:57   ` Tianchen Ding
  0 siblings, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2024-10-16 16:57 UTC (permalink / raw)
  To: Tianchen Ding; +Cc: LKML, Tejun Heo, David Vernet, Peter Zijlstra, bpf

On Tue, Oct 15, 2024 at 7:42 PM Tianchen Ding <dtcccc@linux.alibaba.com> wrote:
>
> Save the searching time during bpf_scx_init.
>
> Signed-off-by: Tianchen Ding <dtcccc@linux.alibaba.com>
> ---
>  kernel/sched/ext.c | 12 +++---------
>  1 file changed, 3 insertions(+), 9 deletions(-)
>
> diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
> index 609b9fb00d6f..1d11a96eefb8 100644
> --- a/kernel/sched/ext.c
> +++ b/kernel/sched/ext.c
> @@ -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]);

Good optimization, but it's also unnecessary.

btf_id is already in btf_tracing_ids[BTF_TRACING_TYPE_TASK].

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

* Re: [PATCH] sched_ext: Use BTF_ID to resolve task_struct
  2024-10-16 16:57 ` Alexei Starovoitov
@ 2024-10-17  1:57   ` Tianchen Ding
  2024-10-17  2:12     ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Tianchen Ding @ 2024-10-17  1:57 UTC (permalink / raw)
  To: Alexei Starovoitov; +Cc: LKML, Tejun Heo, David Vernet, Peter Zijlstra, bpf

On 2024/10/17 00:57, Alexei Starovoitov wrote:
> On Tue, Oct 15, 2024 at 7:42 PM Tianchen Ding <dtcccc@linux.alibaba.com> wrote:
>>
>> Save the searching time during bpf_scx_init.
>>
>> Signed-off-by: Tianchen Ding <dtcccc@linux.alibaba.com>
>> ---
>>   kernel/sched/ext.c | 12 +++---------
>>   1 file changed, 3 insertions(+), 9 deletions(-)
>>
>> diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
>> index 609b9fb00d6f..1d11a96eefb8 100644
>> --- a/kernel/sched/ext.c
>> +++ b/kernel/sched/ext.c
>> @@ -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]);
> 
> Good optimization, but it's also unnecessary.
> 
> 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.

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

* Re: [PATCH] sched_ext: Use BTF_ID to resolve task_struct
  2024-10-17  1:57   ` Tianchen Ding
@ 2024-10-17  2:12     ` Alexei Starovoitov
  0 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2024-10-17  2:12 UTC (permalink / raw)
  To: Tianchen Ding; +Cc: LKML, Tejun Heo, David Vernet, Peter Zijlstra, bpf

On Wed, Oct 16, 2024 at 6:57 PM Tianchen Ding <dtcccc@linux.alibaba.com> wrote:
>
> On 2024/10/17 00:57, Alexei Starovoitov wrote:
> > On Tue, Oct 15, 2024 at 7:42 PM Tianchen Ding <dtcccc@linux.alibaba.com> wrote:
> >>
> >> Save the searching time during bpf_scx_init.
> >>
> >> Signed-off-by: Tianchen Ding <dtcccc@linux.alibaba.com>
> >> ---
> >>   kernel/sched/ext.c | 12 +++---------
> >>   1 file changed, 3 insertions(+), 9 deletions(-)
> >>
> >> diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
> >> index 609b9fb00d6f..1d11a96eefb8 100644
> >> --- a/kernel/sched/ext.c
> >> +++ b/kernel/sched/ext.c
> >> @@ -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]);
> >
> > Good optimization, but it's also unnecessary.
> >
> > 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.

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

end of thread, other threads:[~2024-10-17  2:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-16  2:41 [PATCH] sched_ext: Use BTF_ID to resolve task_struct Tianchen Ding
2024-10-16 16:57 ` Alexei Starovoitov
2024-10-17  1:57   ` Tianchen Ding
2024-10-17  2:12     ` Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox