Commit f71f8530 authored by Tero Kristo's avatar Tero Kristo Committed by Alexei Starovoitov

bpf: Add support for absolute value BPF timers

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 induced by both the BPF and timer subsystems.
Suggested-by: default avatarArtem Bityutskiy <artem.bityutskiy@linux.intel.com>
Signed-off-by: default avatarTero Kristo <tero.kristo@linux.intel.com>
Link: https://lore.kernel.org/r/20230302114614.2985072-2-tero.kristo@linux.intel.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent ec97a76f
...@@ -4969,6 +4969,12 @@ union bpf_attr { ...@@ -4969,6 +4969,12 @@ union bpf_attr {
* different maps if key/value layout matches across maps. * different maps if key/value layout matches across maps.
* Every bpf_timer_set_callback() can have different callback_fn. * 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 * Return
* 0 on success. * 0 on success.
* **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier
...@@ -7097,4 +7103,13 @@ struct bpf_core_relo { ...@@ -7097,4 +7103,13 @@ struct bpf_core_relo {
enum bpf_core_relo_kind kind; 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__ */ #endif /* _UAPI__LINUX_BPF_H__ */
...@@ -1264,10 +1264,11 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla ...@@ -1264,10 +1264,11 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
{ {
struct bpf_hrtimer *t; struct bpf_hrtimer *t;
int ret = 0; int ret = 0;
enum hrtimer_mode mode;
if (in_nmi()) if (in_nmi())
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (flags) if (flags > BPF_F_TIMER_ABS)
return -EINVAL; return -EINVAL;
__bpf_spin_lock_irqsave(&timer->lock); __bpf_spin_lock_irqsave(&timer->lock);
t = timer->timer; t = timer->timer;
...@@ -1275,7 +1276,13 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla ...@@ -1275,7 +1276,13 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
ret = -EINVAL; ret = -EINVAL;
goto out; 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: out:
__bpf_spin_unlock_irqrestore(&timer->lock); __bpf_spin_unlock_irqrestore(&timer->lock);
return ret; return ret;
......
...@@ -4969,6 +4969,12 @@ union bpf_attr { ...@@ -4969,6 +4969,12 @@ union bpf_attr {
* different maps if key/value layout matches across maps. * different maps if key/value layout matches across maps.
* Every bpf_timer_set_callback() can have different callback_fn. * 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 * Return
* 0 on success. * 0 on success.
* **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier
...@@ -7097,4 +7103,13 @@ struct bpf_core_relo { ...@@ -7097,4 +7103,13 @@ struct bpf_core_relo {
enum bpf_core_relo_kind kind; 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__ */ #endif /* _UAPI__LINUX_BPF_H__ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment