Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1921336imm; Tue, 10 Jul 2018 10:02:04 -0700 (PDT) X-Google-Smtp-Source: AAOMgpediSwMIcZ9deoTrrFIDxZkzB+E7+9RjCOD1wo8/B3EStLmxDOkkeS6gAFfIa6TA7tXM5LO X-Received: by 2002:a63:a44a:: with SMTP id c10-v6mr18613577pgp.198.1531242124234; Tue, 10 Jul 2018 10:02:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531242124; cv=none; d=google.com; s=arc-20160816; b=0zLU7nuM2xVSS2Jx+Jriwr56n0deQ2XSv4pHI4TTMROeNRLZ0ZajbSOdmy0iu2gmrc I14jvFMt4pGu91ho/x0UpikVDLVWO8qolmIkgnGAmasA22vNEE3802XjYtRWC4FTP4AD SS7T2ktP0Hpq7cgHA1Stald7tHK2ZqGDjZiXc4UCmiUYyxdrrkwev3ELyrKad+QA67Mk 3miD+iYDnR1bv6Un0gwv6tYtPHnbllaP3MFL1dKYvjInLkSuv5NMF0PW+5+u91CpGz4z sAvd6FsIPJOy1WdFYi/VJjGjQS5d82ApB0k9FpTfc5mTnqWNQfBA5DxU4vEpQgdwSWL/ +ZtA== 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=wTRxp2D1QjRl0zHa2SIgIFAmStf+oIdc1n98zk3P45c=; b=crTnJUfyDzZaV5OwKsdAfhPRx+WQb7P+HqLhzz6BiP6ZSZrWbsoXPr6gGNsN02FFrT zedkZV93aOvSm6cekkKj7JjZVYwFNCaPvQnbw0G71lcW4whsmh0R5nppZl5HncTHxqyF +m7SVBy2U28idwKNz2hy+SAlS/hAp5OhZ+40+B82ZkiXbhmAkihD4Rvc3UAag2FNnVxx 2oAAlmFmcXpDfdLXfENTAIOav7djZKsy2ILAFDKdexs6liZSyXtur74Y2+P319oPTWvx 3y/AMtyV/oNa264wrxEdVgiMnpk0dTFA7ZbkwbQGkjgNV/RLcWF1UQTSjb6lSTW9k34N HPRA== 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 k23-v6si17270946pls.134.2018.07.10.10.01.33; Tue, 10 Jul 2018 10:02:04 -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 S934620AbeGJQkC (ORCPT + 99 others); Tue, 10 Jul 2018 12:40:02 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:45648 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933609AbeGJQj7 (ORCPT ); Tue, 10 Jul 2018 12:39:59 -0400 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w6AGdeX4054818 for ; Tue, 10 Jul 2018 12:39:58 -0400 Received: from e13.ny.us.ibm.com (e13.ny.us.ibm.com [129.33.205.203]) by mx0b-001b2d01.pphosted.com with ESMTP id 2k4xut8a4b-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 10 Jul 2018 12:39:58 -0400 Received: from localhost by e13.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 10 Jul 2018 12:39:57 -0400 Received: from b01cxnp22033.gho.pok.ibm.com (9.57.198.23) by e13.ny.us.ibm.com (146.89.104.200) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 10 Jul 2018 12:39:55 -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 w6AGdshR9175528 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 10 Jul 2018 16:39:54 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D5548B2066; Tue, 10 Jul 2018 12:39:27 -0400 (EDT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A3711B206A; Tue, 10 Jul 2018 12:39:27 -0400 (EDT) Received: from paulmck-ThinkPad-W541 (unknown [9.70.82.159]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP; Tue, 10 Jul 2018 12:39:27 -0400 (EDT) Received: by paulmck-ThinkPad-W541 (Postfix, from userid 1000) id EC6DE16C1972; Tue, 10 Jul 2018 09:42:12 -0700 (PDT) Date: Tue, 10 Jul 2018 09:42:12 -0700 From: "Paul E. McKenney" To: Joel Fernandes Cc: Mathieu Desnoyers , Joel Fernandes , Alexei Starovoitov , Daniel Colascione , Alexei Starovoitov , linux-kernel , Tim Murray , Daniel Borkmann , netdev , fengc@google.com 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> <20180710051347.GA180724@joelaf.mtv.corp.google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180710051347.GA180724@joelaf.mtv.corp.google.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-GCONF: 00 x-cbid: 18071016-0064-0000-0000-0000032832E5 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00009345; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000266; SDB=6.01059378; UDB=6.00543695; IPR=6.00837299; MB=3.00022089; MTD=3.00000008; XFM=3.00000015; UTC=2018-07-10 16:39:57 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18071016-0065-0000-0000-000039E41AC2 Message-Id: <20180710164212.GY3593@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-07-10_06:,, 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-1807100177 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 10:13:47PM -0700, Joel Fernandes 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. > > Mathieu, Thanks a lot for your reply. I understand what you said and agree > with you. Slight OT, but I tried to go back to first principles and > understand how membarrier() uses synchronize_sched() for the "slow path" and > it didn't make immediate sense to me. Let me clarify my dillema.. > > My understanding is membarrier's MEMBARRIER_CMD_GLOBAL will employ > synchronize_sched to make sure all other CPUs aren't executing anymore in an > section of usercode that happen to be accessing memory that was written to > before the membarrier call was made. To do this, the system call will use > synchronize_sched to try to guarantee that all user-mode execution that > started before the membarrier call would be completed when the membarrier > call returns. This guarantees that without using a real memory barrier on the > "fast path", things work just fine and everyone wins. > > But, going through RCU code, I see that a "RCU-sched quiecent state" on a CPU > may be reached when the CPU receives a timer tick while executing in user > mode: > > void rcu_check_callbacks(int user) > { > trace_rcu_utilization(TPS("Start scheduler-tick")); > increment_cpu_stall_ticks(); > if (user || rcu_is_cpu_rrupt_from_idle()) { > [...] > rcu_sched_qs(); > rcu_bh_qs(); > > The problem I see is the CPU could be executing usermode code at the time of > the RCU sched-QS. This IMO is enough reason for synchronize_sched() to > return, because the CPU in question just reported a QS (assuming all other > CPUs also happen to do so if they needed to). This scenario will have inserted the needed smp_mb() into the userspace instruction execution stream, as is required by the sys_membarrier use cases. > Then I am wondering how does the membarrier call even work, the tick could > very well have interrupted the CPU while it was executing usermode code in > the middle of a set of instructions performing memory accesses. Reporting a > quiescent state at such an inopportune time would cause the membarrier call > to prematurely return, no? Sorry if I missed something. One way to think of sys_membarrier() is as something that promotes a barrier() to an smp_mb(). This barrier then separates the target CPU's accesses that the caller saw before the sys_membarrier() from that same CPU's accesses that the caller will see after the sys_membarrier(). > The other question I have is about the whole "nohz-full doesn't work" thing. > I didn't fully understand why. RCU is already tracking the state of nohz-full > CPUs because the rcu dynticks code in (kernel/rcu/tree.c) monitors > transitions to and from usermode even if the timer tick is turned off. So why > would it not work? In the nohz_full case, there is no need for sys_membarrier()'s call to synchronize_sched() to interact directly with the nohz_full CPU. It can instead look at the target CPU's dyntick-idle state, and that state would potentially have been set in the dim distant past, thus having no effect on the target CPU's current execution. Thanx, Paul