linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC] Add BPF_SYNCHRONIZE bpf(2) command
@ 2018-07-07  1:56 Daniel Colascione
  2018-07-07  2:54 ` Alexei Starovoitov
  0 siblings, 1 reply; 30+ messages in thread
From: Daniel Colascione @ 2018-07-07  1:56 UTC (permalink / raw)
  To: joelaf, ast; +Cc: linux-kernel, timmurray, Daniel Colascione

BPF_SYNCHRONIZE waits for any BPF programs active at the time of
BPF_SYNCHRONIZE to complete, allowing userspace to ensure atomicity of
RCU data structure operations with respect to active programs. For
example, userspace can update a map->map entry to point to a new map,
use BPF_SYNCHRONIZE to wait for any BPF programs using the old map to
complete, and then drain the old map without fear that BPF programs
may still be updating it.

Signed-off-by: Daniel Colascione <dancol@google.com>
---
 include/uapi/linux/bpf.h |  1 +
 kernel/bpf/syscall.c     | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index b7db3261c62d..4365c50e8055 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -98,6 +98,7 @@ enum bpf_cmd {
 	BPF_BTF_LOAD,
 	BPF_BTF_GET_FD_BY_ID,
 	BPF_TASK_FD_QUERY,
+	BPF_SYNCHRONIZE,
 };
 
 enum bpf_map_type {
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index d10ecd78105f..60ec7811846e 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2272,6 +2272,20 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
 	if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
+	if (cmd == BPF_SYNCHRONIZE) {
+		if (uattr != NULL || size != 0)
+			return -EINVAL;
+		err = security_bpf(cmd, NULL, 0);
+		if (err < 0)
+			return err;
+		/* BPF programs are run with preempt disabled, so
+		 * synchronize_sched is sufficient even with
+		 * RCU_PREEMPT.
+		 */
+		synchronize_sched();
+		return 0;
+	}
+
 	err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
 	if (err)
 		return err;
-- 
2.18.0.203.gfac676dfb9-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread
* Re: [RFC] Add BPF_SYNCHRONIZE bpf(2) command
@ 2018-07-29 15:57 Alexei Starovoitov
  2018-07-30 22:26 ` Joel Fernandes
  0 siblings, 1 reply; 30+ messages in thread
From: Alexei Starovoitov @ 2018-07-29 15:57 UTC (permalink / raw)
  To: Daniel Colascione
  Cc: Joel Fernandes, Lorenzo Colitti, Chenbo Feng, Mathieu Desnoyers,
	Joel Fernandes, Alexei Starovoitov, lkml, Tim Murray,
	Daniel Borkmann, netdev

On Fri, Jul 27, 2018 at 10:17 PM, Daniel Colascione <dancol@google.com> wrote:
> On Sat, Jul 14, 2018 at 11:18 AM, Joel Fernandes <joel@joelfernandes.org>
> wrote:
>>
>> By the way just curious I was briefly going through kernel/bpf/arraymap.c.
>> How are you protecting against load-store tearing of values of array map
>> updates/lookups?
>>
>> For example, if userspace reads an array map at a particular index, while
>> another CPU is updating it, then userspace can read partial values /
>> half-updated values right? Since rcu_read_lock is in use, I was hoping to
>> find something like rcu_assign_pointer there to protect readers against
>> concurrent updates.  Thanks for any clarification.
>
>
> I'm also curious about the answer to this question.

i'm not sure I understand the question.
bpf_map_type_array is a C-like array.
There is no locking of elements.
If single program executing on two cpus
and accesses the same value it will collide.
Same goes for user space vs prog parallel access.
bpf long_memcpy is an attempt to provide minimal
level of automicity when values are aligned and
size==long.

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

end of thread, other threads:[~2018-07-30 22:27 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-07  1:56 [RFC] Add BPF_SYNCHRONIZE bpf(2) command Daniel Colascione
2018-07-07  2:54 ` Alexei Starovoitov
2018-07-07  3:22   ` Daniel Colascione
2018-07-07 20:33   ` Joel Fernandes
2018-07-08 20:54     ` Mathieu Desnoyers
2018-07-09 21:09       ` Alexei Starovoitov
2018-07-09 21:35         ` Mathieu Desnoyers
2018-07-09 22:19           ` Paul E. McKenney
2018-07-09 22:19             ` Alexei Starovoitov
2018-07-09 22:48               ` Paul E. McKenney
2018-07-09 21:36         ` Daniel Colascione
2018-07-09 22:10           ` Alexei Starovoitov
2018-07-09 22:21             ` Daniel Colascione
2018-07-09 22:34               ` Alexei Starovoitov
2018-07-10  5:25                 ` Chenbo Feng
2018-07-10 23:52                   ` Alexei Starovoitov
2018-07-11  2:46                     ` Lorenzo Colitti
2018-07-11  3:40                       ` Alexei Starovoitov
2018-07-14 18:18                         ` Joel Fernandes
2018-07-16 15:29                           ` Daniel Colascione
2018-07-16 20:23                             ` Joel Fernandes
2018-07-26 16:51                               ` [PATCH v2] Add BPF_SYNCHRONIZE_MAPS " Daniel Colascione
2018-07-10  5:13       ` [RFC] Add BPF_SYNCHRONIZE " Joel Fernandes
2018-07-10 16:42         ` Paul E. McKenney
2018-07-10 16:57           ` Joel Fernandes
2018-07-10 17:12             ` Paul E. McKenney
2018-07-10 17:29               ` Joel Fernandes
2018-07-10 17:42                 ` Paul E. McKenney
2018-07-29 15:57 Alexei Starovoitov
2018-07-30 22:26 ` Joel Fernandes

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).