bpf: Add BPF-helper for accessing CLOCK_TAI
authorJesper Dangaard Brouer <brouer@redhat.com>
Tue, 9 Aug 2022 06:08:02 +0000 (08:08 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 9 Aug 2022 16:47:13 +0000 (09:47 -0700)
Commit 3dc6ffae2da2 ("timekeeping: Introduce fast accessor to clock tai")
introduced a fast and NMI-safe accessor for CLOCK_TAI. Especially in time
sensitive networks (TSN), where all nodes are synchronized by Precision Time
Protocol (PTP), it's helpful to have the possibility to generate timestamps
based on CLOCK_TAI instead of CLOCK_MONOTONIC. With a BPF helper for TAI in
place, it becomes very convenient to correlate activity across different
machines in the network.

Use cases for such a BPF helper include functionalities such as Tx launch
time (e.g. ETF and TAPRIO Qdiscs) and timestamping.

Note: CLOCK_TAI is nothing new per se, only the NMI-safe variant of it is.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
[Kurt: Wrote changelog and renamed helper]
Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de>
Link: https://lore.kernel.org/r/20220809060803.5773-2-kurt@linutronix.de
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf.h
include/uapi/linux/bpf.h
kernel/bpf/core.c
kernel/bpf/helpers.c
tools/include/uapi/linux/bpf.h

index 20c26aed78962e43ffff7e3d6ec0c8ff99507219..a627a02cf8ab79fedf897066625b884d3ed151dd 100644 (file)
@@ -2349,6 +2349,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_tai_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;
index 534e33fb10293e252b87185c101641191dc80de5..7d1e2794d83ebd2b6d33ce46910fecb59807bb70 100644 (file)
@@ -5341,6 +5341,18 @@ union bpf_attr {
  *             **-EACCES** if the SYN cookie is not valid.
  *
  *             **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin.
+ *
+ * u64 bpf_ktime_get_tai_ns(void)
+ *     Description
+ *             A nonsettable system-wide clock derived from wall-clock time but
+ *             ignoring leap seconds.  This clock does not experience
+ *             discontinuities and backwards jumps caused by NTP inserting leap
+ *             seconds as CLOCK_REALTIME does.
+ *
+ *             See: **clock_gettime**\ (**CLOCK_TAI**)
+ *     Return
+ *             Current *ktime*.
+ *
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -5551,6 +5563,7 @@ union bpf_attr {
        FN(tcp_raw_gen_syncookie_ipv6), \
        FN(tcp_raw_check_syncookie_ipv4),       \
        FN(tcp_raw_check_syncookie_ipv6),       \
+       FN(ktime_get_tai_ns),           \
        /* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
index c1e10d088dbb7388c44b17a01711c0b5d505ddb4..639437f3692827e90aabd509f6cf171ad5044623 100644 (file)
@@ -2623,6 +2623,7 @@ 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_coarse_ns_proto __weak;
+const struct bpf_func_proto bpf_ktime_get_tai_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;
index 1f961f9982d276353035185024901672de569ed9..a95eb9fb01ff896ec525076d37a93d7baeff20cc 100644 (file)
@@ -198,6 +198,18 @@ const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto = {
        .ret_type       = RET_INTEGER,
 };
 
+BPF_CALL_0(bpf_ktime_get_tai_ns)
+{
+       /* NMI safe access to clock tai */
+       return ktime_get_tai_fast_ns();
+}
+
+const struct bpf_func_proto bpf_ktime_get_tai_ns_proto = {
+       .func           = bpf_ktime_get_tai_ns,
+       .gpl_only       = false,
+       .ret_type       = RET_INTEGER,
+};
+
 BPF_CALL_0(bpf_get_current_pid_tgid)
 {
        struct task_struct *task = current;
@@ -1617,6 +1629,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_tai_ns:
+               return &bpf_ktime_get_tai_ns_proto;
        case BPF_FUNC_ringbuf_output:
                return &bpf_ringbuf_output_proto;
        case BPF_FUNC_ringbuf_reserve:
index f58d58e1d54787fbd1f4b480813ffc9692e59873..e174ad28aeb7bffc3bb2e239db7cfa43eaea671b 100644 (file)
@@ -5341,6 +5341,18 @@ union bpf_attr {
  *             **-EACCES** if the SYN cookie is not valid.
  *
  *             **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin.
+ *
+ * u64 bpf_ktime_get_tai_ns(void)
+ *     Description
+ *             A nonsettable system-wide clock derived from wall-clock time but
+ *             ignoring leap seconds.  This clock does not experience
+ *             discontinuities and backwards jumps caused by NTP inserting leap
+ *             seconds as CLOCK_REALTIME does.
+ *
+ *             See: **clock_gettime**\ (**CLOCK_TAI**)
+ *     Return
+ *             Current *ktime*.
+ *
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -5551,6 +5563,7 @@ union bpf_attr {
        FN(tcp_raw_gen_syncookie_ipv6), \
        FN(tcp_raw_check_syncookie_ipv4),       \
        FN(tcp_raw_check_syncookie_ipv6),       \
+       FN(ktime_get_tai_ns),           \
        /* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper