BACKPORT: bpf: add bpf_ktime_get_boot_ns()
authorMaciej Żenczykowski <maze@google.com>
Sun, 26 Apr 2020 16:15:25 +0000 (09:15 -0700)
committerCosmin Tanislav <demonsingur@gmail.com>
Thu, 16 May 2024 07:58:23 +0000 (10:58 +0300)
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.

Signed-off-by: Maciej Żenczykowski <maze@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
(cherry picked from commit 71d19214776e61b33da48f7c1b46e522c7f78221)
Change-Id: Ifd62c410dcc5112fd1a473a7e1f70231ca514bc0

include/linux/bpf.h
include/uapi/linux/bpf.h
kernel/bpf/core.c
kernel/bpf/helpers.c
kernel/trace/bpf_trace.c
net/core/filter.c
tools/include/uapi/linux/bpf.h

index 53501a20df6279890c1325b2dd6b3e6ceba614aa..c100e0cd592aa5e5c2744941887d6a7ca46870a4 100644 (file)
@@ -500,6 +500,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;
index 8e12be1b6651c0b742156ee208d2f567f8255beb..83dee4c4b0805449d96480427f364c01815d861d 100644 (file)
@@ -685,6 +685,78 @@ union bpf_attr {
        FN(redirect_map),               \
        FN(sk_redirect_map),            \
        FN(sock_map_update),            \
+       FN(xdp_adjust_meta),            \
+       FN(perf_event_read_value),      \
+       FN(perf_prog_read_value),       \
+       FN(getsockopt),                 \
+       FN(override_return),            \
+       FN(sock_ops_cb_flags_set),      \
+       FN(msg_redirect_map),           \
+       FN(msg_apply_bytes),            \
+       FN(msg_cork_bytes),             \
+       FN(msg_pull_data),              \
+       FN(bind),                       \
+       FN(xdp_adjust_tail),            \
+       FN(skb_get_xfrm_state),         \
+       FN(get_stack),                  \
+       FN(skb_load_bytes_relative),    \
+       FN(fib_lookup),                 \
+       FN(sock_hash_update),           \
+       FN(msg_redirect_hash),          \
+       FN(sk_redirect_hash),           \
+       FN(lwt_push_encap),             \
+       FN(lwt_seg6_store_bytes),       \
+       FN(lwt_seg6_adjust_srh),        \
+       FN(lwt_seg6_action),            \
+       FN(rc_repeat),                  \
+       FN(rc_keydown),                 \
+       FN(skb_cgroup_id),              \
+       FN(get_current_cgroup_id),      \
+       FN(get_local_storage),          \
+       FN(sk_select_reuseport),        \
+       FN(skb_ancestor_cgroup_id),     \
+       FN(sk_lookup_tcp),              \
+       FN(sk_lookup_udp),              \
+       FN(sk_release),                 \
+       FN(map_push_elem),              \
+       FN(map_pop_elem),               \
+       FN(map_peek_elem),              \
+       FN(msg_push_data),              \
+       FN(msg_pop_data),               \
+       FN(rc_pointer_rel),             \
+       FN(spin_lock),                  \
+       FN(spin_unlock),                \
+       FN(sk_fullsock),                \
+       FN(tcp_sock),                   \
+       FN(skb_ecn_set_ce),             \
+       FN(get_listener_sock),          \
+       FN(skc_lookup_tcp),             \
+       FN(tcp_check_syncookie),        \
+       FN(sysctl_get_name),            \
+       FN(sysctl_get_current_value),   \
+       FN(sysctl_get_new_value),       \
+       FN(sysctl_set_new_value),       \
+       FN(strtol),                     \
+       FN(strtoul),                    \
+       FN(sk_storage_get),             \
+       FN(sk_storage_delete),          \
+       FN(send_signal),                \
+       FN(tcp_gen_syncookie),          \
+       FN(skb_output),                 \
+       FN(probe_read_user),            \
+       FN(probe_read_kernel),          \
+       FN(probe_read_user_str),        \
+       FN(probe_read_kernel_str),      \
+       FN(tcp_send_ack),               \
+       FN(send_signal_thread),         \
+       FN(jiffies64),                  \
+       FN(read_branch_records),        \
+       FN(get_ns_current_pid_tgid),    \
+       FN(xdp_output),                 \
+       FN(get_netns_cookie),           \
+       FN(get_current_ancestor_cgroup_id),     \
+       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
index cc84110949aca206ea577bed2eacb1eaabb2f27c..c733e749aa4218a4a5e8dbaf47e3ed588bc23d75 100644 (file)
@@ -1646,6 +1646,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;
index f424da3f2805306d9ed1183d81c5910f1da479c3..aac170ea6c7e7ba9f5cbc35d5d2b30f96ab61bc7 100644 (file)
@@ -116,6 +116,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;
index 7a3df66056dedbc2a72add8250408dc20e352d2d..7ddfb056693b1268d0b9c4eb5a7725f837bc20a8 100644 (file)
@@ -481,6 +481,8 @@ static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
                return &bpf_probe_read_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:
index b19fbab3395ad8a19847a30c81a5e363b9e74889..183e281a48200f9077a5f5e425792e9db5c647b7 100644 (file)
@@ -3191,6 +3191,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;
        case BPF_FUNC_trace_printk:
                if (capable(CAP_SYS_ADMIN))
                        return bpf_get_trace_printk_proto();
index 30f2ce76b5170c8ea04f93a8f8f70f4633e4322b..000f99f85f9cfe8ba727475ad372f5bb060edf92 100644 (file)
@@ -639,6 +639,78 @@ union bpf_attr {
        FN(redirect_map),               \
        FN(sk_redirect_map),            \
        FN(sock_map_update),            \
+       FN(xdp_adjust_meta),            \
+       FN(perf_event_read_value),      \
+       FN(perf_prog_read_value),       \
+       FN(getsockopt),                 \
+       FN(override_return),            \
+       FN(sock_ops_cb_flags_set),      \
+       FN(msg_redirect_map),           \
+       FN(msg_apply_bytes),            \
+       FN(msg_cork_bytes),             \
+       FN(msg_pull_data),              \
+       FN(bind),                       \
+       FN(xdp_adjust_tail),            \
+       FN(skb_get_xfrm_state),         \
+       FN(get_stack),                  \
+       FN(skb_load_bytes_relative),    \
+       FN(fib_lookup),                 \
+       FN(sock_hash_update),           \
+       FN(msg_redirect_hash),          \
+       FN(sk_redirect_hash),           \
+       FN(lwt_push_encap),             \
+       FN(lwt_seg6_store_bytes),       \
+       FN(lwt_seg6_adjust_srh),        \
+       FN(lwt_seg6_action),            \
+       FN(rc_repeat),                  \
+       FN(rc_keydown),                 \
+       FN(skb_cgroup_id),              \
+       FN(get_current_cgroup_id),      \
+       FN(get_local_storage),          \
+       FN(sk_select_reuseport),        \
+       FN(skb_ancestor_cgroup_id),     \
+       FN(sk_lookup_tcp),              \
+       FN(sk_lookup_udp),              \
+       FN(sk_release),                 \
+       FN(map_push_elem),              \
+       FN(map_pop_elem),               \
+       FN(map_peek_elem),              \
+       FN(msg_push_data),              \
+       FN(msg_pop_data),               \
+       FN(rc_pointer_rel),             \
+       FN(spin_lock),                  \
+       FN(spin_unlock),                \
+       FN(sk_fullsock),                \
+       FN(tcp_sock),                   \
+       FN(skb_ecn_set_ce),             \
+       FN(get_listener_sock),          \
+       FN(skc_lookup_tcp),             \
+       FN(tcp_check_syncookie),        \
+       FN(sysctl_get_name),            \
+       FN(sysctl_get_current_value),   \
+       FN(sysctl_get_new_value),       \
+       FN(sysctl_set_new_value),       \
+       FN(strtol),                     \
+       FN(strtoul),                    \
+       FN(sk_storage_get),             \
+       FN(sk_storage_delete),          \
+       FN(send_signal),                \
+       FN(tcp_gen_syncookie),          \
+       FN(skb_output),                 \
+       FN(probe_read_user),            \
+       FN(probe_read_kernel),          \
+       FN(probe_read_user_str),        \
+       FN(probe_read_kernel_str),      \
+       FN(tcp_send_ack),               \
+       FN(send_signal_thread),         \
+       FN(jiffies64),                  \
+       FN(read_branch_records),        \
+       FN(get_ns_current_pid_tgid),    \
+       FN(xdp_output),                 \
+       FN(get_netns_cookie),           \
+       FN(get_current_ancestor_cgroup_id),     \
+       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