diff mbox series

[RESEND] bpf: Add support for absolute value BPF timers

Message ID 20230221151846.2218217-1-tero.kristo@linux.intel.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [RESEND] bpf: Add support for absolute value BPF timers | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1747 this patch: 1747
netdev/cc_maintainers warning 8 maintainers not CCed: john.fastabend@gmail.com sdf@google.com jolsa@kernel.org song@kernel.org martin.lau@linux.dev haoluo@google.com yhs@fb.com kpsingh@kernel.org
netdev/build_clang success Errors and warnings before: 162 this patch: 162
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1744 this patch: 1744
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 51 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 fail Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on s390x with gcc

Commit Message

Tero Kristo Feb. 21, 2023, 3:18 p.m. UTC
Add a new flag BPF_F_TIMER_ABS that can be passed to bpf_timer_start()
to start an absolute value timer instead of the default relative value.
This makes the timer expire at an exact point in time, instead of a time
with latencies and jitter induced by both the BPF and timer subsystems.
This is useful e.g. in certain time sensitive profiling cases, where we
need a timer to expire at an exact point in time.

Signed-off-by: Tero Kristo <tero.kristo@linux.intel.com>
---
 include/uapi/linux/bpf.h | 15 +++++++++++++++
 kernel/bpf/helpers.c     | 11 +++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

Comments

Alexei Starovoitov Feb. 23, 2023, 4:31 p.m. UTC | #1
On Tue, Feb 21, 2023 at 7:21 AM Tero Kristo <tero.kristo@linux.intel.com> wrote:
>
> Add a new flag BPF_F_TIMER_ABS that can be passed to bpf_timer_start()
> to start an absolute value timer instead of the default relative value.
> This makes the timer expire at an exact point in time, instead of a time
> with latencies and jitter induced by both the BPF and timer subsystems.
> This is useful e.g. in certain time sensitive profiling cases, where we
> need a timer to expire at an exact point in time.

"certain time sensitive profiling cases" is too vague.

Please precisely describe the use case.

> Signed-off-by: Tero Kristo <tero.kristo@linux.intel.com>
> ---
>  include/uapi/linux/bpf.h | 15 +++++++++++++++
>  kernel/bpf/helpers.c     | 11 +++++++++--
>  2 files changed, 24 insertions(+), 2 deletions(-)
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 464ca3f01fe7..7f5b71847984 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -4951,6 +4951,12 @@ union bpf_attr {
>   *             different maps if key/value layout matches across maps.
>   *             Every bpf_timer_set_callback() can have different callback_fn.
>   *
> + *             *flags* can be one of:
> + *
> + *             **BPF_F_TIMER_ABS**
> + *                     Start the timer in absolute expire value instead of the
> + *                     default relative one.
> + *
>   *     Return
>   *             0 on success.
>   *             **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier
> @@ -7050,4 +7056,13 @@ struct bpf_core_relo {
>         enum bpf_core_relo_kind kind;
>  };
>
> +/*
> + * Flags to control bpf_timer_start() behaviour.
> + *     - BPF_F_TIMER_ABS: Timeout passed is absolute time, by default it is
> + *       relative to current time.
> + */
> +enum {
> +       BPF_F_TIMER_ABS = (1ULL << 0),
> +};
> +
>  #endif /* _UAPI__LINUX_BPF_H__ */
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index af30c6cbd65d..924849d89828 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -1253,10 +1253,11 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
>  {
>         struct bpf_hrtimer *t;
>         int ret = 0;
> +       enum hrtimer_mode mode;
>
>         if (in_nmi())
>                 return -EOPNOTSUPP;
> -       if (flags)
> +       if (flags > BPF_F_TIMER_ABS)
>                 return -EINVAL;
>         __bpf_spin_lock_irqsave(&timer->lock);
>         t = timer->timer;
> @@ -1264,7 +1265,13 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
>                 ret = -EINVAL;
>                 goto out;
>         }
> -       hrtimer_start(&t->timer, ns_to_ktime(nsecs), HRTIMER_MODE_REL_SOFT);
> +
> +       if (flags & BPF_F_TIMER_ABS)
> +               mode = HRTIMER_MODE_ABS_SOFT;
> +       else
> +               mode = HRTIMER_MODE_REL_SOFT;
> +
> +       hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode);

