bpf: Add bpf_ktime_get_real_ns
Subsystems (9)
PATCH v5 1/1 bpf: Add bpf_ktime_get_real_ns
From: Bimmy Pujari <[email protected]>
The existing bpf helper functions to get timestamp return the time elapsed since system boot. This timestamp is not particularly useful where epoch timestamp is required or more than one server is involved and time sync is required. Instead, you want to use CLOCK_REALTIME, which provides epoch timestamp. Hence adding a new helper function bfp_ktime_get_real_ns() based around CLOCK_REALTIME.
--- drivers/media/rc/bpf-lirc.c | 2 ++ include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 8 ++++++++ kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 13 +++++++++++++ kernel/trace/bpf_trace.c | 2 ++ tools/include/uapi/linux/bpf.h | 8 ++++++++ 7 files changed, 35 insertions(+)
drivers/media/rc/bpf-lirc.c +2 -0
@@ -105,6 +105,8 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_ktime_get_ns_proto;case BPF_FUNC_ktime_get_boot_ns:return &bpf_ktime_get_boot_ns_proto; +case BPF_FUNC_ktime_get_real_ns: +return &bpf_ktime_get_real_ns_proto;case BPF_FUNC_tail_call:return &bpf_tail_call_proto;case BPF_FUNC_get_prandom_u32:
include/linux/bpf.h +1 -0
@@ -1757,6 +1757,7 @@ 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_ktime_get_real_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 +8 -0
@@ -3586,6 +3586,13 @@ union bpf_attr {* the data in *dst*. This is a wrapper of **copy_from_user**\ (). * Return * 0 on success, or a negative error in case of failure. +* +* u64 bpf_ktime_get_real_ns(void) +* Description +* Return the real time in nanoseconds. +* See: **clock_gettime**\ (**CLOCK_REALTIME**) +* Return +* Current *ktime*. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3737,6 +3744,7 @@ union bpf_attr {FN (inode_storage_delete), \FN (d_path), \FN (copy_from_user), \ +FN (ktime_get_real_ns), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/core.c +1 -0
@@ -2208,6 +2208,7 @@ 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_ktime_get_real_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 +13 -0
@@ -166,6 +166,17 @@ const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = { .gpl_only = false, .ret_type = RET_INTEGER, }; +BPF_CALL_0 (bpf_ktime_get_real_ns) +{ +/* NMI safe access to clock realtime */ +return ktime_get_real_fast_ns (); +} + +const struct bpf_func_proto bpf_ktime_get_real_ns_proto = { + .func = bpf_ktime_get_real_ns, + .gpl_only = false, + .ret_type =RET_INTEGER , +};BPF_CALL_0 (bpf_get_current_pid_tgid) {
@@ -657,6 +668,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_ktime_get_ns_proto;case BPF_FUNC_ktime_get_boot_ns:return &bpf_ktime_get_boot_ns_proto; +case BPF_FUNC_ktime_get_real_ns: +return &bpf_ktime_get_real_ns_proto;case BPF_FUNC_ringbuf_output:return &bpf_ringbuf_output_proto;case BPF_FUNC_ringbuf_reserve:
kernel/trace/bpf_trace.c +2 -0
@@ -1167,6 +1167,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_ktime_get_ns_proto;case BPF_FUNC_ktime_get_boot_ns:return &bpf_ktime_get_boot_ns_proto; +case BPF_FUNC_ktime_get_real_ns: +return &bpf_ktime_get_real_ns_proto;case BPF_FUNC_tail_call:return &bpf_tail_call_proto;case BPF_FUNC_get_current_pid_tgid:
tools/include/uapi/linux/bpf.h +8 -0
@@ -3586,6 +3586,13 @@ union bpf_attr {* the data in *dst*. This is a wrapper of **copy_from_user**\ (). * Return * 0 on success, or a negative error in case of failure. +* +* u64 bpf_ktime_get_real_ns(void) +* Description +* Return the real time in nanoseconds. +* See: **clock_gettime**\ (**CLOCK_REALTIME**) +* Return +* Current *ktime*. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3737,6 +3744,7 @@ union bpf_attr {FN (inode_storage_delete), \FN (d_path), \FN (copy_from_user), \ +FN (ktime_get_real_ns), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.17.1
include/uapi/linux/bpf.h union bpf_attr { · line 3593
Quoted 13 lines · On 9/24/20 4:07 PM, [email protected] wrote:
include/uapi/linux/bpf.h +5
-0@@ -3586,6 +3586,13 @@ union bpf_attr {* the data in *dst*. This is a wrapper of **copy_from_user**\ (). * Return * 0 on success, or a negative error in case of failure. +* +* u64 bpf_ktime_get_real_ns(void) +* Description +* Return the real time in nanoseconds. +* See: **clock_gettime**\ (**CLOCK_REALTIME**)
This should be a little more explicit -- something like "See the caveats regarding use of CLOCK_REALTIME in clock_gettime man page."
Quoted 13 lines
include/uapi/linux/bpf.h +3
-0+* Return +* Current *ktime*. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -3737,6 +3744,7 @@ union bpf_attr {FN (inode_storage_delete), \FN (d_path), \FN (copy_from_user), \ +FN (ktime_get_real_ns), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Daniel Borkmann
maintainerQuoted 17 lines · David Ahern
include/uapi/linux/bpf.h union bpf_attr { · line 3593
Quoted 13 lines · On 9/24/20 4:07 PM, [email protected] wrote:
include/uapi/linux/bpf.h +5
-0@@ -3586,6 +3586,13 @@ union bpf_attr {* the data in *dst*. This is a wrapper of **copy_from_user**\ (). * Return * 0 on success, or a negative error in case of failure. +* +* u64 bpf_ktime_get_real_ns(void) +* Description +* Return the real time in nanoseconds. +* See: **clock_gettime**\ (**CLOCK_REALTIME**) This should be a little more explicit -- something like "See the caveats regarding use of
CLOCK_REALTIMEinclock_gettimeman page."
+1, and also other feedback got ignored along the way [0]; please don't just resend several new revisions in the meantime when there were things that still need to be addressed. Thanks!
[0] https://lore.kernel.org/bpf/[email protected]/T/#ma2997c90e80329c457eb92e9592e185f27ce222f