Skip to contentSkip to conversation

bpf: Add bpf_ktime_get_real_ns

PATCH Superseded [email protected] 2 participants 2 messages Sep 30, 2020 – Oct 1, 2020

Subsystems (9)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/2 bpf: Add bpf_ktime_get_real_ns

From: Bimmy Pujari <[email protected]>

The existing bpf helper functions to get timestamp return the time elapsed since system boot. This timestamp is not particularly useful where epoch timestamp is required or more than one server is involved and time sync is required. Instead, you want to use CLOCK_REALTIME, which provides epoch timestamp. Hence adding a new helper bfp_ktime_get_real_ns() based around CLOCK_REALTIME.

---
 drivers/media/rc/bpf-lirc.c    |  2 ++
 include/linux/bpf.h            |  1 +
 include/uapi/linux/bpf.h       | 11 +++++++++++
 kernel/bpf/core.c              |  1 +
 kernel/bpf/helpers.c           | 14 ++++++++++++++
 kernel/trace/bpf_trace.c       |  2 ++
 tools/include/uapi/linux/bpf.h | 11 +++++++++++
 7 files changed, 42 insertions(+)
drivers/media/rc/bpf-lirc.c +2 -0
index 3fe3edd80876..82b184e02248 100644
@@ -105,6 +105,8 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_ktime_get_ns_proto;
 	case BPF_FUNC_ktime_get_boot_ns:
 		return &bpf_ktime_get_boot_ns_proto;
+	case BPF_FUNC_ktime_get_real_ns:
+		return &bpf_ktime_get_real_ns_proto
 	case BPF_FUNC_tail_call:
 		return &bpf_tail_call_proto;
 	case BPF_FUNC_get_prandom_u32:
include/linux/bpf.h +1 -0
index 50e5c4b52bd1..01866d714438 100644
@@ -1791,6 +1791,7 @@ 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_ktime_get_ns_proto;
 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