The patch looks fine, but please add a selftest for new functionality
in the 2nd patch and resend them together.
Tero Kristo Feb. 24, 2023, 11:54 a.m. UTC | #2
On 23/02/2023 18:31, Alexei Starovoitov wrote:
> On Tue, Feb 21, 2023 at 7:21 AM Tero Kristo <tero.kristo@linux.intel.com> wrote:
>> Add a new flag BPF_F_TIMER_ABS that can be passed to bpf_timer_start()
>> to start an absolute value timer instead of the default relative value.
>> This makes the timer expire at an exact point in time, instead of a time
>> with latencies and jitter induced by both the BPF and timer subsystems.
>> This is useful e.g. in certain time sensitive profiling cases, where we
>> need a timer to expire at an exact point in time.
> "certain time sensitive profiling cases" is too vague.
>
> Please precisely describe the use case.
Ok, I'll do my best to update the changelog on that. There is an open 
source tool available that will benefit from this change so I can refer 
to that.
>
>> Signed-off-by: Tero Kristo <tero.kristo@linux.intel.com>
>> ---
>>   include/uapi/linux/bpf.h | 15 +++++++++++++++
>>   kernel/bpf/helpers.c     | 11 +++++++++--
>>   2 files changed, 24 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
>> index 464ca3f01fe7..7f5b71847984 100644
>> --- a/include/uapi/linux/bpf.h
>> +++ b/include/uapi/linux/bpf.h
>> @@ -4951,6 +4951,12 @@ union bpf_attr {
>>    *             different maps if key/value layout matches across maps.
>>    *             Every bpf_timer_set_callback() can have different callback_fn.
>>    *
>> + *             *flags* can be one of:
>> + *
>> + *             **BPF_F_TIMER_ABS**
>> + *                     Start the timer in absolute expire value instead of the
>> + *                     default relative one.
>> + *
>>    *     Return
>>    *             0 on success.
>>    *             **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier
>> @@ -7050,4 +7056,13 @@ struct bpf_core_relo {
>>          enum bpf_core_relo_kind kind;
>>   };
>>
>> +/*
>> + * Flags to control bpf_timer_start() behaviour.
>> + *     - BPF_F_TIMER_ABS: Timeout passed is absolute time, by default it is
>> + *       relative to current time.
>> + */
>> +enum {
>> +       BPF_F_TIMER_ABS = (1ULL << 0),
>> +};
>> +
>>   #endif /* _UAPI__LINUX_BPF_H__ */
>> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
>> index af30c6cbd65d..924849d89828 100644
>> --- a/kernel/bpf/helpers.c
>> +++ b/kernel/bpf/helpers.c
>> @@ -1253,10 +1253,11 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
>>   {
>>          struct bpf_hrtimer *t;
>>          int ret = 0;
>> +       enum hrtimer_mode mode;
>>
>>          if (in_nmi())
>>                  return -EOPNOTSUPP;
>> -       if (flags)
>> +       if (flags > BPF_F_TIMER_ABS)
>>                  return -EINVAL;
>>          __bpf_spin_lock_irqsave(&timer->lock);
>>          t = timer->timer;
>> @@ -1264,7 +1265,13 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
>>                  ret = -EINVAL;
>>                  goto out;
>>          }
>> -       hrtimer_start(&t->timer, ns_to_ktime(nsecs), HRTIMER_MODE_REL_SOFT);
>> +
>> +       if (flags & BPF_F_TIMER_ABS)
>> +               mode = HRTIMER_MODE_ABS_SOFT;
>> +       else
>> +               mode = HRTIMER_MODE_REL_SOFT;
>> +
>> +       hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode);
> The patch looks fine, but please add a selftest for new functionality
> in the 2nd patch and resend them together.

Ok, will look at that, thanks!

-Tero
diff mbox series

Patch

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 464ca3f01fe7..7f5b71847984 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -4951,6 +4951,12 @@  union bpf_attr {
  *		different maps if key/value layout matches across maps.
  *		Every bpf_timer_set_callback() can have different callback_fn.
  *
+ *		*flags* can be one of:
+ *
+ *		**BPF_F_TIMER_ABS**
+ *			Start the timer in absolute expire value instead of the
+ *			default relative one.
+ *
  *	Return
  *		0 on success.
  *		**-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier
@@ -7050,4 +7056,13 @@  struct bpf_core_relo {
 	enum bpf_core_relo_kind kind;
 };
 
+/*
+ * Flags to control bpf_timer_start() behaviour.
+ *     - BPF_F_TIMER_ABS: Timeout passed is absolute time, by default it is
+ *       relative to current time.
+ */
+enum {
+	BPF_F_TIMER_ABS = (1ULL << 0),
+};
+
 #endif /* _UAPI__LINUX_BPF_H__ */
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index af30c6cbd65d..924849d89828 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1253,10 +1253,11 @@  BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
 {
 	struct bpf_hrtimer *t;
 	int ret = 0;
+	enum hrtimer_mode mode;
 
 	if (in_nmi())
 		return -EOPNOTSUPP;
-	if (flags)
+	if (flags > BPF_F_TIMER_ABS)
 		return -EINVAL;
 	__bpf_spin_lock_irqsave(&timer->lock);
 	t = timer->timer;
@@ -1264,7 +1265,13 @@  BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
 		ret = -EINVAL;
 		goto out;
 	}
-	hrtimer_start(&t->timer, ns_to_ktime(nsecs), HRTIMER_MODE_REL_SOFT);
+
+	if (flags & BPF_F_TIMER_ABS)
+		mode = HRTIMER_MODE_ABS_SOFT;
+	else
+		mode = HRTIMER_MODE_REL_SOFT;
+
+	hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode);
 out:
 	__bpf_spin_unlock_irqrestore(&timer->lock);
 	return ret;