Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp1213680imm; Fri, 12 Oct 2018 13:55:08 -0700 (PDT) X-Google-Smtp-Source: ACcGV63h44vSbjYlaZ61vL58OMvQtEib1ZNNIIpLm9a0USpMx7SlC+Uuv4DMOKgtkK10eA+1C6SC X-Received: by 2002:a17:902:20c5:: with SMTP id v5-v6mr7282639plg.62.1539377708294; Fri, 12 Oct 2018 13:55:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539377708; cv=none; d=google.com; s=arc-20160816; b=w4T/4+H7DFhm3o9stXb3cpUIa/JMfZWnXfpGlSwLM+K1y8L37PFKLtU9PGXqs66LCb V3T1SLWjVQa3ho1tMOxVivY0EtTl4rn+YrFN/jbJyV0armAP7H2C0Q4JIQe6Ixucqsor c6a170TSMxuyMUV39xoiUmHjOcICZ4XHNl4SrlL6b++SvXFzUy0BcQQ63A1VPtvoR1jc Qr7VDa6GauCBXH7uu49dRGV75AKTUNO5Tc5XSLczaNbpFFJjkz9A2nmvaQ6NwrFgphE9 blezjf3rirUQUJ+lDZ55Qc0SIe2TAwZ70WbTKVQ/aoVPaxiCTkpNNSbG9CT6RYmUTE+m 7aIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=rqdu3rt+5zvIPF2L2ugSweeyTSFTri9Kybi6S1s2K7g=; b=gYXt/qz4DNzOYoKEEdQ5uvJpxJ2XqjAFyEF6pF3cg3f1Uqtu4FPjESVBS4yeoovIao ZnOwowG90LUxsnaxP25FIu0oRcx08NC+x3Gw78VlLGlFiyWvR/Jp6t/Hidj3SfNMTSGH ZKeQD57fz1+eEeJP0G1ByDDDl8eIwPTA01OfP9Jgoqpy4aOEtPXNNTdWM8m/wkIyszrD fkgxQgMohtX6pogANp+Q7+Cj+aH40GiMaLTtJhwUS6B/xOmSAZ4YN7LZdf0DWY0AvshE NjRuNykjFXRcBmpnUaRaXFyYxkQMiaaoF4wQwqzPNNYOUMvrSPz9pGXBmwi1xoLNWlNu 4iSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=NMqITDD3; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z65-v6si2300139pff.223.2018.10.12.13.54.52; Fri, 12 Oct 2018 13:55:08 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=NMqITDD3; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726086AbeJME2t (ORCPT + 99 others); Sat, 13 Oct 2018 00:28:49 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:43685 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725785AbeJME2s (ORCPT ); Sat, 13 Oct 2018 00:28:48 -0400 Received: by mail-pl1-f193.google.com with SMTP id 30-v6so6441459plb.10 for ; Fri, 12 Oct 2018 13:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=rqdu3rt+5zvIPF2L2ugSweeyTSFTri9Kybi6S1s2K7g=; b=NMqITDD3r3VDxvgZPXokM2X2iQ0vipZPrkhsglyl9s31bEVvSHtXIDUmlkInAD0RrN OOybxfyIJL8iWGEcA3FHHMB1gnlLUwUGt6vUz388Z7LNM7q/UV6CZiUpq0iAEfzbQqwG 7s07NniObseN6o5lt5WrBmQKOGf5Or8AJlyqft2YzXKR4pDMFW/sf1FhT87HFojlTMFy rsbCAAaxu/DeLDGU/e2UnuAcbSq/heblxqxf9zawx69vqobdykjK0QycFsUXLo3kRod9 zitfWqlM75c3pUQbHd62/DuH4NFwsiYsippmjtruoAXneXcdnkoofjvJVjnTlJcb9GnC n/LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=rqdu3rt+5zvIPF2L2ugSweeyTSFTri9Kybi6S1s2K7g=; b=JwmZficfSaMhazc99WogSw3lHv6foFosqgHXEiXNByQR3dokFEh0oy+0keeh6d4nB+ axNhivuewyGrAQ4XxapUIFmALgok3UrdbkjH+xpsMb/SZO2udS8DA70eBo+rgcUMw2Qt 1kB8qzarghrcB9qtt3ZruYldjdCBCC2XzpMZDquXzMW/V1pwvlN24GDFGrWf0u/yzAgQ oF2gsPIXRo0sxO56XOSajMB9zCP+1HyZ2L2g96auk+7Pqox1tCQVZ/nKWiJt6RHs3JM0 txyvhWiUC5+NAW4kW/wnrHLKG9kqfinnvJjAQUG5ZEPi3zdNjqhQvAcVhXM76tZ2TNBP bZuQ== X-Gm-Message-State: ABuFfogjcc9c8bDgtaV8VU1BzTNtaPksAu1WIroRG7h/4hqm8ktdzvzN Vf+9I+glpoAPKn7kgUpsGMluNQz1oXtz4Q== X-Received: by 2002:a17:902:5a4d:: with SMTP id f13-v6mr837670plm.114.1539377670379; Fri, 12 Oct 2018 13:54:30 -0700 (PDT) Received: from localhost ([2620:0:1000:1601:3aef:314f:b9ea:889f]) by smtp.gmail.com with ESMTPSA id p62-v6sm3586240pfp.111.2018.10.12.13.54.29 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 12 Oct 2018 13:54:29 -0700 (PDT) Date: Fri, 12 Oct 2018 13:54:28 -0700 From: Joel Fernandes To: Daniel Colascione Cc: linux-kernel@vger.kernel.org, timmurray@google.com, netdev@vger.kernel.org, Alexei Starovoitov , Lorenzo Colitti , Chenbo Feng , Mathieu Desnoyers , Alexei Starovoitov , Daniel Borkmann , kernel-team@android.com Subject: Re: [PATCH v4] Wait for running BPF programs when updating map-in-map Message-ID: <20181012205428.GA32209@joelaf.mtv.corp.google.com> References: <20180816040145.gqzqicab4s6kcbye@ast-mbp.dhcp.thefacebook.com> <20181012105427.243779-1-dancol@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181012105427.243779-1-dancol@google.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 12, 2018 at 03:54:27AM -0700, Daniel Colascione wrote: > The map-in-map frequently serves as a mechanism for atomic > snapshotting of state that a BPF program might record. The current > implementation is dangerous to use in this way, however, since > userspace has no way of knowing when all programs that might have > retrieved the "old" value of the map may have completed. > > This change ensures that map update operations on map-in-map map types > always wait for all references to the old map to drop before returning > to userspace. > > Signed-off-by: Daniel Colascione > --- > kernel/bpf/syscall.c | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c > index 8339d81cba1d..d7c16ae1e85a 100644 > --- a/kernel/bpf/syscall.c > +++ b/kernel/bpf/syscall.c > @@ -741,6 +741,18 @@ static int map_lookup_elem(union bpf_attr *attr) > return err; > } > > +static void maybe_wait_bpf_programs(struct bpf_map *map) > +{ > + /* Wait for any running BPF programs to complete so that > + * userspace, when we return to it, knows that all programs > + * that could be running use the new map value. > + */ > + if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || > + map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) { > + synchronize_rcu(); > + } > +} > + > #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags > > static int map_update_elem(union bpf_attr *attr) > @@ -831,6 +843,7 @@ static int map_update_elem(union bpf_attr *attr) > } > __this_cpu_dec(bpf_prog_active); > preempt_enable(); > + maybe_wait_bpf_programs(map); > out: > free_value: > kfree(value); > @@ -883,6 +896,7 @@ static int map_delete_elem(union bpf_attr *attr) > rcu_read_unlock(); > __this_cpu_dec(bpf_prog_active); > preempt_enable(); > + maybe_wait_bpf_programs(map); Looks good to me, Reviewed-by: Joel Fernandes (Google) Also I believe that those rcu_read_lock() and rcu_read_unlock() calls in the existing code are useless. preempt_disable()d code is already an RCU read-side section, and synchronize_rcu and friends work on those type of read-side sections as well (as of recent kernel releases) however removing it may make lockdep unhappy, unless we also replace all rcu_dereference() usages with rcu_dereference_sched(), so lets leave that alone for now I guess. thanks, - Joel