linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: bpf: add bpf_ktime_get_boot_ns()
@ 2020-04-20 20:26 Maciej Żenczykowski
  2020-04-26 16:31 ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Maciej Żenczykowski @ 2020-04-20 20:26 UTC (permalink / raw)
  To: Maciej Żenczykowski, Alexei Starovoitov, Daniel Borkmann
  Cc: Linux Network Development Mailing List, linux-kernel, David S . Miller

From: Maciej Żenczykowski <maze@google.com>

On a device like a cellphone which is constantly suspending
and resuming CLOCK_MONOTONIC is not particularly useful for
keeping track of or reacting to external network events.
Instead you want to use CLOCK_BOOTTIME.

Hence add bpf_ktime_get_boot_ns() as a mirror of bpf_ktime_get_ns()
based around CLOCK_BOOTTIME instead of CLOCK_MONOTONIC.

Signed-off-by: Maciej Żenczykowski <maze@google.com>
---
 drivers/media/rc/bpf-lirc.c    |  2 ++
 include/linux/bpf.h            |  1 +
 include/uapi/linux/bpf.h       | 13 ++++++++++++-
 kernel/bpf/core.c              |  1 +
 kernel/bpf/helpers.c           | 12 ++++++++++++
 kernel/trace/bpf_trace.c       |  2 ++
 net/core/filter.c              |  2 ++
 tools/include/uapi/linux/bpf.h | 13 ++++++++++++-
 8 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/drivers/media/rc/bpf-lirc.c b/drivers/media/rc/bpf-lirc.c
index 0f3417d161b8..069c42f22a8c 100644
--- a/drivers/media/rc/bpf-lirc.c
+++ b/drivers/media/rc/bpf-lirc.c
@@ -103,6 +103,8 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_map_peek_elem_proto;
 	case BPF_FUNC_ktime_get_ns:
 		return &bpf_ktime_get_ns_proto;
