Commit 71d19214 authored by Maciej Żenczykowski's avatar Maciej Żenczykowski Committed by Alexei Starovoitov

bpf: add bpf_ktime_get_boot_ns()

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: default avatarMaciej Żenczykowski <maze@google.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 0a05861f
...@@ -103,6 +103,8 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -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; return &bpf_map_peek_elem_proto;
case BPF_FUNC_ktime_get_ns: case BPF_FUNC_ktime_get_ns:
return &bpf_ktime_get_ns_proto; 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: case BPF_FUNC_tail_call:
return &bpf_tail_call_proto; return &bpf_tail_call_proto;
case BPF_FUNC_get_prandom_u32: case BPF_FUNC_get_prandom_u32:
......
...@@ -1509,6 +1509,7 @@ extern const struct bpf_func_proto bpf_get_smp_processor_id_proto; ...@@ -1509,6 +1509,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_get_numa_node_id_proto;
extern const struct bpf_func_proto bpf_tail_call_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_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_pid_tgid_proto;
extern const struct bpf_func_proto bpf_get_current_uid_gid_proto; extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
extern const struct bpf_func_proto bpf_get_current_comm_proto; extern const struct bpf_func_proto bpf_get_current_comm_proto;
......
...@@ -652,6 +652,8 @@ union bpf_attr { ...@@ -652,6 +652,8 @@ union bpf_attr {
* u64 bpf_ktime_get_ns(void) * u64 bpf_ktime_get_ns(void)
* Description * Description
* Return the time elapsed since system boot, in nanoseconds. * Return the time elapsed since system boot, in nanoseconds.
* Does not include time the system was suspended.
* See: clock_gettime(CLOCK_MONOTONIC)
* Return * Return
* Current *ktime*. * Current *ktime*.
* *
...@@ -3025,6 +3027,14 @@ union bpf_attr { ...@@ -3025,6 +3027,14 @@ union bpf_attr {
* * **-EOPNOTSUPP** Unsupported operation, for example a * * **-EOPNOTSUPP** Unsupported operation, for example a
* call from outside of TC ingress. * call from outside of TC ingress.
* * **-ESOCKTNOSUPPORT** Socket type not supported (reuseport). * * **-ESOCKTNOSUPPORT** Socket type not supported (reuseport).
*
* 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*.
*/ */
#define __BPF_FUNC_MAPPER(FN) \ #define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \ FN(unspec), \
...@@ -3151,7 +3161,8 @@ union bpf_attr { ...@@ -3151,7 +3161,8 @@ union bpf_attr {
FN(xdp_output), \ FN(xdp_output), \
FN(get_netns_cookie), \ FN(get_netns_cookie), \
FN(get_current_ancestor_cgroup_id), \ 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 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
* function eBPF program intends to call * function eBPF program intends to call
......
...@@ -2156,6 +2156,7 @@ const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; ...@@ -2156,6 +2156,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_smp_processor_id_proto __weak;
const struct bpf_func_proto bpf_get_numa_node_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_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_pid_tgid_proto __weak;
const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
......
...@@ -155,6 +155,18 @@ const struct bpf_func_proto bpf_ktime_get_ns_proto = { ...@@ -155,6 +155,18 @@ const struct bpf_func_proto bpf_ktime_get_ns_proto = {
.ret_type = RET_INTEGER, .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) BPF_CALL_0(bpf_get_current_pid_tgid)
{ {
struct task_struct *task = current; struct task_struct *task = current;
...@@ -615,6 +627,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) ...@@ -615,6 +627,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
return &bpf_tail_call_proto; return &bpf_tail_call_proto;
case BPF_FUNC_ktime_get_ns: case BPF_FUNC_ktime_get_ns:
return &bpf_ktime_get_ns_proto; return &bpf_ktime_get_ns_proto;
case BPF_FUNC_ktime_get_boot_ns:
return &bpf_ktime_get_boot_ns_proto;
default: default:
break; break;
} }
......
...@@ -797,6 +797,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -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; return &bpf_map_peek_elem_proto;
case BPF_FUNC_ktime_get_ns: case BPF_FUNC_ktime_get_ns:
return &bpf_ktime_get_ns_proto; 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: case BPF_FUNC_tail_call:
return &bpf_tail_call_proto; return &bpf_tail_call_proto;
case BPF_FUNC_get_current_pid_tgid: case BPF_FUNC_get_current_pid_tgid:
......
...@@ -652,6 +652,8 @@ union bpf_attr { ...@@ -652,6 +652,8 @@ union bpf_attr {
* u64 bpf_ktime_get_ns(void) * u64 bpf_ktime_get_ns(void)
* Description * Description
* Return the time elapsed since system boot, in nanoseconds. * Return the time elapsed since system boot, in nanoseconds.
* Does not include time the system was suspended.
* See: clock_gettime(CLOCK_MONOTONIC)
* Return * Return
* Current *ktime*. * Current *ktime*.
* *
...@@ -3025,6 +3027,14 @@ union bpf_attr { ...@@ -3025,6 +3027,14 @@ union bpf_attr {
* * **-EOPNOTSUPP** Unsupported operation, for example a * * **-EOPNOTSUPP** Unsupported operation, for example a
* call from outside of TC ingress. * call from outside of TC ingress.
* * **-ESOCKTNOSUPPORT** Socket type not supported (reuseport). * * **-ESOCKTNOSUPPORT** Socket type not supported (reuseport).
*
* 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*.
*/ */
#define __BPF_FUNC_MAPPER(FN) \ #define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \ FN(unspec), \
...@@ -3151,7 +3161,8 @@ union bpf_attr { ...@@ -3151,7 +3161,8 @@ union bpf_attr {
FN(xdp_output), \ FN(xdp_output), \
FN(get_netns_cookie), \ FN(get_netns_cookie), \
FN(get_current_ancestor_cgroup_id), \ 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 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
* function eBPF program intends to call * function eBPF program intends to call
......
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