net: bpf: add bpf_ktime_get_boot_ns()
Subsystems (10)
PATCH 1/1 net: bpf: add bpf_ktime_get_boot_ns()
From: Maciej Żenczykowski <[email protected]>
On a device like a cellphone which is constantly suspending and resuming CLOCK_MONOTONIC is not particularly useful for keeping track of or reacting to external network events. Instead you want to use CLOCK_BOOTTIME.
Hence add bpf_ktime_get_boot_ns() as a mirror of bpf_ktime_get_ns() based around CLOCK_BOOTTIME instead of CLOCK_MONOTONIC.
--- drivers/media/rc/bpf-lirc.c | 2 ++ include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 13 ++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 12 ++++++++++++ kernel/trace/bpf_trace.c | 2 ++ net/core/filter.c | 2 ++ tools/include/uapi/linux/bpf.h | 13 ++++++++++++- 8 files changed, 44 insertions(+), 2 deletions(-)
drivers/media/rc/bpf-lirc.c +2 -0
@@ -103,6 +103,8 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_map_peek_elem_proto;case BPF_FUNC_ktime_get_ns:return &bpf_ktime_get_ns_proto; +case BPF_FUNC_ktime_get_boot_ns: +return &bpf_ktime_get_boot_ns_proto;case BPF_FUNC_tail_call:return &bpf_tail_call_proto;case BPF_FUNC_get_prandom_u32:
include/linux/bpf.h +1 -0
@@ -1502,6 +1502,7 @@ extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;extern const struct bpf_func_proto bpf_get_numa_node_id_proto;extern const struct bpf_func_proto bpf_tail_call_proto;extern const struct bpf_func_proto bpf_ktime_get_ns_proto; +extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;extern const struct bpf_func_proto bpf_get_current_comm_proto;
include/uapi/linux/bpf.h +12 -1
@@ -652,6 +652,16 @@ union bpf_attr {* u64 bpf_ktime_get_ns(void) * Description * Return the time elapsed since system boot, in nanoseconds. +* Does not include time the system was suspended. +* See: clock_gettime(CLOCK_MONOTONIC) +* Return +* Current *ktime*. +* +* u64 bpf_ktime_get_boot_ns(void) +* Description +* Return the time elapsed since system boot, in nanoseconds. +* Does include the time the system was suspended. +* See: clock_gettime(CLOCK_BOOTTIME) * Return * Current *ktime*. *
@@ -3151,7 +3161,8 @@ union bpf_attr {FN (xdp_output), \FN (get_netns_cookie), \FN (get_current_ancestor_cgroup_id), \ -FN (sk_assign), +FN (sk_assign), \ +FN (ktime_get_boot_ns),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
kernel/bpf/core.c +1 -0
@@ -2151,6 +2151,7 @@ const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;const struct bpf_func_proto bpf_get_smp_processor_id_proto__weak ;const struct bpf_func_proto bpf_get_numa_node_id_proto__weak ;const struct bpf_func_proto bpf_ktime_get_ns_proto__weak ; +const struct bpf_func_proto bpf_ktime_get_boot_ns_proto__weak ;const struct bpf_func_proto bpf_get_current_pid_tgid_proto__weak ;const struct bpf_func_proto bpf_get_current_uid_gid_proto__weak ;
kernel/bpf/helpers.c +12 -0
@@ -155,6 +155,18 @@ const struct bpf_func_proto bpf_ktime_get_ns_proto = { .ret_type = RET_INTEGER, }; +BPF_CALL_0 (bpf_ktime_get_boot_ns) +{ +/* NMI safe access to clock boottime */ +return ktime_get_boot_fast_ns (); +} + +const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = { + .func = bpf_ktime_get_boot_ns, + .gpl_only = false, + .ret_type =RET_INTEGER , +}; +BPF_CALL_0 (bpf_get_current_pid_tgid) {struct task_struct *task = current;
kernel/trace/bpf_trace.c +2 -0
@@ -797,6 +797,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_map_peek_elem_proto;case BPF_FUNC_ktime_get_ns:return &bpf_ktime_get_ns_proto; +case BPF_FUNC_ktime_get_boot_ns: +return &bpf_ktime_get_boot_ns_proto;case BPF_FUNC_tail_call:return &bpf_tail_call_proto;case BPF_FUNC_get_current_pid_tgid:
net/core/filter.c +2 -0
@@ -6009,6 +6009,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_tail_call_proto;case BPF_FUNC_ktime_get_ns:return &bpf_ktime_get_ns_proto; +case BPF_FUNC_ktime_get_boot_ns: +return &bpf_ktime_get_boot_ns_proto;default :break ; }
tools/include/uapi/linux/bpf.h +12 -1
@@ -652,6 +652,16 @@ union bpf_attr {* u64 bpf_ktime_get_ns(void) * Description * Return the time elapsed since system boot, in nanoseconds. +* Does not include time the system was suspended. +* See: clock_gettime(CLOCK_MONOTONIC) +* Return +* Current *ktime*. +* +* u64 bpf_ktime_get_boot_ns(void) +* Description +* Return the time elapsed since system boot, in nanoseconds. +* Does include the time the system was suspended. +* See: clock_gettime(CLOCK_BOOTTIME) * Return * Current *ktime*. *
@@ -3151,7 +3161,8 @@ union bpf_attr {FN (xdp_output), \FN (get_netns_cookie), \FN (get_current_ancestor_cgroup_id), \ -FN (sk_assign), +FN (sk_assign), \ +FN (ktime_get_boot_ns),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
Signature · 1 line
2.26.1.301.g55bc3eb7cb9-goog
Quoted 11 lines · Maciej Żenczykowski
From: Maciej Żenczykowski <[email protected]>
On a device like a cellphone which is constantly suspending and resuming
CLOCK_MONOTONICis not particularly useful for keeping track of or reacting to external network events. Instead you want to useCLOCK_BOOTTIME.Hence add
bpf_ktime_get_boot_ns()as a mirror ofbpf_ktime_get_ns()based aroundCLOCK_BOOTTIMEinstead ofCLOCK_MONOTONIC.Signed-off-by Maciej Żenczykowski <[email protected]>
...
net/core/filter.c bpf_base_func_proto(enum bpf_func_id func_id) · line 6016
Quoted 13 lines
net/core/filter.c +2
-0@@ -6009,6 +6009,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_tail_call_proto;case BPF_FUNC_ktime_get_ns:return &bpf_ktime_get_ns_proto; +case BPF_FUNC_ktime_get_boot_ns: +return &bpf_ktime_get_boot_ns_proto;default :break ; }
That part got moved into kernel/bpf/helpers.c in the mean time. I fixed it up and applied. Thanks
In the future please cc bpf@vger for all bpf related patches.
Quoted 31 lines · Alexei Starovoitov
Quoted 11 lines · Maciej Żenczykowski
From: Maciej Żenczykowski <[email protected]>
On a device like a cellphone which is constantly suspending and resuming
CLOCK_MONOTONICis not particularly useful for keeping track of or reacting to external network events. Instead you want to useCLOCK_BOOTTIME.Hence add
bpf_ktime_get_boot_ns()as a mirror ofbpf_ktime_get_ns()based aroundCLOCK_BOOTTIMEinstead ofCLOCK_MONOTONIC.Signed-off-by Maciej Żenczykowski <[email protected]>...
net/core/filter.c bpf_base_func_proto(enum bpf_func_id func_id) · line 6016
Quoted 13 lines
net/core/filter.c +2
-0@@ -6009,6 +6009,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_tail_call_proto;case BPF_FUNC_ktime_get_ns:return &bpf_ktime_get_ns_proto; +case BPF_FUNC_ktime_get_boot_ns: +return &bpf_ktime_get_boot_ns_proto;default :break ; }That part got moved into
kernel/bpf/helpers.cin the mean time. I fixed it up and applied. Thanks
Quoted 1 line
In the future please cc bpf@vger for all bpf related patches.
The order of comments for bpf_ktime_get_boot_ns
was also incorrect.
Most selftests were broken.
I fixed it up as well.
Quoted 3 lines
Quoted 3 lines
I fixed it up and applied. Thanks
In the future please cc bpf@vger for all bpf related patches.
Will do so.
Quoted 4 lines
The order of comments for
bpf_ktime_get_boot_ns
was also incorrect.
Most selftests were broken.
I fixed it up as well.
Thank you.