+extern const struct bpf_func_proto bpf_ktime_get_real_ns_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_comm_proto;
include/uapi/linux/bpf.h +11 -0
index 1f17c6752deb..f944d9a14137 100644
@@ -3665,6 +3665,16 @@ union bpf_attr {
  * 	Return
  * 		The helper returns **TC_ACT_REDIRECT** on success or
  * 		**TC_ACT_SHOT** on error.
+ *
+ * u64 bpf_ktime_get_real_ns(void)
+ *	Description
+ *		Return the real time since epoch in nanoseconds.
+ *		See: **clock_gettime**\ (**CLOCK_REALTIME**)
+ *
+ *		As REALCLOCK can jump around, this helper should not be used to
+ *		measure passage of time.
+ *	Return
+ *		Current *ktime*.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3820,6 +3830,7 @@ union bpf_attr {
 	FN(seq_printf_btf),		\
 	FN(skb_cgroup_classid),		\
 	FN(redirect_neigh),		\
+	FN(ktime_get_real_ns),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/core.c +1 -0
index cda674f1392f..7fb353ccb8ae 100644
@@ -2211,6 +2211,7 @@ 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_ktime_get_ns_proto __weak;
 const struct bpf_func_proto bpf_ktime_get_boot_ns_proto __weak;
+const struct bpf_func_proto bpf_ktime_get_real_ns_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;
kernel/bpf/helpers.c +14 -0
index e825441781ab..0fefcd076d7b 100644
@@ -167,6 +167,18 @@ const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
 	.ret_type	= RET_INTEGER,
 };
 
+BPF_CALL_0(bpf_ktime_get_real_ns)
+{
+	/* NMI safe access to clock realtime */
+	return ktime_get_real_fast_ns();
+}
+
+const struct bpf_func_proto bpf_ktime_get_real_ns_proto = {
+	.func		= bpf_ktime_get_real_ns,
+	.gpl_only	= false,
+	.ret_type	= RET_INTEGER,
+};
+
 BPF_CALL_0(bpf_get_current_pid_tgid)
 {
 	struct task_struct *task = current;
@@ -657,6 +669,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
 		return &bpf_ktime_get_ns_proto;
 	case BPF_FUNC_ktime_get_boot_ns:
 		return &bpf_ktime_get_boot_ns_proto;
+	case BPF_FUNC_ktime_get_real_ns:
+		return &bpf_ktime_get_real_ns_proto;
 	case BPF_FUNC_ringbuf_output:
 		return &bpf_ringbuf_output_proto;
 	case BPF_FUNC_ringbuf_reserve:
kernel/trace/bpf_trace.c +2 -0
index e118a83439c3..1ee1c29cb711 100644
@@ -1259,6 +1259,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_ktime_get_ns_proto;
 	case BPF_FUNC_ktime_get_boot_ns:
 		return &bpf_ktime_get_boot_ns_proto;
+	case BPF_FUNC_ktime_get_real_ns:
+		return &bpf_ktime_get_real_ns_proto;
 	case BPF_FUNC_tail_call:
 		return &bpf_tail_call_proto;
 	case BPF_FUNC_get_current_pid_tgid:
tools/include/uapi/linux/bpf.h +11 -0
index 1f17c6752deb..f944d9a14137 100644
@@ -3665,6 +3665,16 @@ union bpf_attr {
  * 	Return
  * 		The helper returns **TC_ACT_REDIRECT** on success or
  * 		**TC_ACT_SHOT** on error.
+ *
+ * u64 bpf_ktime_get_real_ns(void)
+ *	Description
+ *		Return the real time since epoch in nanoseconds.
+ *		See: **clock_gettime**\ (**CLOCK_REALTIME**)
+ *
+ *		As REALCLOCK can jump around, this helper should not be used to
+ *		measure passage of time.
+ *	Return
+ *		Current *ktime*.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3820,6 +3830,7 @@ union bpf_attr {
 	FN(seq_printf_btf),		\
 	FN(skb_cgroup_classid),		\
 	FN(redirect_neigh),		\
+	FN(ktime_get_real_ns),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.17.1
#1 lore raw
Thank you for the patch! Yet something to improve:

Hi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/bimmy-pujari-intel-com/bpf-Add-bpf_ktime_get_real_ns/20201001-063024
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: xtensa-allyesconfig (attached as .config)
compiler: xtensa-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/5b19ed9ce24c3c15a4da400e9c3b18aa31d6eb36
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review bimmy-pujari-intel-com/bpf-Add-bpf_ktime_get_real_ns/20201001-063024 git checkout 5b19ed9ce24c3c15a4da400e9c3b18aa31d6eb36
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=xtensa

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

drivers/media/rc/bpf-lirc.c: In function 'lirc_mode2_func_proto':

>> drivers/media/rc/bpf-lirc.c:109:38: error: expected ';' before 'case'
     109 |   return &bpf_ktime_get_real_ns_proto
         |                                      ^
         |                                      ;
     110 |  case BPF_FUNC_tail_call:
         |  ~~~~                                 

vim +109 drivers/media/rc/bpf-lirc.c

    81	
    82	static const struct bpf_func_proto *
    83	lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
    84	{
    85		switch (func_id) {
    86		case BPF_FUNC_rc_repeat:
    87			return &rc_repeat_proto;
    88		case BPF_FUNC_rc_keydown:
    89			return &rc_keydown_proto;
    90		case BPF_FUNC_rc_pointer_rel:
    91			return &rc_pointer_rel_proto;
    92		case BPF_FUNC_map_lookup_elem:
    93			return &bpf_map_lookup_elem_proto;
    94		case BPF_FUNC_map_update_elem:
    95			return &bpf_map_update_elem_proto;
    96		case BPF_FUNC_map_delete_elem:
    97			return &bpf_map_delete_elem_proto;
    98		case BPF_FUNC_map_push_elem:
    99			return &bpf_map_push_elem_proto;
   100		case BPF_FUNC_map_pop_elem:
   101			return &bpf_map_pop_elem_proto;
   102		case BPF_FUNC_map_peek_elem:
   103			return &bpf_map_peek_elem_proto;
   104		case BPF_FUNC_ktime_get_ns:
   105			return &bpf_ktime_get_ns_proto;
   106		case BPF_FUNC_ktime_get_boot_ns:
   107			return &bpf_ktime_get_boot_ns_proto;
   108		case BPF_FUNC_ktime_get_real_ns:
 > 109			return &bpf_ktime_get_real_ns_proto

110 case BPF_FUNC_tail_call:
111 return &bpf_tail_call_proto;
112 case BPF_FUNC_get_prandom_u32:
113 return &bpf_get_prandom_u32_proto;
114 case BPF_FUNC_trace_printk:
115 if (perfmon_capable())
116 return bpf_get_trace_printk_proto();
117 fallthrough;
118 default:
119 return NULL;
120 }
121 }
122

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org