Skip to content

Commit 5576b99

Browse files
iamkafaiAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add BPF_FUNC_jiffies64
This patch adds a helper to read the 64bit jiffies. It will be used in a later patch to implement the bpf_cubic.c. The helper is inlined for jit_requested and 64 BITS_PER_LONG as the map_gen_lookup(). Other cases could be considered together with map_gen_lookup() if needed. Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 1b2fd38 commit 5576b99

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;
14141414
extern const struct bpf_func_proto bpf_strtol_proto;
14151415
extern const struct bpf_func_proto bpf_strtoul_proto;
14161416
extern const struct bpf_func_proto bpf_tcp_sock_proto;
1417+
extern const struct bpf_func_proto bpf_jiffies64_proto;
14171418

14181419
/* Shared helpers among cBPF and eBPF. */
14191420
void bpf_user_rnd_init_once(void);

include/uapi/linux/bpf.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -2886,6 +2886,12 @@ union bpf_attr {
28862886
* **-EPERM** if no permission to send the *sig*.
28872887
*
28882888
* **-EAGAIN** if bpf program can try again.
2889+
*
2890+
* u64 bpf_jiffies64(void)
2891+
* Description
2892+
* Obtain the 64bit jiffies
2893+
* Return
2894+
* The 64 bit jiffies
28892895
*/
28902896
#define __BPF_FUNC_MAPPER(FN) \
28912897
FN(unspec), \
@@ -3005,7 +3011,8 @@ union bpf_attr {
30053011
FN(probe_read_user_str), \
30063012
FN(probe_read_kernel_str), \
30073013
FN(tcp_send_ack), \
3008-
FN(send_signal_thread),
3014+
FN(send_signal_thread), \
3015+
FN(jiffies64),
30093016

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

kernel/bpf/core.c

+1
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,7 @@ const struct bpf_func_proto bpf_map_pop_elem_proto __weak;
21372137
const struct bpf_func_proto bpf_map_peek_elem_proto __weak;
21382138
const struct bpf_func_proto bpf_spin_lock_proto __weak;
21392139
const struct bpf_func_proto bpf_spin_unlock_proto __weak;
2140+
const struct bpf_func_proto bpf_jiffies64_proto __weak;
21402141

21412142
const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
21422143
const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;

kernel/bpf/helpers.c

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/uidgid.h>
1212
#include <linux/filter.h>
1313
#include <linux/ctype.h>
14+
#include <linux/jiffies.h>
1415

1516
#include "../../lib/kstrtox.h"
1617

@@ -312,6 +313,17 @@ void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
312313
preempt_enable();
313314
}
314315

316+
BPF_CALL_0(bpf_jiffies64)
317+
{
318+
return get_jiffies_64();
319+
}
320+
321+
const struct bpf_func_proto bpf_jiffies64_proto = {
322+
.func = bpf_jiffies64,
323+
.gpl_only = false,
324+
.ret_type = RET_INTEGER,
325+
};
326+
315327
#ifdef CONFIG_CGROUPS
316328
BPF_CALL_0(bpf_get_current_cgroup_id)
317329
{

kernel/bpf/verifier.c

+24
Original file line numberDiff line numberDiff line change
@@ -9445,6 +9445,30 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
94459445
goto patch_call_imm;
94469446
}
94479447

9448+
if (prog->jit_requested && BITS_PER_LONG == 64 &&
9449+
insn->imm == BPF_FUNC_jiffies64) {
9450+
struct bpf_insn ld_jiffies_addr[2] = {
9451+
BPF_LD_IMM64(BPF_REG_0,
9452+
(unsigned long)&jiffies),
9453+
};
9454+
9455+
insn_buf[0] = ld_jiffies_addr[0];
9456+
insn_buf[1] = ld_jiffies_addr[1];
9457+
insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0,
9458+
BPF_REG_0, 0);
9459+
cnt = 3;
9460+
9461+
new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
9462+
cnt);
9463+
if (!new_prog)
9464+
return -ENOMEM;
9465+
9466+
delta += cnt - 1;
9467+
env->prog = prog = new_prog;
9468+
insn = new_prog->insnsi + i + delta;
9469+
continue;
9470+
}
9471+
94489472
patch_call_imm:
94499473
fn = env->ops->get_func_proto(insn->imm, env->prog);
94509474
/* all functions that have prototype and verifier allowed

net/core/filter.c

+2
Original file line numberDiff line numberDiff line change
@@ -5923,6 +5923,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
59235923
return &bpf_spin_unlock_proto;
59245924
case BPF_FUNC_trace_printk:
59255925
return bpf_get_trace_printk_proto();
5926+
case BPF_FUNC_jiffies64:
5927+
return &bpf_jiffies64_proto;
59265928
default:
59275929
return NULL;
59285930
}

0 commit comments

Comments
 (0)