Skip to content

Commit 71d1921

Browse files
zenczykowskiAlexei Starovoitov
authored andcommitted
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: Maciej Żenczykowski <maze@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 0a05861 commit 71d1921

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

drivers/media/rc/bpf-lirc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
103103
return &bpf_map_peek_elem_proto;
104104
case BPF_FUNC_ktime_get_ns:
105105
return &bpf_ktime_get_ns_proto;
106+
case BPF_FUNC_ktime_get_boot_ns:
107+
return &bpf_ktime_get_boot_ns_proto;
106108
case BPF_FUNC_tail_call:
107109
return &bpf_tail_call_proto;
108110
case BPF_FUNC_get_prandom_u32:

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,7 @@ extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
15091509
extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
15101510
extern const struct bpf_func_proto bpf_tail_call_proto;
15111511
extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
1512+
extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
15121513
extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
15131514
extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
15141515
extern const struct bpf_func_proto bpf_get_current_comm_proto;

include/uapi/linux/bpf.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ union bpf_attr {
652652
* u64 bpf_ktime_get_ns(void)
653653
* Description
654654
* Return the time elapsed since system boot, in nanoseconds.
655+
* Does not include time the system was suspended.
656+
* See: clock_gettime(CLOCK_MONOTONIC)
655657
* Return
656658
* Current *ktime*.
657659
*
@@ -3025,6 +3027,14 @@ union bpf_attr {
30253027
* * **-EOPNOTSUPP** Unsupported operation, for example a
30263028
* call from outside of TC ingress.
30273029
* * **-ESOCKTNOSUPPORT** Socket type not supported (reuseport).
3030+
*
3031+
* u64 bpf_ktime_get_boot_ns(void)
3032+
* Description
3033+
* Return the time elapsed since system boot, in nanoseconds.
3034+
* Does include the time the system was suspended.
3035+
* See: clock_gettime(CLOCK_BOOTTIME)
3036+
* Return
3037+
* Current *ktime*.
30283038
*/
30293039
#define __BPF_FUNC_MAPPER(FN) \
30303040
FN(unspec), \
@@ -3151,7 +3161,8 @@ union bpf_attr {
31513161
FN(xdp_output), \
31523162
FN(get_netns_cookie), \
31533163
FN(get_current_ancestor_cgroup_id), \
3154-
FN(sk_assign),
3164+
FN(sk_assign), \
3165+
FN(ktime_get_boot_ns),
31553166

31563167
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
31573168
* function eBPF program intends to call

kernel/bpf/core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,7 @@ const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
21562156
const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
21572157
const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
21582158
const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
2159+
const struct bpf_func_proto bpf_ktime_get_boot_ns_proto __weak;
21592160

21602161
const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
21612162
const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;

kernel/bpf/helpers.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ const struct bpf_func_proto bpf_ktime_get_ns_proto = {
155155
.ret_type = RET_INTEGER,
156156
};
157157

158+
BPF_CALL_0(bpf_ktime_get_boot_ns)
159+
{
160+
/* NMI safe access to clock boottime */
161+
return ktime_get_boot_fast_ns();
162+
}
163+
164+
const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
165+
.func = bpf_ktime_get_boot_ns,
166+
.gpl_only = false,
167+
.ret_type = RET_INTEGER,
168+
};
169+
158170
BPF_CALL_0(bpf_get_current_pid_tgid)
159171
{
160172
struct task_struct *task = current;
@@ -615,6 +627,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
615627
return &bpf_tail_call_proto;
616628
case BPF_FUNC_ktime_get_ns:
617629
return &bpf_ktime_get_ns_proto;
630+
case BPF_FUNC_ktime_get_boot_ns:
631+
return &bpf_ktime_get_boot_ns_proto;
618632
default:
619633
break;
620634
}

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
797797
return &bpf_map_peek_elem_proto;
798798
case BPF_FUNC_ktime_get_ns:
799799
return &bpf_ktime_get_ns_proto;
800+
case BPF_FUNC_ktime_get_boot_ns:
801+
return &bpf_ktime_get_boot_ns_proto;
800802
case BPF_FUNC_tail_call:
801803
return &bpf_tail_call_proto;
802804
case BPF_FUNC_get_current_pid_tgid:

tools/include/uapi/linux/bpf.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ union bpf_attr {
652652
* u64 bpf_ktime_get_ns(void)
653653
* Description
654654
* Return the time elapsed since system boot, in nanoseconds.
655+
* Does not include time the system was suspended.
656+
* See: clock_gettime(CLOCK_MONOTONIC)
655657
* Return
656658
* Current *ktime*.
657659
*
@@ -3025,6 +3027,14 @@ union bpf_attr {
30253027
* * **-EOPNOTSUPP** Unsupported operation, for example a
30263028
* call from outside of TC ingress.
30273029
* * **-ESOCKTNOSUPPORT** Socket type not supported (reuseport).
3030+
*
3031+
* u64 bpf_ktime_get_boot_ns(void)
3032+
* Description
3033+
* Return the time elapsed since system boot, in nanoseconds.
3034+
* Does include the time the system was suspended.
3035+
* See: clock_gettime(CLOCK_BOOTTIME)
3036+
* Return
3037+
* Current *ktime*.
30283038
*/
30293039
#define __BPF_FUNC_MAPPER(FN) \
30303040
FN(unspec), \
@@ -3151,7 +3161,8 @@ union bpf_attr {
31513161
FN(xdp_output), \
31523162
FN(get_netns_cookie), \
31533163
FN(get_current_ancestor_cgroup_id), \
3154-
FN(sk_assign),
3164+
FN(sk_assign), \
3165+
FN(ktime_get_boot_ns),
31553166

31563167
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
31573168
* function eBPF program intends to call

0 commit comments

Comments
 (0)