Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1012885imm; Mon, 9 Jul 2018 15:17:54 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcjZIAzFgW4DdIEJGn1wH6yM0NpHEFUKu/tfm92n5hnTKLOUPbOamWh2uyK7hT4TkSv8zWQ X-Received: by 2002:a65:550d:: with SMTP id f13-v6mr20763759pgr.340.1531174674244; Mon, 09 Jul 2018 15:17:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531174674; cv=none; d=google.com; s=arc-20160816; b=PYI5mlw8agH2kxZ9lzbTZ7nf6Pn8wqcjWKYZxFUALsoY04IOMSLigVrOozCl9CwllZ +g30cQQiW3wn2r1+kiH1E9o+Wip5aue3u5UbqQFjtzmyawQvhnbAZB/TVQAZdwcw29Ul P0f+U5m3xuhjAKlJCIGWeePQMWknCQIGBW5NQVGWP8ECjzZXEZ7KuRHIl3++0p5MUBBX vzWY+/o8DjXkA0J0CDGZGGywHDqicH0VWaL+Hz1hSiGD9SYMrIe42gdkgHFdPd056Vgy GFJavvmsfaBtZGXyMd2UH0b5vNolFcnLQHbIYLKq9hTdqX2bDbAK7KNLPh7FpmBKEtw/ /k3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:user-agent:in-reply-to :content-disposition:mime-version:references:reply-to:subject:cc:to :from:date:arc-authentication-results; bh=Mt2ofcFxU0SuGCw88TSV6Vh935sm5eGMkSToKeMSzWI=; b=uz2RFtsflDUYPxDjISPlKco8s9PuS7s9AGaMWqdp9u0XtkWGsSd69BIyoDgEfbvWxj 6o4tX8HjNgl2FF9rR4q6st+qHCOupcX/dkNojSIIv46Ku/T6UMpkp0G0Er7+WnZf/SxD vVkz618VM6uftE/7QItE+HlHBhGMHI2BBhKDvxGNb6Pb1rPS3xxa1XQUbCl2yuayKCd8 Xoe+VCJZmJtkDgE42jxYWpZcBSQAcVXzcsDa1/jeN1IMwJ9z9aeKN2SpMitNlbFwHXJm AalN6GVFc7FnTmR0w/15so7ByG8D/nm0uSX+mwloSUWCLu+wVUi3NeAP/Dh66t8+OoKI z+EA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n5-v6si14253698pgq.167.2018.07.09.15.17.39; Mon, 09 Jul 2018 15:17:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932797AbeGIWQz (ORCPT + 99 others); Mon, 9 Jul 2018 18:16:55 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:45074 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754210AbeGIWQw (ORCPT ); Mon, 9 Jul 2018 18:16:52 -0400 Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w69MECeX032590 for ; Mon, 9 Jul 2018 18:16:51 -0400 Received: from e16.ny.us.ibm.com (e16.ny.us.ibm.com [129.33.205.206]) by mx0a-001b2d01.pphosted.com with ESMTP id 2k4ehr6qc0-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 09 Jul 2018 18:16:51 -0400 Received: from localhost by e16.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 9 Jul 2018 18:16:50 -0400 Received: from b01cxnp22033.gho.pok.ibm.com (9.57.198.23) by e16.ny.us.ibm.com (146.89.104.203) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Mon, 9 Jul 2018 18:16:45 -0400 Received: from b01ledav003.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp22033.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w69MGiWl9371962 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 9 Jul 2018 22:16:44 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8045EB2065; Mon, 9 Jul 2018 18:16:19 -0400 (EDT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4F0C8B2064; Mon, 9 Jul 2018 18:16:19 -0400 (EDT) Received: from paulmck-ThinkPad-W541 (unknown [9.70.82.159]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP; Mon, 9 Jul 2018 18:16:19 -0400 (EDT) Received: by paulmck-ThinkPad-W541 (Postfix, from userid 1000) id 4F54F16C2F11; Mon, 9 Jul 2018 15:19:03 -0700 (PDT) Date: Mon, 9 Jul 2018 15:19:03 -0700 From: "Paul E. McKenney" To: Mathieu Desnoyers Cc: Alexei Starovoitov , Joel Fernandes , Daniel Colascione , Alexei Starovoitov , linux-kernel , Tim Murray , Daniel Borkmann , netdev , fengc Subject: Re: [RFC] Add BPF_SYNCHRONIZE bpf(2) command Reply-To: paulmck@linux.vnet.ibm.com References: <20180707015616.25988-1-dancol@google.com> <20180707025426.ssxipi7hsehoiuyo@ast-mbp.dhcp.thefacebook.com> <20180707203340.GA74719@joelaf.mtv.corp.google.com> <951478560.1636.1531083278064.JavaMail.zimbra@efficios.com> <20180709210944.quulirpmv3ydytk7@ast-mbp.dhcp.thefacebook.com> <566257859.2699.1531172134285.JavaMail.zimbra@efficios.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <566257859.2699.1531172134285.JavaMail.zimbra@efficios.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-GCONF: 00 x-cbid: 18070922-0072-0000-0000-0000037D75F4 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00009341; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000266; SDB=6.01059011; UDB=6.00543474; IPR=6.00836931; MB=3.00022077; MTD=3.00000008; XFM=3.00000015; UTC=2018-07-09 22:16:48 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18070922-0073-0000-0000-000048A7E32F Message-Id: <20180709221903.GK3593@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-07-09_07:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1806210000 definitions=main-1807090250 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 09, 2018 at 05:35:34PM -0400, Mathieu Desnoyers wrote: > > > ----- On Jul 9, 2018, at 5:09 PM, Alexei Starovoitov alexei.starovoitov@gmail.com wrote: > > > On Sun, Jul 08, 2018 at 04:54:38PM -0400, Mathieu Desnoyers wrote: > >> ----- On Jul 7, 2018, at 4:33 PM, Joel Fernandes joelaf@google.com wrote: > >> > >> > On Fri, Jul 06, 2018 at 07:54:28PM -0700, Alexei Starovoitov wrote: > >> >> On Fri, Jul 06, 2018 at 06:56:16PM -0700, Daniel Colascione wrote: > >> >> > 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 > >> >> > --- > >> >> > 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; > >> >> > >> >> I don't think it's necessary. sys_membarrier() can do this already > >> >> and some folks use it exactly for this use case. > >> > > >> > Alexei, the use of sys_membarrier for this purpose seems kind of weird to me > >> > though. No where does the manpage say membarrier should be implemented this > >> > way so what happens if the implementation changes? > >> > > >> > Further, membarrier manpage says that a memory barrier should be matched with > >> > a matching barrier. In this use case there is no matching barrier, so it > >> > makes it weirder. > >> > > >> > Lastly, sys_membarrier seems will not work on nohz-full systems, so its a bit > >> > fragile to depend on it for this? > >> > > >> > case MEMBARRIER_CMD_GLOBAL: > >> > /* MEMBARRIER_CMD_GLOBAL is not compatible with nohz_full. */ > >> > if (tick_nohz_full_enabled()) > >> > return -EINVAL; > >> > if (num_online_cpus() > 1) > >> > synchronize_sched(); > >> > return 0; > >> > > >> > > >> > Adding Mathieu as well who I believe is author/maintainer of membarrier. > >> > >> See commit 907565337 > >> "Fix: Disable sys_membarrier when nohz_full is enabled" > >> > >> "Userspace applications should be allowed to expect the membarrier system > >> call with MEMBARRIER_CMD_SHARED command to issue memory barriers on > >> nohz_full CPUs, but synchronize_sched() does not take those into > >> account." > >> > >> So AFAIU you'd want to re-use membarrier to issue synchronize_sched, and you > >> only care about kernel preempt off critical sections. > >> > >> Clearly bpf code does not run in user-space, so it would "work". > >> > >> But the guarantees provided by membarrier are not to synchronize against > >> preempt off per se. It's just that the current implementation happens to > >> do that. The point of membarrier is to turn user-space memory barriers > >> into compiler barriers. > >> > >> If what you need is to wait for a RCU grace period for whatever RCU flavor > >> ebpf is using, I would against using membarrier for this. I would rather > >> recommend adding a dedicated BPF_SYNCHRONIZE so you won't leak > >> implementation details to user-space, *and* you can eventually change you > >> RCU implementation for e.g. SRCU in the future if needed. > > > > The point about future changes to underlying bpf mechanisms is valid. > > There is work already on the way to reduce the scope of preempt_off+rcu_lock > > that currently lasts the whole prog. We will have new prog types that won't > > have such wrappers and will do rcu_lock/unlock and preempt on/off only > > when necessary. > > So something like BPF_SYNCHRONIZE will break soon, since the kernel cannot have > > guarantees on when programs finish. Calling this command BPF_SYNCHRONIZE_PROG > > also won't make sense for the same reason. > > What we can do it instead is to define synchronization barrier for > > programs accessing maps. May be call it something like: > > BPF_SYNC_MAP_ACCESS ? > > uapi/bpf.h would need to have extensive comment what this barrier is doing. > > Implementation should probably call synchronize_rcu() and not play games > > with synchronize_sched(), since that's going too much into implementation. > > Also should such sys_bpf command be root only? > > I'm not sure whether dos attack can be made by spamming synchronize_rcu() > > and synchronize_sched() for that matter. > > Adding Paul E. McKenney in CC. He may want to share his thoughts on the matter. Let's see... Spamming synchronize_rcu() and synchronize_sched() should be a non-event, at least aside from the CPUs doing the spamming. The reason for this is that a given task can only fire off a single synchronize_sched or synchronize_rcu() per few milliseconds, so you need a -lot- of tasks to have much effect, at which point the sheer number of tasks is much more a problem than the large number of outstanding synchronize_rcu() or synchronize_sched() invocations. I very strongly agree that usermode should have a operation that synchronizes with whatever eBPF uses, rather than something that forces a specific type of RCU grace period. Finally, in a few releases, synchronize_sched() will be retiring in favor of synchronize_rcu(), which will wait on preemption-disabled regions of code in addition to waiting on RCU read-side critical sections. Not a big deal, as I expect to enlist Coccinelle's aid in this. Did I manage to hit all the high points? Thanx, Paul