Skip to content

Commit d055126

Browse files
ubiqueborkmann
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]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent ea87ae8 commit d055126

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed

include/linux/bpf.h

+1
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

+11
Original file line numberDiff line numberDiff line change
@@ -3797,6 +3797,16 @@ union bpf_attr {
37973797
* is cleared if the flag is not specified.
37983798
* Return
37993799
* **-EINVAL** if invalid *flags* are passed, zero otherwise.
3800+
*
3801+
* u64 bpf_ktime_get_coarse_ns(void)
3802+
* Description
3803+
* Return a coarse-grained version of the time elapsed since
3804+
* system boot, in nanoseconds. Does not include time the system
3805+
* was suspended.
3806+
*
3807+
* See: **clock_gettime**\ (**CLOCK_MONOTONIC_COARSE**)
3808+
* Return
3809+
* Current *ktime*.
38003810
*/
38013811
#define __BPF_FUNC_MAPPER(FN) \
38023812
FN(unspec), \
@@ -3959,6 +3969,7 @@ union bpf_attr {
39593969
FN(task_storage_delete), \
39603970
FN(get_current_task_btf), \
39613971
FN(bprm_opts_set), \
3972+
FN(ktime_get_coarse_ns), \
39623973
/* */
39633974

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

kernel/bpf/core.c

+1
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

+13
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
167167
.ret_type = RET_INTEGER,
168168
};
169169

170+
BPF_CALL_0(bpf_ktime_get_coarse_ns)
171+
{
172+
return ktime_get_coarse_ns();
173+
}
174+
175+
const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto = {
176+
.func = bpf_ktime_get_coarse_ns,
177+
.gpl_only = false,
178+
.ret_type = RET_INTEGER,
179+
};
180+
170181
BPF_CALL_0(bpf_get_current_pid_tgid)
171182
{
172183
struct task_struct *task = current;
@@ -685,6 +696,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
685696
return &bpf_ktime_get_ns_proto;
686697
case BPF_FUNC_ktime_get_boot_ns:
687698
return &bpf_ktime_get_boot_ns_proto;
699+
case BPF_FUNC_ktime_get_coarse_ns:
700+
return &bpf_ktime_get_coarse_ns_proto;
688701
case BPF_FUNC_ringbuf_output:
689702
return &bpf_ringbuf_output_proto;
690703
case BPF_FUNC_ringbuf_reserve:

kernel/trace/bpf_trace.c

+2
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
12801280
return &bpf_ktime_get_ns_proto;
12811281
case BPF_FUNC_ktime_get_boot_ns:
12821282
return &bpf_ktime_get_boot_ns_proto;
1283+
case BPF_FUNC_ktime_get_coarse_ns:
1284+
return &bpf_ktime_get_coarse_ns_proto;
12831285
case BPF_FUNC_tail_call:
12841286
return &bpf_tail_call_proto;
12851287
case BPF_FUNC_get_current_pid_tgid:

tools/include/uapi/linux/bpf.h

+11
Original file line numberDiff line numberDiff line change
@@ -3797,6 +3797,16 @@ union bpf_attr {
37973797
* is cleared if the flag is not specified.
37983798
* Return
37993799
* **-EINVAL** if invalid *flags* are passed, zero otherwise.
3800+
*
3801+
* u64 bpf_ktime_get_coarse_ns(void)
3802+
* Description
3803+
* Return a coarse-grained version of the time elapsed since
3804+
* system boot, in nanoseconds. Does not include time the system
3805+
* was suspended.
3806+
*
3807+
* See: **clock_gettime**\ (**CLOCK_MONOTONIC_COARSE**)
3808+
* Return
3809+
* Current *ktime*.
38003810
*/
38013811
#define __BPF_FUNC_MAPPER(FN) \
38023812
FN(unspec), \
@@ -3959,6 +3969,7 @@ union bpf_attr {
39593969
FN(task_storage_delete), \
39603970
FN(get_current_task_btf), \
39613971
FN(bprm_opts_set), \
3972+
FN(ktime_get_coarse_ns), \
39623973
/* */
39633974

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

0 commit comments

Comments
 (0)