From patchwork Wed Sep 30 22:28:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pujari, Bimmy" X-Patchwork-Id: 11810429 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 81A75112C for ; Wed, 30 Sep 2020 22:28:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6EA7B20719 for ; Wed, 30 Sep 2020 22:28:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730949AbgI3W2i (ORCPT ); Wed, 30 Sep 2020 18:28:38 -0400 Received: from mga03.intel.com ([134.134.136.65]:40330 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730499AbgI3W2h (ORCPT ); Wed, 30 Sep 2020 18:28:37 -0400 IronPort-SDR: 7NeHopKkEp5hQ+vhllQbH6RgZ/dZEspXnf9KPTkt9c4obibgM5Y4q9GLQ33AP32xImtUUEjhgB BF+0d38LPVQQ== X-IronPort-AV: E=McAfee;i="6000,8403,9760"; a="162616455" X-IronPort-AV: E=Sophos;i="5.77,322,1596524400"; d="scan'208";a="162616455" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2020 15:28:33 -0700 IronPort-SDR: 8+K8LH92PdQEaTc38Hijo2GusyCw8SfgFNt2BsDUbn1G12jdLymjvHuSJMLAjuTVuBRBnjcTiV diTnzWBPolvw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.77,322,1596524400"; d="scan'208";a="351685057" Received: from bpujari-bxdsw.sc.intel.com ([10.232.14.242]) by orsmga007.jf.intel.com with ESMTP; 30 Sep 2020 15:28:26 -0700 From: bimmy.pujari@intel.com To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, mchehab@kernel.org, ast@kernel.org, daniel@iogearbox.net, kafai@fb.com, maze@google.com, bimmy.pujari@intel.com, ashkan.nikravesh@intel.com, Daniel.A.Alvarez@intel.com Subject: [PATCH bpf-next v6 1/2] bpf: Add bpf_ktime_get_real_ns Date: Wed, 30 Sep 2020 15:28:48 -0700 Message-Id: <20200930222848.14254-1-bimmy.pujari@intel.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org From: Bimmy Pujari 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. Signed-off-by: Ashkan Nikravesh Signed-off-by: Bimmy Pujari Reported-by: kernel test robot --- 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(+) diff --git a/drivers/media/rc/bpf-lirc.c b/drivers/media/rc/bpf-lirc.c index 3fe3edd80876..82b184e02248 100644 --- a/drivers/media/rc/bpf-lirc.c +++ b/drivers/media/rc/bpf-lirc.c @@ -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: diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 50e5c4b52bd1..01866d714438 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -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; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 1f17c6752deb..f944d9a14137 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -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 diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index cda674f1392f..7fb353ccb8ae 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -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; diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index e825441781ab..0fefcd076d7b 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -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: diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index e118a83439c3..1ee1c29cb711 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -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: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 1f17c6752deb..f944d9a14137 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -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 From patchwork Wed Sep 30 22:29:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pujari, Bimmy" X-Patchwork-Id: 11810431 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 40E066CA for ; Wed, 30 Sep 2020 22:29:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2E7B82076A for ; Wed, 30 Sep 2020 22:29:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730577AbgI3W3P (ORCPT ); Wed, 30 Sep 2020 18:29:15 -0400 Received: from mga17.intel.com ([192.55.52.151]:23089 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726992AbgI3W3P (ORCPT ); Wed, 30 Sep 2020 18:29:15 -0400 IronPort-SDR: bzNfuRTFMVh3iPW2a7VEyJyR1n9uNUcPbSWvVxzMk0MyVCdrWGWNt91wh7rPncjdag10CHbs9j nwKapXI8Ry/Q== X-IronPort-AV: E=McAfee;i="6000,8403,9760"; a="142570731" X-IronPort-AV: E=Sophos;i="5.77,322,1596524400"; d="scan'208";a="142570731" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2020 15:29:12 -0700 IronPort-SDR: Ms8uk+SalYWX/prYfZUjetUdfT/jWltAdrKf9xJ61Pe43ZADP/rLFSQwWhDZP996jWiSbyUiuq /Zzn8tfDDd6Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.77,322,1596524400"; d="scan'208";a="339269049" Received: from bpujari-bxdsw.sc.intel.com ([10.232.14.242]) by fmsmga004.fm.intel.com with ESMTP; 30 Sep 2020 15:29:10 -0700 From: bimmy.pujari@intel.com To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, mchehab@kernel.org, ast@kernel.org, daniel@iogearbox.net, kafai@fb.com, maze@google.com, bimmy.pujari@intel.com, ashkan.nikravesh@intel.com, Daniel.A.Alvarez@intel.com Subject: [PATCH bpf-next v6 2/2] selftests/bpf: Selftest for real time helper Date: Wed, 30 Sep 2020 15:29:30 -0700 Message-Id: <20200930222930.14316-1-bimmy.pujari@intel.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org From: Bimmy Pujari Add test validating that bpf_ktime_get_real_ns works fine. Signed-off-by: Bimmy Pujari --- .../selftests/bpf/prog_tests/ktime_real.c | 42 +++++++++++++++++++ .../bpf/progs/test_ktime_get_real_ns.c | 36 ++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/ktime_real.c create mode 100644 tools/testing/selftests/bpf/progs/test_ktime_get_real_ns.c diff --git a/tools/testing/selftests/bpf/prog_tests/ktime_real.c b/tools/testing/selftests/bpf/prog_tests/ktime_real.c new file mode 100644 index 000000000000..85235f2786b2 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/ktime_real.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +static void *time_thread(void *arg) +{ + __u32 duration, retval; + int err, prog_fd = *(u32 *) arg; + + err = bpf_prog_test_run(prog_fd, 10000, &pkt_v4, sizeof(pkt_v4), + NULL, NULL, &retval, &duration); + CHECK(err || retval, "", + "err %d errno %d retval %d duration %d\n", + err, errno, retval, duration); + pthread_exit(arg); +} + +void test_ktime_real(void) +{ + const char *file = "./test_ktime_get_real_ns.o"; + struct bpf_object *obj = NULL; + pthread_t thread_id; + int prog_fd; + int err = 0; + void *ret; + + err = bpf_prog_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd); + if (CHECK_FAIL(err)) { + printf("test_ktime_get_real_ns:bpf_prog_load errno %d\n", errno); + goto close_prog; + } + + if (CHECK_FAIL(pthread_create(&thread_id, NULL, + &time_thread, &prog_fd))) + goto close_prog; + + if (CHECK_FAIL(pthread_join(thread_id, &ret) || + ret != (void *)&prog_fd)) + goto close_prog; +close_prog: + bpf_object__close(obj); +} diff --git a/tools/testing/selftests/bpf/progs/test_ktime_get_real_ns.c b/tools/testing/selftests/bpf/progs/test_ktime_get_real_ns.c new file mode 100644 index 000000000000..37132c59de61 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_ktime_get_real_ns.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, int); + __type(value, unsigned long long); +} time_map SEC(".maps"); + +SEC("realtime_helper") +int realtime_helper_test(struct __sk_buff *skb) +{ + unsigned long long *lasttime; + unsigned long long curtime; + int key = 0; + int err = 0; + + lasttime = bpf_map_lookup_elem(&time_map, &key); + if (!lasttime) + goto err; + + curtime = bpf_ktime_get_real_ns(); + if (curtime <= *lasttime) { + err = 1; + goto err; + } + *lasttime = curtime; + +err: + return err; +} + +char _license[] SEC("license") = "GPL";