Skip to content

Commit aa8d228

Browse files
ubiquekernel-patches-bot
authored andcommitted
bpf: Add bpf_ktime_get_coarse_ns helper
The helper uses CLOCK_MONOTONIC_COARSE source of time that is less accurate but more performant. We have a BPF CGROUP_SKB firewall that supports event logging through bpf_perf_event_output(). Each event has a timestamp and currently we use bpf_ktime_get_ns() for it. Use of bpf_ktime_get_coarse_ns() saves ~15-20 ns in time required for event logging. bpf_ktime_get_ns(): EgressLogByRemoteEndpoint 113.82ns 8.79M bpf_ktime_get_coarse_ns(): EgressLogByRemoteEndpoint 95.40ns 10.48M Signed-off-by: Dmitrii Banshchikov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]>
1 parent 8b38697 commit aa8d228

File tree

6 files changed

+35
-0
lines changed

6 files changed

+35
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,7 @@ extern const struct bpf_func_proto bpf_copy_from_user_proto;
18421842
extern const struct bpf_func_proto bpf_snprintf_btf_proto;
18431843
extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
18441844
extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
1845+
extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
18451846

18461847
const struct bpf_func_proto *bpf_tracing_func_proto(
18471848
enum bpf_func_id func_id, const struct bpf_prog *prog);

include/uapi/linux/bpf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,14 @@ union bpf_attr {
37873787
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
37883788
* Return
37893789
* Pointer to the current task.
3790+
*
3791+
* u64 bpf_ktime_get_coarse_ns(void)
3792+
* Description
3793+
* Return a coarse-grained version of the time elapsed since system boot, in nanoseconds.
3794+
* Does not include time the system was suspended.
3795+
* See: **clock_gettime**\ (**CLOCK_MONOTONIC_COARSE**)
3796+
* Return
3797+
* Current *ktime*.
37903798
*/
37913799
#define __BPF_FUNC_MAPPER(FN) \
37923800
FN(unspec), \
@@ -3948,6 +3956,7 @@ union bpf_attr {
39483956
FN(task_storage_get), \
39493957
FN(task_storage_delete), \
39503958
FN(get_current_task_btf), \
3959+
FN(ktime_get_coarse_ns), \
39513960
/* */
39523961

39533962
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

kernel/bpf/core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,7 @@ const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
22112211
const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
22122212
const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
22132213
const struct bpf_func_proto bpf_ktime_get_boot_ns_proto __weak;
2214+
const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto __weak;
22142215

22152216
const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
22162217
const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;

kernel/bpf/helpers.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,17 @@ const struct bpf_func_proto bpf_this_cpu_ptr_proto = {
651651
.arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
652652
};
653653

654+
BPF_CALL_0(bpf_ktime_get_coarse_ns)
655+
{
656+
return ktime_get_coarse_ns();
657+
}
658+
659+
const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto = {
660+
.func = bpf_ktime_get_coarse_ns,
661+
.gpl_only = false,
662+
.ret_type = RET_INTEGER,
663+
};
664+
654665
const struct bpf_func_proto bpf_get_current_task_proto __weak;
655666
const struct bpf_func_proto bpf_probe_read_user_proto __weak;
656667
const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
@@ -695,6 +706,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
695706
return &bpf_ringbuf_discard_proto;
696707
case BPF_FUNC_ringbuf_query:
697708
return &bpf_ringbuf_query_proto;
709+
case BPF_FUNC_ktime_get_coarse_ns:
710+
return &bpf_ktime_get_coarse_ns_proto;
698711
default:
699712
break;
700713
}

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
13541354
return &bpf_per_cpu_ptr_proto;
13551355
case BPF_FUNC_bpf_this_cpu_ptr:
13561356
return &bpf_this_cpu_ptr_proto;
1357+
case BPF_FUNC_ktime_get_coarse_ns:
1358+
return &bpf_ktime_get_coarse_ns_proto;
13571359
default:
13581360
return NULL;
13591361
}

tools/include/uapi/linux/bpf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,14 @@ union bpf_attr {
37873787
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
37883788
* Return
37893789
* Pointer to the current task.
3790+
*
3791+
* u64 bpf_ktime_get_coarse_ns(void)
3792+
* Description
3793+
* Return a coarse-grained version of the time elapsed since system boot, in nanoseconds.
3794+
* Does not include time the system was suspended.
3795+
* See: **clock_gettime**\ (**CLOCK_MONOTONIC_COARSE**)
3796+
* Return
3797+
* Current *ktime*.
37903798
*/
37913799
#define __BPF_FUNC_MAPPER(FN) \
37923800
FN(unspec), \
@@ -3948,6 +3956,7 @@ union bpf_attr {
39483956
FN(task_storage_get), \
39493957
FN(task_storage_delete), \
39503958
FN(get_current_task_btf), \
3959+
FN(ktime_get_coarse_ns), \
39513960
/* */
39523961

39533962
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)