+	case BPF_FUNC_ktime_get_boot_ns:
+		return &bpf_ktime_get_boot_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 fd2b2322412d..65217c52474d 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1502,6 +1502,7 @@ extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
 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_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 2e29a671d67e..2e13b094438d 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -652,6 +652,16 @@ union bpf_attr {
  * u64 bpf_ktime_get_ns(void)
  * 	Description
  * 		Return the time elapsed since system boot, in nanoseconds.
+ * 		Does not include time the system was suspended.
+ * 		See: clock_gettime(CLOCK_MONOTONIC)
+ * 	Return
+ * 		Current *ktime*.
+ *
+ * u64 bpf_ktime_get_boot_ns(void)
+ * 	Description
+ * 		Return the time elapsed since system boot, in nanoseconds.
+ * 		Does include the time the system was suspended.
+ * 		See: clock_gettime(CLOCK_BOOTTIME)
  * 	Return
  * 		Current *ktime*.
  *
@@ -3151,7 +3161,8 @@ union bpf_attr {
 	FN(xdp_output),			\
 	FN(get_netns_cookie),		\
 	FN(get_current_ancestor_cgroup_id),	\
-	FN(sk_assign),
+	FN(sk_assign),			\
+	FN(ktime_get_boot_ns),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 916f5132a984..d87877cd99c1 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -2151,6 +2151,7 @@ const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
 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_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 a5158a179e81..fb3ecc5dee7f 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -155,6 +155,18 @@ const struct bpf_func_proto bpf_ktime_get_ns_proto = {
 	.ret_type	= RET_INTEGER,
 };
 
+BPF_CALL_0(bpf_ktime_get_boot_ns)
+{
+	/* NMI safe access to clock boottime */
+	return ktime_get_boot_fast_ns();
+}
+
+const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
+	.func		= bpf_ktime_get_boot_ns,
+	.gpl_only	= false,
+	.ret_type	= RET_INTEGER,
+};
+
 BPF_CALL_0(bpf_get_current_pid_tgid)
 {
 	struct task_struct *task = current;
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index ca1796747a77..e875c95d3ced 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -797,6 +797,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_map_peek_elem_proto;
 	case BPF_FUNC_ktime_get_ns:
 		return &bpf_ktime_get_ns_proto;
+	case BPF_FUNC_ktime_get_boot_ns:
+		return &bpf_ktime_get_boot_ns_proto;
 	case BPF_FUNC_tail_call:
 		return &bpf_tail_call_proto;
 	case BPF_FUNC_get_current_pid_tgid:
diff --git a/net/core/filter.c b/net/core/filter.c
index 755867867e57..ec567d1e6fb9 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -6009,6 +6009,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
 		return &bpf_tail_call_proto;
 	case BPF_FUNC_ktime_get_ns:
 		return &bpf_ktime_get_ns_proto;
+	case BPF_FUNC_ktime_get_boot_ns:
+		return &bpf_ktime_get_boot_ns_proto;
 	default:
 		break;
 	}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 2e29a671d67e..2e13b094438d 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -652,6 +652,16 @@ union bpf_attr {
  * u64 bpf_ktime_get_ns(void)
  * 	Description
  * 		Return the time elapsed since system boot, in nanoseconds.
+ * 		Does not include time the system was suspended.
+ * 		See: clock_gettime(CLOCK_MONOTONIC)
+ * 	Return
+ * 		Current *ktime*.
+ *
+ * u64 bpf_ktime_get_boot_ns(void)
+ * 	Description
+ * 		Return the time elapsed since system boot, in nanoseconds.
+ * 		Does include the time the system was suspended.
+ * 		See: clock_gettime(CLOCK_BOOTTIME)
  * 	Return
  * 		Current *ktime*.
  *
@@ -3151,7 +3161,8 @@ union bpf_attr {
 	FN(xdp_output),			\
 	FN(get_netns_cookie),		\
 	FN(get_current_ancestor_cgroup_id),	\
-	FN(sk_assign),
+	FN(sk_assign),			\
+	FN(ktime_get_boot_ns),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
-- 
2.26.1.301.g55bc3eb7cb9-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] net: bpf: add bpf_ktime_get_boot_ns()
  2020-04-20 20:26 [PATCH] net: bpf: add bpf_ktime_get_boot_ns() Maciej Żenczykowski
@ 2020-04-26 16:31 ` Alexei Starovoitov
  2020-04-26 16:47   ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2020-04-26 16:31 UTC (permalink / raw)
  To: Maciej Żenczykowski
  Cc: Maciej Żenczykowski, Alexei Starovoitov, Daniel Borkmann,
	Linux Network Development Mailing List, linux-kernel,
	David S . Miller, bpf

On Mon, Apr 20, 2020 at 01:26:43PM -0700, Maciej Żenczykowski wrote:
> From: Maciej Żenczykowski <maze@google.com>
> 
> On a device like a cellphone which is constantly suspending
> and resuming CLOCK_MONOTONIC is not particularly useful for
> keeping track of or reacting to external network events.
> Instead you want to use CLOCK_BOOTTIME.
> 
> Hence add bpf_ktime_get_boot_ns() as a mirror of bpf_ktime_get_ns()
> based around CLOCK_BOOTTIME instead of CLOCK_MONOTONIC.
> 
> Signed-off-by: Maciej Żenczykowski <maze@google.com>
...
> diff --git a/net/core/filter.c b/net/core/filter.c
> index 755867867e57..ec567d1e6fb9 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -6009,6 +6009,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
>  		return &bpf_tail_call_proto;
>  	case BPF_FUNC_ktime_get_ns:
>  		return &bpf_ktime_get_ns_proto;
> +	case BPF_FUNC_ktime_get_boot_ns:
> +		return &bpf_ktime_get_boot_ns_proto;
>  	default:
>  		break;
>  	}

That part got moved into kernel/bpf/helpers.c in the mean time.
I fixed it up and applied. Thanks

In the future please cc bpf@vger for all bpf related patches.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] net: bpf: add bpf_ktime_get_boot_ns()
  2020-04-26 16:31 ` Alexei Starovoitov
@ 2020-04-26 16:47   ` Alexei Starovoitov
  2020-04-26 20:54     ` Maciej Żenczykowski
  0 siblings, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2020-04-26 16:47 UTC (permalink / raw)
  To: Maciej Żenczykowski
  Cc: Maciej Żenczykowski, Alexei Starovoitov, Daniel Borkmann,
	Linux Network Development Mailing List, LKML, David S . Miller,
	bpf

On Sun, Apr 26, 2020 at 9:31 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Mon, Apr 20, 2020 at 01:26:43PM -0700, Maciej Żenczykowski wrote:
> > From: Maciej Żenczykowski <maze@google.com>
> >
> > On a device like a cellphone which is constantly suspending
> > and resuming CLOCK_MONOTONIC is not particularly useful for
> > keeping track of or reacting to external network events.
> > Instead you want to use CLOCK_BOOTTIME.
> >
> > Hence add bpf_ktime_get_boot_ns() as a mirror of bpf_ktime_get_ns()
> > based around CLOCK_BOOTTIME instead of CLOCK_MONOTONIC.
> >
> > Signed-off-by: Maciej Żenczykowski <maze@google.com>
> ...
> > diff --git a/net/core/filter.c b/net/core/filter.c
> > index 755867867e57..ec567d1e6fb9 100644
> > --- a/net/core/filter.c
> > +++ b/net/core/filter.c
> > @@ -6009,6 +6009,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
> >               return &bpf_tail_call_proto;
> >       case BPF_FUNC_ktime_get_ns:
> >               return &bpf_ktime_get_ns_proto;
> > +     case BPF_FUNC_ktime_get_boot_ns:
> > +             return &bpf_ktime_get_boot_ns_proto;
> >       default:
> >               break;
> >       }
>
> That part got moved into kernel/bpf/helpers.c in the mean time.
> I fixed it up and applied. Thanks
>
> In the future please cc bpf@vger for all bpf related patches.

The order of comments for bpf_ktime_get_boot_ns
was also incorrect.
Most selftests were broken.
I fixed it up as well.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] net: bpf: add bpf_ktime_get_boot_ns()
  2020-04-26 16:47   ` Alexei Starovoitov
@ 2020-04-26 20:54     ` Maciej Żenczykowski
  0 siblings, 0 replies; 4+ messages in thread
From: Maciej Żenczykowski @ 2020-04-26 20:54 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Alexei Starovoitov, Daniel Borkmann,
	Linux Network Development Mailing List, LKML, David S . Miller,
	bpf

> > I fixed it up and applied. Thanks
> >
> > In the future please cc bpf@vger for all bpf related patches.

Will do so.

> The order of comments for bpf_ktime_get_boot_ns
> was also incorrect.
> Most selftests were broken.
> I fixed it up as well.

Thank you.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-04-26 20:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-20 20:26 [PATCH] net: bpf: add bpf_ktime_get_boot_ns() Maciej Żenczykowski
2020-04-26 16:31 ` Alexei Starovoitov
2020-04-26 16:47   ` Alexei Starovoitov
2020-04-26 20:54     ` Maciej Żenczykowski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).