From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=qFa8=AG=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-4.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,
	SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=no
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id ED8C6C433DF
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:03 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id C83742076E
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:03 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="d+d5TAas"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S2405363AbgFYOOD (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Thu, 25 Jun 2020 10:14:03 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44564 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S2405359AbgFYOOC (ORCPT <rfc822;bpf@vger.kernel.org>);
        Thu, 25 Jun 2020 10:14:02 -0400
Received: from mail-lj1-x244.google.com (mail-lj1-x244.google.com [IPv6:2a00:1450:4864:20::244])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E522C08C5DB
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:02 -0700 (PDT)
Received: by mail-lj1-x244.google.com with SMTP id s1so6716263ljo.0
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:02 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=cloudflare.com; s=google;
        h=from:to:cc:subject:date:message-id:mime-version
         :content-transfer-encoding;
        bh=ldn6FBnq77DB/yy/XXlMExQzH2a0FQzjuDXigv4+ejk=;
        b=d+d5TAasDL3beWsWjtWjkBkMWstxF9TJO5DH5OlLAaQ9c9ShjBooa0622Q8PfpH2kC
         F5iCdXwutjHic5+M8O6rk8DxWnqVuoiBFi7+cRHPJGL8SgCDR7/zwvB3Ft0ODaGXN6Mw
         mNHbA6pgB0CSm2AU3MoHohtNLIqU/oad+FxNM=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version
         :content-transfer-encoding;
        bh=ldn6FBnq77DB/yy/XXlMExQzH2a0FQzjuDXigv4+ejk=;
        b=iN3wGhtfu+UaKSbEO456vqEerOOjSKPsDLsvGesJtmwDA5e0NdtuDkEc9ouwYVeD3e
         IyLHSXayCz4n5IlAEniMejTMkmtYJrWu51Q244GcUF+j6VXR5MuqfTCIVQ4foe059Il0
         p7CCC27g2FX5upvpBCPnCWB0ubctBbhXm4TNM2r25xlqyZ8LZUUS4iTdOb37GIDU5fJF
         z2WkRwvjs7C6wzaPJSdbMGeOadowQjlGAumiKQknePerl+/t1x9ZpEmfYP3T8P5a8uIZ
         i+MLHjQC6A/lp2M4GWERIqzqB39ktOH5qPRCLbdCr0U3Gp3/OZnbty4Pb8NFzotA5p80
         8EPw==
X-Gm-Message-State: AOAM533KrsHmZovRWX9+FFHg3lz7nD5J7y5pe9kGpHE9YvWGMPIFk1/D
        St6C+eN9ae6ddVXSgyeOyK1oh8C8JA6EEw==
X-Google-Smtp-Source: ABdhPJwwFS1QNZO155U1/cGCWMoyVuUuoyBNOXZB1e4mVW1gwol8Rd4yERtyfBw6vj+9q5SbvvEONQ==
X-Received: by 2002:a2e:9cd4:: with SMTP id g20mr16570156ljj.371.1593094440144;
        Thu, 25 Jun 2020 07:14:00 -0700 (PDT)
Received: from cloudflare.com ([2a02:a310:c262:aa00:b35e:8938:2c2a:ba8b])
        by smtp.gmail.com with ESMTPSA id z2sm5609698ljh.72.2020.06.25.07.13.59
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 25 Jun 2020 07:13:59 -0700 (PDT)
From:   Jakub Sitnicki <jakub@cloudflare.com>
To:     bpf@vger.kernel.org
Cc:     netdev@vger.kernel.org, kernel-team@cloudflare.com,
        Andrii Nakryiko <andrii.nakryiko@gmail.com>,
        Martin KaFai Lau <kafai@fb.com>,
        Stanislav Fomichev <sdf@google.com>
Subject: [PATCH bpf-next v3 0/4] bpf, netns: Prepare for multi-prog attachment
Date:   Thu, 25 Jun 2020 16:13:53 +0200
Message-Id: <20200625141357.910330-1-jakub@cloudflare.com>
X-Mailer: git-send-email 2.25.4
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

This patch set prepares ground for link-based multi-prog attachment for
future netns attach types, with BPF_SK_LOOKUP attach type in mind [0].

Two changes are needed in order to attach and run a series of BPF programs:

  1) an bpf_prog_array of programs to run (patch #2), and
  2) a list of attached links to keep track of attachments (patch #3).

Nothing changes for BPF flow_dissector. Just as before only one program can
be attached to netns.


In v3 I've simplified patch #2 that introduces bpf_prog_array to take
advantage of the fact that it will hold at most one program for now.

In particular, I'm no longer using bpf_prog_array_copy. It turned out to be
less suitable for link operations than I thought as it fails to append the
same BPF program.

bpf_prog_array_replace_item is also gone, because we know we always want to
replace the first element in prog_array.

Naturally the code that handles bpf_prog_array will need change once
more when there is a program type that allows multi-prog attachment. But I
feel it will be better to do it gradually and present it together with
tests that actually exercise multi-prog code paths.

Thanks,
-jkbs

[0] https://lore.kernel.org/bpf/20200511185218.1422406-1-jakub@cloudflare.com/

Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Stanislav Fomichev <sdf@google.com>

v2 -> v3:
- Don't check if run_array is null in link update callback. (Martin)
- Allow updating the link with the same BPF program. (Andrii)
- Add patch #4 with a test for the above case.
- Kill bpf_prog_array_replace_item. Access the run_array directly.
- Switch from bpf_prog_array_copy() to bpf_prog_array_alloc(1, ...).
- Replace rcu_deref_protected & RCU_INIT_POINTER with rcu_replace_pointer.
- Drop Andrii's Ack from patch #2. Code changed.

v1 -> v2:

- Show with a (void) cast that bpf_prog_array_replace_item() return value
  is ignored on purpose. (Andrii)
- Explain why bpf-cgroup cannot replace programs in bpf_prog_array based
  on bpf_prog pointer comparison in patch #2 description. (Andrii)

Jakub Sitnicki (4):
  flow_dissector: Pull BPF program assignment up to bpf-netns
  bpf, netns: Keep attached programs in bpf_prog_array
  bpf, netns: Keep a list of attached bpf_link's
  selftests/bpf: Test updating flow_dissector link with same program

 include/net/flow_dissector.h                  |   3 +-
 include/net/netns/bpf.h                       |   7 +-
 kernel/bpf/net_namespace.c                    | 162 ++++++++++++------
 net/core/flow_dissector.c                     |  32 ++--
 .../bpf/prog_tests/flow_dissector_reattach.c  |  32 +++-
 5 files changed, 160 insertions(+), 76 deletions(-)

-- 
2.25.4


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=qFa8=AG=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,
	USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 3FF4FC433E1
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:13 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 1E18820702
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:13 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="pP62zYhi"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S2405372AbgFYOOJ (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Thu, 25 Jun 2020 10:14:09 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44574 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S2405285AbgFYOOE (ORCPT <rfc822;bpf@vger.kernel.org>);
        Thu, 25 Jun 2020 10:14:04 -0400
Received: from mail-lf1-x141.google.com (mail-lf1-x141.google.com [IPv6:2a00:1450:4864:20::141])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A781C08C5DB
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:04 -0700 (PDT)
Received: by mail-lf1-x141.google.com with SMTP id y18so3286739lfh.11
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:04 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=cloudflare.com; s=google;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=YWExEfsXXQUQcMS8kRbVy31FOuquakLr9FFsow70cAs=;
        b=pP62zYhiJb5j/NwXGy0sfg9MKWeWoFCHA5VpaG+lC4GMpq3yMi8R54xFKIb0vF5csB
         hMClUYGIqOhkXIT0A6bwPtNFH5U47VvluKreVFnPSKgkrGHeFE65LeDJDq9M0inB/I0s
         gI7ECEcbj5Lgan9O2dZqaS+FC4xgGEO1ajB0Q=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=YWExEfsXXQUQcMS8kRbVy31FOuquakLr9FFsow70cAs=;
        b=klrc5nYZFH8g0iQIa+ILHFHwD0Xr1OKcE7VLrVb0vqYXpkC08Z/mdGX+8P+xH3gjfu
         Yju109vpsA1qGCElYTbtIKtI8q6a0+BExF/w/1DLUyB8Laau4MnJRmMK+pIOT7KiFmP6
         I7InqAXMidmIrzw3jkMb8kiXMnD5wRZNXmoiJHAMoxpjhlAzZh8EA3zSKBPKJQn82V6s
         IPpTXetHg6GE1OTPtu/Gjf+rAax1ZvqdIl+s8ypHvK7XPyU2FnZB4jyb1JAXC7tA/Z3g
         1df15JWBeBQtq9kPF3hBDEy9JGonCyEzpqSJscI70iBZlDSqoQXi+QUgxoFb8KrCSTE8
         ctkQ==
X-Gm-Message-State: AOAM531d+Rqd/ZzRS2sIg/ecBmFPYUKJGzliAi3Ur34BITl1tH5Md/PY
        u4aLIFFg/LeectTC8KfpcXIYJ4VsvRA2Yg==
X-Google-Smtp-Source: ABdhPJy92NGNaqtCE+TGjZVGoVoylwKmv+EjsRZm3+5ZueZ3egvLjOh57qYgdQ0Nksm3TCgnOEWvww==
X-Received: by 2002:a19:dc08:: with SMTP id t8mr15446981lfg.191.1593094442286;
        Thu, 25 Jun 2020 07:14:02 -0700 (PDT)
Received: from cloudflare.com ([2a02:a310:c262:aa00:b35e:8938:2c2a:ba8b])
        by smtp.gmail.com with ESMTPSA id y2sm4523489lfh.1.2020.06.25.07.14.01
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 25 Jun 2020 07:14:01 -0700 (PDT)
From:   Jakub Sitnicki <jakub@cloudflare.com>
To:     bpf@vger.kernel.org
Cc:     netdev@vger.kernel.org, kernel-team@cloudflare.com,
        Andrii Nakryiko <andriin@fb.com>
Subject: [PATCH bpf-next v3 1/4] flow_dissector: Pull BPF program assignment up to bpf-netns
Date:   Thu, 25 Jun 2020 16:13:54 +0200
Message-Id: <20200625141357.910330-2-jakub@cloudflare.com>
X-Mailer: git-send-email 2.25.4
In-Reply-To: <20200625141357.910330-1-jakub@cloudflare.com>
References: <20200625141357.910330-1-jakub@cloudflare.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

Prepare for using bpf_prog_array to store attached programs by moving out
code that updates the attached program out of flow dissector.

Managing bpf_prog_array is more involved than updating a single bpf_prog
pointer. This will let us do it all from one place, bpf/net_namespace.c, in
the subsequent patch.

No functional change intended.

Acked-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 include/net/flow_dissector.h |  3 ++-
 kernel/bpf/net_namespace.c   | 20 ++++++++++++++++++--
 net/core/flow_dissector.c    | 13 ++-----------
 3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/include/net/flow_dissector.h b/include/net/flow_dissector.h
index a7eba43fe4e4..4b6e36288ddd 100644
--- a/include/net/flow_dissector.h
+++ b/include/net/flow_dissector.h
@@ -372,7 +372,8 @@ flow_dissector_init_keys(struct flow_dissector_key_control *key_control,
 }
 
 #ifdef CONFIG_BPF_SYSCALL
-int flow_dissector_bpf_prog_attach(struct net *net, struct bpf_prog *prog);
+int flow_dissector_bpf_prog_attach_check(struct net *net,
+					 struct bpf_prog *prog);
 #endif /* CONFIG_BPF_SYSCALL */
 
 #endif
diff --git a/kernel/bpf/net_namespace.c b/kernel/bpf/net_namespace.c
index 78cf061f8179..b951dab2687f 100644
--- a/kernel/bpf/net_namespace.c
+++ b/kernel/bpf/net_namespace.c
@@ -189,6 +189,7 @@ int netns_bpf_prog_query(const union bpf_attr *attr,
 int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 {
 	enum netns_bpf_attach_type type;
+	struct bpf_prog *attached;
 	struct net *net;
 	int ret;
 
@@ -207,12 +208,26 @@ int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 
 	switch (type) {
 	case NETNS_BPF_FLOW_DISSECTOR:
-		ret = flow_dissector_bpf_prog_attach(net, prog);
+		ret = flow_dissector_bpf_prog_attach_check(net, prog);
 		break;
 	default:
 		ret = -EINVAL;
 		break;
 	}
+	if (ret)
+		goto out_unlock;
+
+	attached = rcu_dereference_protected(net->bpf.progs[type],
+					     lockdep_is_held(&netns_bpf_mutex));
+	if (attached == prog) {
+		/* The same program cannot be attached twice */
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+	rcu_assign_pointer(net->bpf.progs[type], prog);
+	if (attached)
+		bpf_prog_put(attached);
+
 out_unlock:
 	mutex_unlock(&netns_bpf_mutex);
 
@@ -277,7 +292,7 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
 
 	switch (type) {
 	case NETNS_BPF_FLOW_DISSECTOR:
-		err = flow_dissector_bpf_prog_attach(net, link->prog);
+		err = flow_dissector_bpf_prog_attach_check(net, link->prog);
 		break;
 	default:
 		err = -EINVAL;
@@ -286,6 +301,7 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
 	if (err)
 		goto out_unlock;
 
+	rcu_assign_pointer(net->bpf.progs[type], link->prog);
 	net->bpf.links[type] = link;
 
 out_unlock:
diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c
index d02df0b6d0d9..b57fb1359395 100644
--- a/net/core/flow_dissector.c
+++ b/net/core/flow_dissector.c
@@ -70,10 +70,10 @@ void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
 EXPORT_SYMBOL(skb_flow_dissector_init);
 
 #ifdef CONFIG_BPF_SYSCALL
-int flow_dissector_bpf_prog_attach(struct net *net, struct bpf_prog *prog)
+int flow_dissector_bpf_prog_attach_check(struct net *net,
+					 struct bpf_prog *prog)
 {
 	enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
-	struct bpf_prog *attached;
 
 	if (net == &init_net) {
 		/* BPF flow dissector in the root namespace overrides
@@ -97,15 +97,6 @@ int flow_dissector_bpf_prog_attach(struct net *net, struct bpf_prog *prog)
 			return -EEXIST;
 	}
 
-	attached = rcu_dereference_protected(net->bpf.progs[type],
-					     lockdep_is_held(&netns_bpf_mutex));
-	if (attached == prog)
-		/* The same program cannot be attached twice */
-		return -EINVAL;
-
-	rcu_assign_pointer(net->bpf.progs[type], prog);
-	if (attached)
-		bpf_prog_put(attached);
 	return 0;
 }
 #endif /* CONFIG_BPF_SYSCALL */
-- 
2.25.4


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=qFa8=AG=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,
	USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 8BD7BC433E1
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:15 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 68CE720702
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:15 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="AJuVkJrj"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S2405381AbgFYOON (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Thu, 25 Jun 2020 10:14:13 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44592 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S2405370AbgFYOOJ (ORCPT <rfc822;bpf@vger.kernel.org>);
        Thu, 25 Jun 2020 10:14:09 -0400
Received: from mail-lf1-x144.google.com (mail-lf1-x144.google.com [IPv6:2a00:1450:4864:20::144])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B530FC08C5DF
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:08 -0700 (PDT)
Received: by mail-lf1-x144.google.com with SMTP id t74so3311980lff.2
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:08 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=cloudflare.com; s=google;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=2WRoV9X9kFx4tH/O9RiGcHsAYeLr9yIqCeeMhhCgrJ8=;
        b=AJuVkJrj4JMoYvcFwn5bfoxKUOySLhe1Qu5NKfks5fX+n1gllAAdCDc9dSpOuBabba
         GcocTip1EVSfQlyzS0amHO+YHW5QvCyZW/RS/WYjniAJbRDOaiiPT2KEBU2p6ZrIfONG
         7A9jUM9f0NpveaIxpV20HI6aTHj6Fb3AVglns=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=2WRoV9X9kFx4tH/O9RiGcHsAYeLr9yIqCeeMhhCgrJ8=;
        b=Frm4uEeeVUn/FBGO561ZOpUiK7FLV1xW4nQDu3+tRCBTE7lIsgSV9EPNbZ9QPgSkf5
         bi+8qRw7nos8UvaCqpbip3ZPoY+HFJ7YPAeFUSnKHt+iqn2XHr9K4XA8xtRnd/2SPS15
         LGtpqecd26lYyM6WkhOY82rJKNiKmxcTEP1tHqM5ds0/2O5qV0O5wd6v+ewXqv9+Lb5D
         XGwTmxhLSuFOc/vblf5jPlTi+hEPs6cY9pAlWQ3SbYstYBFCXvkUxbMxMyc+MH3rAqSc
         d6Q0m1VXMmAV0eXVlRtp475sFRxgJMndKdup7xV/sLdUWpX35OumDsmW32p4w/Q7OOVh
         VqyQ==
X-Gm-Message-State: AOAM5313TzzmPhjVkvVTu2PHf31Dutqhl93t551od745GhoCN0xkBWpy
        I3g0sRlJTGQSFgGQ7dchSvpfw2MwYEIH8A==
X-Google-Smtp-Source: ABdhPJyIAnEr+JggWl+wWJkg38pE7pCLXRu45g3xASJ7fSJb5hEwCwaDzNobxf3LfbiiuYrKXW5fpQ==
X-Received: by 2002:a19:701:: with SMTP id 1mr18265306lfh.138.1593094446807;
        Thu, 25 Jun 2020 07:14:06 -0700 (PDT)
Received: from cloudflare.com ([2a02:a310:c262:aa00:b35e:8938:2c2a:ba8b])
        by smtp.gmail.com with ESMTPSA id a17sm5988539lfo.73.2020.06.25.07.14.05
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 25 Jun 2020 07:14:06 -0700 (PDT)
From:   Jakub Sitnicki <jakub@cloudflare.com>
To:     bpf@vger.kernel.org
Cc:     netdev@vger.kernel.org, kernel-team@cloudflare.com,
        Andrii Nakryiko <andriin@fb.com>
Subject: [PATCH bpf-next v3 3/4] bpf, netns: Keep a list of attached bpf_link's
Date:   Thu, 25 Jun 2020 16:13:56 +0200
Message-Id: <20200625141357.910330-4-jakub@cloudflare.com>
X-Mailer: git-send-email 2.25.4
In-Reply-To: <20200625141357.910330-1-jakub@cloudflare.com>
References: <20200625141357.910330-1-jakub@cloudflare.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

To support multi-prog link-based attachments for new netns attach types, we
need to keep track of more than one bpf_link per attach type. Hence,
convert net->bpf.links into a list, that currently can be either empty or
have just one item.

Instead of reusing bpf_prog_list from bpf-cgroup, we link together
bpf_netns_link's themselves. This makes list management simpler as we don't
have to allocate, initialize, and later release list elements. We can do
this because multi-prog attachment will be available only for bpf_link, and
we don't need to build a list of programs attached directly and indirectly
via links.

No functional changes intended.

Acked-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 include/net/netns/bpf.h    |  2 +-
 kernel/bpf/net_namespace.c | 42 +++++++++++++++++++++-----------------
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/include/net/netns/bpf.h b/include/net/netns/bpf.h
index a5015bda9979..0ca6a1b87185 100644
--- a/include/net/netns/bpf.h
+++ b/include/net/netns/bpf.h
@@ -15,7 +15,7 @@ struct netns_bpf {
 	/* Array of programs to run compiled from progs or links */
 	struct bpf_prog_array __rcu *run_array[MAX_NETNS_BPF_ATTACH_TYPE];
 	struct bpf_prog *progs[MAX_NETNS_BPF_ATTACH_TYPE];
-	struct bpf_link *links[MAX_NETNS_BPF_ATTACH_TYPE];
+	struct list_head links[MAX_NETNS_BPF_ATTACH_TYPE];
 };
 
 #endif /* __NETNS_BPF_H__ */
diff --git a/kernel/bpf/net_namespace.c b/kernel/bpf/net_namespace.c
index 0dba97202357..7a34a8caf954 100644
--- a/kernel/bpf/net_namespace.c
+++ b/kernel/bpf/net_namespace.c
@@ -19,20 +19,12 @@ struct bpf_netns_link {
 	 * with netns_bpf_mutex held.
 	 */
 	struct net *net;
+	struct list_head node; /* node in list of links attached to net */
 };
 
 /* Protects updates to netns_bpf */
 DEFINE_MUTEX(netns_bpf_mutex);
 
-/* Must be called with netns_bpf_mutex held. */
-static void __net_exit bpf_netns_link_auto_detach(struct bpf_link *link)
-{
-	struct bpf_netns_link *net_link =
-		container_of(link, struct bpf_netns_link, link);
-
-	net_link->net = NULL;
-}
-
 /* Must be called with netns_bpf_mutex held. */
 static void netns_bpf_run_array_detach(struct net *net,
 				       enum netns_bpf_attach_type type)
@@ -66,7 +58,7 @@ static void bpf_netns_link_release(struct bpf_link *link)
 		goto out_unlock;
 
 	netns_bpf_run_array_detach(net, type);
-	net->bpf.links[type] = NULL;
+	list_del(&net_link->node);
 
 out_unlock:
 	mutex_unlock(&netns_bpf_mutex);
@@ -225,7 +217,7 @@ int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	mutex_lock(&netns_bpf_mutex);
 
 	/* Attaching prog directly is not compatible with links */
-	if (net->bpf.links[type]) {
+	if (!list_empty(&net->bpf.links[type])) {
 		ret = -EEXIST;
 		goto out_unlock;
 	}
@@ -279,7 +271,7 @@ static int __netns_bpf_prog_detach(struct net *net,
 	struct bpf_prog *attached;
 
 	/* Progs attached via links cannot be detached */
-	if (net->bpf.links[type])
+	if (!list_empty(&net->bpf.links[type]))
 		return -EINVAL;
 
 	attached = net->bpf.progs[type];
@@ -310,13 +302,15 @@ int netns_bpf_prog_detach(const union bpf_attr *attr)
 static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
 				 enum netns_bpf_attach_type type)
 {
+	struct bpf_netns_link *net_link =
+		container_of(link, struct bpf_netns_link, link);
 	struct bpf_prog_array *run_array;
 	int err;
 
 	mutex_lock(&netns_bpf_mutex);
 
 	/* Allow attaching only one prog or link for now */
-	if (net->bpf.links[type]) {
+	if (!list_empty(&net->bpf.links[type])) {
 		err = -E2BIG;
 		goto out_unlock;
 	}
@@ -345,7 +339,7 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
 	run_array->items[0].prog = link->prog;
 	rcu_assign_pointer(net->bpf.run_array[type], run_array);
 
-	net->bpf.links[type] = link;
+	list_add_tail(&net_link->node, &net->bpf.links[type]);
 
 out_unlock:
 	mutex_unlock(&netns_bpf_mutex);
@@ -404,24 +398,34 @@ int netns_bpf_link_create(const union bpf_attr *attr, struct bpf_prog *prog)
 	return err;
 }
 
+static int __net_init netns_bpf_pernet_init(struct net *net)
+{
+	int type;
+
+	for (type = 0; type < MAX_NETNS_BPF_ATTACH_TYPE; type++)
+		INIT_LIST_HEAD(&net->bpf.links[type]);
+
+	return 0;
+}
+
 static void __net_exit netns_bpf_pernet_pre_exit(struct net *net)
 {
 	enum netns_bpf_attach_type type;
-	struct bpf_link *link;
+	struct bpf_netns_link *net_link;
 
 	mutex_lock(&netns_bpf_mutex);
 	for (type = 0; type < MAX_NETNS_BPF_ATTACH_TYPE; type++) {
 		netns_bpf_run_array_detach(net, type);
-		link = net->bpf.links[type];
-		if (link)
-			bpf_netns_link_auto_detach(link);
-		else if (net->bpf.progs[type])
+		list_for_each_entry(net_link, &net->bpf.links[type], node)
+			net_link->net = NULL; /* auto-detach link */
+		if (net->bpf.progs[type])
 			bpf_prog_put(net->bpf.progs[type]);
 	}
 	mutex_unlock(&netns_bpf_mutex);
 }
 
 static struct pernet_operations netns_bpf_pernet_ops __net_initdata = {
+	.init = netns_bpf_pernet_init,
 	.pre_exit = netns_bpf_pernet_pre_exit,
 };
 
-- 
2.25.4


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=qFa8=AG=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,
	USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id CBAE6C433DF
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:15 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 9C25820702
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:15 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="LXqihZhg"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S2405385AbgFYOOM (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Thu, 25 Jun 2020 10:14:12 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44600 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S2405381AbgFYOOL (ORCPT <rfc822;bpf@vger.kernel.org>);
        Thu, 25 Jun 2020 10:14:11 -0400
Received: from mail-lj1-x241.google.com (mail-lj1-x241.google.com [IPv6:2a00:1450:4864:20::241])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12A6FC08C5DB
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:11 -0700 (PDT)
Received: by mail-lj1-x241.google.com with SMTP id y11so6669527ljm.9
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:10 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=cloudflare.com; s=google;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=sAqsIP04ByaDq2EDPMw15fX7T2DXBmYi0FY+Hk/6H0s=;
        b=LXqihZhgYvEEw9G7kGkmPezGcQva2huxgiPIkRxdA0ZkYJEIF+CDkPoSkWUUfFpyeV
         bZHGXOuy2hQG4ZEsqn86Ln3CYqJtrwmHKQYt0vZkEaprggM2zf9/SEX3dUyeNpl9jdeF
         RTot7q5lzBT2uG0937nXfFMczym8EYvtaSFTY=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=sAqsIP04ByaDq2EDPMw15fX7T2DXBmYi0FY+Hk/6H0s=;
        b=Am1xe1TrTMfMJUnVY1PFUWthEvwAB7g/n1Ni6MESElVLH1PKERdzn9l85zzNjXZTwt
         15xRC8MyW/ogUJ7Xz41wzgPK6IRW8Q+JW+cwrw0D1EeuJ0P8a57qFOQKicRegfru684c
         soMlVfRCBXePw1xFzXMQmYakXkLuA5buo8fq0Xl4e4eev+UxpPwUNy7Ny39aqx0ZR+pc
         wU0rV4jwcFKPuCvPrui0e4cul/UYPAAvn6dmXOWDF1WRIXnVLo/yexxeWuE2HkWHxVIL
         KaGp32/ctI3+iekfOkqkVkCTIdRAwgbItpDmv03fb1aYkoivslxdhYEmzbQjtGaNPAcW
         v3LA==
X-Gm-Message-State: AOAM531g8Efq5t0SJFY9rADjJ1scBGPUEOjrUTgcyJ61Oql0NbKX7CsM
        feJDMO498ZXyreVVoXH3chPEGFuYEQqaoA==
X-Google-Smtp-Source: ABdhPJwsHCl1XIp3EiYzINeyRwCHt4OnAV12U94LXF7dvJhP/a6B4kIl1kovM6RPW18+NLVapg6WRQ==
X-Received: by 2002:a2e:1508:: with SMTP id s8mr16038125ljd.52.1593094449209;
        Thu, 25 Jun 2020 07:14:09 -0700 (PDT)
Received: from cloudflare.com ([2a02:a310:c262:aa00:b35e:8938:2c2a:ba8b])
        by smtp.gmail.com with ESMTPSA id z84sm3952142lfa.54.2020.06.25.07.14.07
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 25 Jun 2020 07:14:08 -0700 (PDT)
From:   Jakub Sitnicki <jakub@cloudflare.com>
To:     bpf@vger.kernel.org
Cc:     netdev@vger.kernel.org, kernel-team@cloudflare.com
Subject: [PATCH bpf-next v3 4/4] selftests/bpf: Test updating flow_dissector link with same program
Date:   Thu, 25 Jun 2020 16:13:57 +0200
Message-Id: <20200625141357.910330-5-jakub@cloudflare.com>
X-Mailer: git-send-email 2.25.4
In-Reply-To: <20200625141357.910330-1-jakub@cloudflare.com>
References: <20200625141357.910330-1-jakub@cloudflare.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

This case, while not particularly useful, is worth covering because we
expect the operation to succeed as opposed when re-attaching the same
program directly with PROG_ATTACH.

While at it, update the tests summary that fell out of sync when tests
extended to cover links.

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 .../bpf/prog_tests/flow_dissector_reattach.c  | 32 ++++++++++++++++---
 1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c b/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c
index 15cb554a66d8..a2db3b0f84db 100644
--- a/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c
+++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c
@@ -1,9 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Test that the flow_dissector program can be updated with a single
- * syscall by attaching a new program that replaces the existing one.
- *
- * Corner case - the same program cannot be attached twice.
+ * Tests for attaching, detaching, and replacing flow_dissector BPF program.
  */
 
 #define _GNU_SOURCE
@@ -308,6 +305,31 @@ static void test_link_update_replace_old_prog(int netns, int prog1, int prog2)
 	CHECK_FAIL(prog_is_attached(netns));
 }
 
+static void test_link_update_same_prog(int netns, int prog1, int prog2)
+{
+	DECLARE_LIBBPF_OPTS(bpf_link_create_opts, create_opts);
+	DECLARE_LIBBPF_OPTS(bpf_link_update_opts, update_opts);
+	int err, link;
+
+	link = bpf_link_create(prog1, netns, BPF_FLOW_DISSECTOR, &create_opts);
+	if (CHECK_FAIL(link < 0)) {
+		perror("bpf_link_create(prog1)");
+		return;
+	}
+	CHECK_FAIL(query_attached_prog_id(netns) != query_prog_id(prog1));
+
+	/* Expect success updating the prog with the same one */
+	update_opts.flags = 0;
+	update_opts.old_prog_fd = 0;
+	err = bpf_link_update(link, prog1, &update_opts);
+	if (CHECK_FAIL(err))
+		perror("bpf_link_update");
+	CHECK_FAIL(query_attached_prog_id(netns) != query_prog_id(prog1));
+
+	close(link);
+	CHECK_FAIL(prog_is_attached(netns));
+}
+
 static void test_link_update_invalid_opts(int netns, int prog1, int prog2)
 {
 	DECLARE_LIBBPF_OPTS(bpf_link_create_opts, create_opts);
@@ -571,6 +593,8 @@ static void run_tests(int netns)
 		  test_link_update_no_old_prog },
 		{ "link update with replace old prog",
 		  test_link_update_replace_old_prog },
+		{ "link update with same prog",
+		  test_link_update_same_prog },
 		{ "link update invalid opts",
 		  test_link_update_invalid_opts },
 		{ "link update invalid prog",
-- 
2.25.4


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=qFa8=AG=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,
	USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 01431C433E0
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:18 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id CF21B20781
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 14:14:17 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="u2+ylto/"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S2405392AbgFYOOQ (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Thu, 25 Jun 2020 10:14:16 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44582 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S2405367AbgFYOOH (ORCPT <rfc822;bpf@vger.kernel.org>);
        Thu, 25 Jun 2020 10:14:07 -0400
Received: from mail-lj1-x241.google.com (mail-lj1-x241.google.com [IPv6:2a00:1450:4864:20::241])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A66DC08C5DD
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:06 -0700 (PDT)
Received: by mail-lj1-x241.google.com with SMTP id i3so6715740ljg.3
        for <bpf@vger.kernel.org>; Thu, 25 Jun 2020 07:14:06 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=cloudflare.com; s=google;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=MQPngL9TiDnLWfORRYBLFIi1xFMCbQlZKd+Xog1DvxE=;
        b=u2+ylto/ovSGT5iefFuJsUGRzvGZLtwZaNCsGBgtB2hTuuI8GVjS/h41uJXFAlHW+N
         nHgU0w9tp4b8YGYV2tDXG+PDkykvyeC2cfCRvDyS1Dn0HOMZq/qtJ8taOsEUujnWgfbN
         9nWN+7C6N5Wljd5a3xBFs9Oyetv6qrkBvFW7k=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=MQPngL9TiDnLWfORRYBLFIi1xFMCbQlZKd+Xog1DvxE=;
        b=czvZ5IPfIMfUPFzpangzJQ9fD9CP5xwabw+rnU/FPIZqEVNmdFrC110ueekFpgnTlZ
         DMCEnOg6RtVHCeGobYF4VGgI4hp2fdWvkwnnZG01dhdJOtK5vWTUPWLtX3gkra8fLGlk
         lnTkWBue2zuip0W1YJ5HiTuVYdo8wo8cKGK023c6akHxHYnzRrcfqT/Dz+v4Zu7LsmCT
         ZIZYdcJWHnstwr9K2ZcEGgxcbAacIk35lcYdXSyfzJW1SBrFvEE/m26VmpMTxxf9LR0e
         F0Cez0KriGyofVHODjCRrLmuPHtn4immAU75vi/6UfSFPK4uyHnC/2vtB0NS/vosRcI9
         oLCA==
X-Gm-Message-State: AOAM5305nRuiR1Je2SSUtFXZXlcAzibobBFIpHowKQBBLCkapWtU4BhP
        q9ii8y6HUNtK8GxhcD7MUNfSgipHpsMm8Q==
X-Google-Smtp-Source: ABdhPJxQOekBLwIU8lBZQFf1Hxrb9rQaWgLeZXy61XCUyHu9B470HBm0dUbxZtrIhSBQLRsce3pBKw==
X-Received: by 2002:a2e:8150:: with SMTP id t16mr16595105ljg.160.1593094444556;
        Thu, 25 Jun 2020 07:14:04 -0700 (PDT)
Received: from cloudflare.com ([2a02:a310:c262:aa00:b35e:8938:2c2a:ba8b])
        by smtp.gmail.com with ESMTPSA id w20sm4817269ljo.68.2020.06.25.07.14.03
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 25 Jun 2020 07:14:03 -0700 (PDT)
From:   Jakub Sitnicki <jakub@cloudflare.com>
To:     bpf@vger.kernel.org
Cc:     netdev@vger.kernel.org, kernel-team@cloudflare.com
Subject: [PATCH bpf-next v3 2/4] bpf, netns: Keep attached programs in bpf_prog_array
Date:   Thu, 25 Jun 2020 16:13:55 +0200
Message-Id: <20200625141357.910330-3-jakub@cloudflare.com>
X-Mailer: git-send-email 2.25.4
In-Reply-To: <20200625141357.910330-1-jakub@cloudflare.com>
References: <20200625141357.910330-1-jakub@cloudflare.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

Prepare for having multi-prog attachments for new netns attach types by
storing programs to run in a bpf_prog_array, which is well suited for
iterating over programs and running them in sequence.

After this change bpf(PROG_QUERY) may block to allocate memory in
bpf_prog_array_copy_to_user() for collected program IDs. This forces a
change in how we protect access to the attached program in the query
callback. Because bpf_prog_array_copy_to_user() can sleep, we switch from
an RCU read lock to holding a mutex that serializes updaters.

Because we allow only one BPF flow_dissector program to be attached to
netns at all times, the bpf_prog_array pointed by net->bpf.run_array is
always either detached (null) or one element long.

No functional changes intended.

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 include/net/netns/bpf.h    |   5 +-
 kernel/bpf/net_namespace.c | 120 +++++++++++++++++++++++++------------
 net/core/flow_dissector.c  |  19 +++---
 3 files changed, 96 insertions(+), 48 deletions(-)

diff --git a/include/net/netns/bpf.h b/include/net/netns/bpf.h
index a8dce2a380c8..a5015bda9979 100644
--- a/include/net/netns/bpf.h
+++ b/include/net/netns/bpf.h
@@ -9,9 +9,12 @@
 #include <linux/bpf-netns.h>
 
 struct bpf_prog;
+struct bpf_prog_array;
 
 struct netns_bpf {
-	struct bpf_prog __rcu *progs[MAX_NETNS_BPF_ATTACH_TYPE];
+	/* Array of programs to run compiled from progs or links */
+	struct bpf_prog_array __rcu *run_array[MAX_NETNS_BPF_ATTACH_TYPE];
+	struct bpf_prog *progs[MAX_NETNS_BPF_ATTACH_TYPE];
 	struct bpf_link *links[MAX_NETNS_BPF_ATTACH_TYPE];
 };
 
diff --git a/kernel/bpf/net_namespace.c b/kernel/bpf/net_namespace.c
index b951dab2687f..0dba97202357 100644
--- a/kernel/bpf/net_namespace.c
+++ b/kernel/bpf/net_namespace.c
@@ -33,6 +33,17 @@ static void __net_exit bpf_netns_link_auto_detach(struct bpf_link *link)
 	net_link->net = NULL;
 }
 
+/* Must be called with netns_bpf_mutex held. */
+static void netns_bpf_run_array_detach(struct net *net,
+				       enum netns_bpf_attach_type type)
+{
+	struct bpf_prog_array *run_array;
+
+	run_array = rcu_replace_pointer(net->bpf.run_array[type], NULL,
+					lockdep_is_held(&netns_bpf_mutex));
+	bpf_prog_array_free(run_array);
+}
+
 static void bpf_netns_link_release(struct bpf_link *link)
 {
 	struct bpf_netns_link *net_link =
@@ -54,8 +65,8 @@ static void bpf_netns_link_release(struct bpf_link *link)
 	if (!net)
 		goto out_unlock;
 
+	netns_bpf_run_array_detach(net, type);
 	net->bpf.links[type] = NULL;
-	RCU_INIT_POINTER(net->bpf.progs[type], NULL);
 
 out_unlock:
 	mutex_unlock(&netns_bpf_mutex);
@@ -76,6 +87,7 @@ static int bpf_netns_link_update_prog(struct bpf_link *link,
 	struct bpf_netns_link *net_link =
 		container_of(link, struct bpf_netns_link, link);
 	enum netns_bpf_attach_type type = net_link->netns_type;
+	struct bpf_prog_array *run_array;
 	struct net *net;
 	int ret = 0;
 
@@ -93,8 +105,11 @@ static int bpf_netns_link_update_prog(struct bpf_link *link,
 		goto out_unlock;
 	}
 
+	run_array = rcu_dereference_protected(net->bpf.run_array[type],
+					      lockdep_is_held(&netns_bpf_mutex));
+	WRITE_ONCE(run_array->items[0].prog, new_prog);
+
 	old_prog = xchg(&link->prog, new_prog);
-	rcu_assign_pointer(net->bpf.progs[type], new_prog);
 	bpf_prog_put(old_prog);
 
 out_unlock:
@@ -142,14 +157,38 @@ static const struct bpf_link_ops bpf_netns_link_ops = {
 	.show_fdinfo = bpf_netns_link_show_fdinfo,
 };
 
+/* Must be called with netns_bpf_mutex held. */
+static int __netns_bpf_prog_query(const union bpf_attr *attr,
+				  union bpf_attr __user *uattr,
+				  struct net *net,
+				  enum netns_bpf_attach_type type)
+{
+	__u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
+	struct bpf_prog_array *run_array;
+	u32 prog_cnt = 0, flags = 0;
+
+	run_array = rcu_dereference_protected(net->bpf.run_array[type],
+					      lockdep_is_held(&netns_bpf_mutex));
+	if (run_array)
+		prog_cnt = bpf_prog_array_length(run_array);
+
+	if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
+		return -EFAULT;
+	if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
+		return -EFAULT;
+	if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
+		return 0;
+
+	return bpf_prog_array_copy_to_user(run_array, prog_ids,
+					   attr->query.prog_cnt);
+}
+
 int netns_bpf_prog_query(const union bpf_attr *attr,
 			 union bpf_attr __user *uattr)
 {
-	__u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
-	u32 prog_id, prog_cnt = 0, flags = 0;
 	enum netns_bpf_attach_type type;
-	struct bpf_prog *attached;
 	struct net *net;
+	int ret;
 
 	if (attr->query.query_flags)
 		return -EINVAL;
@@ -162,32 +201,17 @@ int netns_bpf_prog_query(const union bpf_attr *attr,
 	if (IS_ERR(net))
 		return PTR_ERR(net);
 
-	rcu_read_lock();
-	attached = rcu_dereference(net->bpf.progs[type]);
-	if (attached) {
-		prog_cnt = 1;
-		prog_id = attached->aux->id;
-	}
-	rcu_read_unlock();
+	mutex_lock(&netns_bpf_mutex);
+	ret = __netns_bpf_prog_query(attr, uattr, net, type);
+	mutex_unlock(&netns_bpf_mutex);
 
 	put_net(net);
-
-	if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
-		return -EFAULT;
-	if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
-		return -EFAULT;
-
-	if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
-		return 0;
-
-	if (copy_to_user(prog_ids, &prog_id, sizeof(u32)))
-		return -EFAULT;
-
-	return 0;
+	return ret;
 }
 
 int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 {
+	struct bpf_prog_array *run_array;
 	enum netns_bpf_attach_type type;
 	struct bpf_prog *attached;
 	struct net *net;
@@ -217,14 +241,28 @@ int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	if (ret)
 		goto out_unlock;
 
-	attached = rcu_dereference_protected(net->bpf.progs[type],
-					     lockdep_is_held(&netns_bpf_mutex));
+	attached = net->bpf.progs[type];
 	if (attached == prog) {
 		/* The same program cannot be attached twice */
 		ret = -EINVAL;
 		goto out_unlock;
 	}
-	rcu_assign_pointer(net->bpf.progs[type], prog);
+
+	run_array = rcu_dereference_protected(net->bpf.run_array[type],
+					      lockdep_is_held(&netns_bpf_mutex));
+	if (run_array) {
+		WRITE_ONCE(run_array->items[0].prog, prog);
+	} else {
+		run_array = bpf_prog_array_alloc(1, GFP_KERNEL);
+		if (!run_array) {
+			ret = -ENOMEM;
+			goto out_unlock;
+		}
+		run_array->items[0].prog = prog;
+		rcu_assign_pointer(net->bpf.run_array[type], run_array);
+	}
+
+	net->bpf.progs[type] = prog;
 	if (attached)
 		bpf_prog_put(attached);
 
@@ -244,11 +282,11 @@ static int __netns_bpf_prog_detach(struct net *net,
 	if (net->bpf.links[type])
 		return -EINVAL;
 
-	attached = rcu_dereference_protected(net->bpf.progs[type],
-					     lockdep_is_held(&netns_bpf_mutex));
+	attached = net->bpf.progs[type];
 	if (!attached)
 		return -ENOENT;
-	RCU_INIT_POINTER(net->bpf.progs[type], NULL);
+	netns_bpf_run_array_detach(net, type);
+	net->bpf.progs[type] = NULL;
 	bpf_prog_put(attached);
 	return 0;
 }
@@ -272,7 +310,7 @@ int netns_bpf_prog_detach(const union bpf_attr *attr)
 static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
 				 enum netns_bpf_attach_type type)
 {
-	struct bpf_prog *prog;
+	struct bpf_prog_array *run_array;
 	int err;
 
 	mutex_lock(&netns_bpf_mutex);
@@ -283,9 +321,7 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
 		goto out_unlock;
 	}
 	/* Links are not compatible with attaching prog directly */
-	prog = rcu_dereference_protected(net->bpf.progs[type],
-					 lockdep_is_held(&netns_bpf_mutex));
-	if (prog) {
+	if (net->bpf.progs[type]) {
 		err = -EEXIST;
 		goto out_unlock;
 	}
@@ -301,7 +337,14 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
 	if (err)
 		goto out_unlock;
 
-	rcu_assign_pointer(net->bpf.progs[type], link->prog);
+	run_array = bpf_prog_array_alloc(1, GFP_KERNEL);
+	if (!run_array) {
+		err = -ENOMEM;
+		goto out_unlock;
+	}
+	run_array->items[0].prog = link->prog;
+	rcu_assign_pointer(net->bpf.run_array[type], run_array);
+
 	net->bpf.links[type] = link;
 
 out_unlock:
@@ -368,11 +411,12 @@ static void __net_exit netns_bpf_pernet_pre_exit(struct net *net)
 
 	mutex_lock(&netns_bpf_mutex);
 	for (type = 0; type < MAX_NETNS_BPF_ATTACH_TYPE; type++) {
+		netns_bpf_run_array_detach(net, type);
 		link = net->bpf.links[type];
 		if (link)
 			bpf_netns_link_auto_detach(link);
-		else
-			__netns_bpf_prog_detach(net, type);
+		else if (net->bpf.progs[type])
+			bpf_prog_put(net->bpf.progs[type]);
 	}
 	mutex_unlock(&netns_bpf_mutex);
 }
diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c
index b57fb1359395..142a8824f0a8 100644
--- a/net/core/flow_dissector.c
+++ b/net/core/flow_dissector.c
@@ -86,14 +86,14 @@ int flow_dissector_bpf_prog_attach_check(struct net *net,
 		for_each_net(ns) {
 			if (ns == &init_net)
 				continue;
-			if (rcu_access_pointer(ns->bpf.progs[type]))
+			if (rcu_access_pointer(ns->bpf.run_array[type]))
 				return -EEXIST;
 		}
 	} else {
 		/* Make sure root flow dissector is not attached
 		 * when attaching to the non-root namespace.
 		 */
-		if (rcu_access_pointer(init_net.bpf.progs[type]))
+		if (rcu_access_pointer(init_net.bpf.run_array[type]))
 			return -EEXIST;
 	}
 
@@ -894,7 +894,6 @@ bool __skb_flow_dissect(const struct net *net,
 	struct flow_dissector_key_addrs *key_addrs;
 	struct flow_dissector_key_tags *key_tags;
 	struct flow_dissector_key_vlan *key_vlan;
-	struct bpf_prog *attached = NULL;
 	enum flow_dissect_ret fdret;
 	enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
 	bool mpls_el = false;
@@ -951,14 +950,14 @@ bool __skb_flow_dissect(const struct net *net,
 	WARN_ON_ONCE(!net);
 	if (net) {
 		enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
+		struct bpf_prog_array *run_array;
 
 		rcu_read_lock();
-		attached = rcu_dereference(init_net.bpf.progs[type]);
+		run_array = rcu_dereference(init_net.bpf.run_array[type]);
+		if (!run_array)
+			run_array = rcu_dereference(net->bpf.run_array[type]);
 
-		if (!attached)
-			attached = rcu_dereference(net->bpf.progs[type]);
-
-		if (attached) {
+		if (run_array) {
 			struct bpf_flow_keys flow_keys;
 			struct bpf_flow_dissector ctx = {
 				.flow_keys = &flow_keys,
@@ -966,6 +965,7 @@ bool __skb_flow_dissect(const struct net *net,
 				.data_end = data + hlen,
 			};
 			__be16 n_proto = proto;
+			struct bpf_prog *prog;
 
 			if (skb) {
 				ctx.skb = skb;
@@ -976,7 +976,8 @@ bool __skb_flow_dissect(const struct net *net,
 				n_proto = skb->protocol;
 			}
 
-			ret = bpf_flow_dissect(attached, &ctx, n_proto, nhoff,
+			prog = READ_ONCE(run_array->items[0].prog);
+			ret = bpf_flow_dissect(prog, &ctx, n_proto, nhoff,
 					       hlen, flags);
 			__skb_flow_bpf_to_target(&flow_keys, flow_dissector,
 						 target_container);
-- 
2.25.4


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=qFa8=AG=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-4.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID,
	DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,
	SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 0979AC433DF
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 20:50:43 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id CD155206FA
	for <bpf@archiver.kernel.org>; Thu, 25 Jun 2020 20:50:42 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NTeS+7SE"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S2405056AbgFYUum (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Thu, 25 Jun 2020 16:50:42 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49618 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S2404004AbgFYUum (ORCPT <rfc822;bpf@vger.kernel.org>);
        Thu, 25 Jun 2020 16:50:42 -0400
Received: from mail-qt1-x841.google.com (mail-qt1-x841.google.com [IPv6:2607:f8b0:4864:20::841])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEC34C08C5C1;
        Thu, 25 Jun 2020 13:50:41 -0700 (PDT)
Received: by mail-qt1-x841.google.com with SMTP id j10so5811385qtq.11;
        Thu, 25 Jun 2020 13:50:41 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=mime-version:references:in-reply-to:from:date:message-id:subject:to
         :cc;
        bh=L5sBsv2VLcxQkwy3Z1G10hTQJuI20Eze6ofD5v+LMz0=;
        b=NTeS+7SEXEzhROIyC6OlKtFIZnj+QZ7WMHOitMgPteCYnaaTTJjtsRInZ1w0e5AaeW
         iw+EZt1JaDufB9H6Ds8rsQA6DYcRnH9q5XdcRAIDb/nZNyuspC5mfwU3EMUOlLOdruSV
         I0tkLAVeWUtNF9MFFKwBR5kuGK5n8t66dYnNnBCzagH0qu6HTtzOdscH7NDL5ZVn5K7E
         M1S6DMo+JbkIjjqQ5LdJLgmjwhkUZqYJLYSCVznS3JrLRNN+u7+9eI5S688e8csKFXOE
         7g+u7dCe/URHQ0qIUNIAzraQjjPVaR8Q11dPfollIVD47ECX27pHt1OCW3ZQHWGVHGHo
         28vQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:references:in-reply-to:from:date
         :message-id:subject:to:cc;
        bh=L5sBsv2VLcxQkwy3Z1G10hTQJuI20Eze6ofD5v+LMz0=;
        b=VeDAIzqC6Svcw+I8Z+3Jj69igsfmOH6H0khtqj1M7WHBvch9X8RG4R9M69aidmwcau
         QFy5W5ZaykNZkCgp+wsyWKlYXWUiInZh37VvsDIoSYVeYz9m8I6tCsj43jlJmf41vxSt
         htE5ZIHAC2+5RQiqswOzW1eIc10OubqfBAymw8nqP0VXSH5Z5u6ElvpLPyoyjuyMbmMW
         lyPJbknCNK4crCj5+CjsfM9nt09TdVmLJgVZ2gKsYAYg/LqjT1lwDdBDXKIq/Vdx0LDB
         kZmoYfOKSkmk9TlvmnvMUMSuu9UWy8ybAweesKxnOSXphSQDZAO/Qhij66ixCVDOO+lS
         tErA==
X-Gm-Message-State: AOAM5334hXWbvgBgUhCtQHte0cMGuRaBAsk4GfrFG8EvNC+7qZV37lFz
        HbNg+g4cMmFPIGHnk20PEFS9MAD0UE+sELgUcck=
X-Google-Smtp-Source: ABdhPJyr0FC5yZDOdt0VdVhuE7WqEP2MW+KZr6pWoIdQpLNUyP1ue9S6ng8xPuRXf/Fe2zU5CYzOwroH2/wO8MMcH2o=
X-Received: by 2002:ac8:2bba:: with SMTP id m55mr33744407qtm.171.1593118240960;
 Thu, 25 Jun 2020 13:50:40 -0700 (PDT)
MIME-Version: 1.0
References: <20200625141357.910330-1-jakub@cloudflare.com> <20200625141357.910330-3-jakub@cloudflare.com>
In-Reply-To: <20200625141357.910330-3-jakub@cloudflare.com>
From:   Andrii Nakryiko <andrii.nakryiko@gmail.com>
Date:   Thu, 25 Jun 2020 13:50:29 -0700
Message-ID: <CAEf4Bzar93mCMm5vgMiYu6_m2N=icv2Wgmy2ohuKoQr810Kk1w@mail.gmail.com>
Subject: Re: [PATCH bpf-next v3 2/4] bpf, netns: Keep attached programs in bpf_prog_array
To:     Jakub Sitnicki <jakub@cloudflare.com>
Cc:     bpf <bpf@vger.kernel.org>, Networking <netdev@vger.kernel.org>,
        kernel-team <kernel-team@cloudflare.com>
Content-Type: text/plain; charset="UTF-8"
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

On Thu, Jun 25, 2020 at 7:17 AM Jakub Sitnicki <jakub@cloudflare.com> wrote:
>
> Prepare for having multi-prog attachments for new netns attach types by
> storing programs to run in a bpf_prog_array, which is well suited for
> iterating over programs and running them in sequence.
>
> After this change bpf(PROG_QUERY) may block to allocate memory in
> bpf_prog_array_copy_to_user() for collected program IDs. This forces a
> change in how we protect access to the attached program in the query
> callback. Because bpf_prog_array_copy_to_user() can sleep, we switch from
> an RCU read lock to holding a mutex that serializes updaters.
>
> Because we allow only one BPF flow_dissector program to be attached to
> netns at all times, the bpf_prog_array pointed by net->bpf.run_array is
> always either detached (null) or one element long.
>
> No functional changes intended.
>
> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
> ---

I wonder if instead of NULL prog_array, it's better to just use a
dummy empty (but allocated) array. Might help eliminate some of the
IFs, maybe even in the hot path.


>  include/net/netns/bpf.h    |   5 +-
>  kernel/bpf/net_namespace.c | 120 +++++++++++++++++++++++++------------
>  net/core/flow_dissector.c  |  19 +++---
>  3 files changed, 96 insertions(+), 48 deletions(-)
>

[...]


>
> +/* Must be called with netns_bpf_mutex held. */
> +static int __netns_bpf_prog_query(const union bpf_attr *attr,
> +                                 union bpf_attr __user *uattr,
> +                                 struct net *net,
> +                                 enum netns_bpf_attach_type type)
> +{
> +       __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
> +       struct bpf_prog_array *run_array;
> +       u32 prog_cnt = 0, flags = 0;
> +
> +       run_array = rcu_dereference_protected(net->bpf.run_array[type],
> +                                             lockdep_is_held(&netns_bpf_mutex));
> +       if (run_array)
> +               prog_cnt = bpf_prog_array_length(run_array);
> +
> +       if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
> +               return -EFAULT;
> +       if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
> +               return -EFAULT;
> +       if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
> +               return 0;
> +
> +       return bpf_prog_array_copy_to_user(run_array, prog_ids,
> +                                          attr->query.prog_cnt);

It doesn't seem like bpf_prog_array_copy_to_user can handle NULL run_array

> +}
> +
>  int netns_bpf_prog_query(const union bpf_attr *attr,
>                          union bpf_attr __user *uattr)
>  {
> -       __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
> -       u32 prog_id, prog_cnt = 0, flags = 0;
>         enum netns_bpf_attach_type type;
> -       struct bpf_prog *attached;
>         struct net *net;
> +       int ret;
>
>         if (attr->query.query_flags)
>                 return -EINVAL;

[...]

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=o7vX=AH=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-2.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID,
	DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,
	MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1
	autolearn=no autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id B37F1C433E0
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 01:38:48 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 7A755207D8
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 01:38:48 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="RmOIC61a";
	dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="VRx4mMCr"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S1727792AbgFZBir (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Thu, 25 Jun 2020 21:38:47 -0400
Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:19518 "EHLO
        mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL)
        by vger.kernel.org with ESMTP id S1727781AbgFZBir (ORCPT
        <rfc822;bpf@vger.kernel.org>); Thu, 25 Jun 2020 21:38:47 -0400
Received: from pps.filterd (m0001303.ppops.net [127.0.0.1])
        by m0001303.ppops.net (8.16.0.42/8.16.0.42) with SMTP id 05Q1cj1R030204;
        Thu, 25 Jun 2020 18:38:45 -0700
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=date : from : to : cc :
 subject : message-id : references : content-type : in-reply-to :
 mime-version; s=facebook; bh=Vybb1l2L69sNjn+C+62QxDNNI6j2Cb2pvdI2hAYMIz8=;
 b=RmOIC61aIDwIaMchJ/Y5a1vFfZxUxfC0r75CrTABFo2BiMXegwyH1QHAym5nEFKcKWLA
 Yf7VU9ElTZEFp/Sv0u5HJFr7pmLvPnJNzDlxA9NaP7MYrZsr/o0LmR5UJexo9bYlTnqt
 fRkCMDO6vKdnFvyRcUbPJvF17cHKEYkF/7A= 
Received: from maileast.thefacebook.com ([163.114.130.16])
        by m0001303.ppops.net with ESMTP id 31ux1gk5ar-2
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT);
        Thu, 25 Jun 2020 18:38:45 -0700
Received: from NAM12-MW2-obe.outbound.protection.outlook.com (100.104.31.183)
 by o365-in.thefacebook.com (100.104.36.101) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id
 15.1.1979.3; Thu, 25 Jun 2020 18:38:19 -0700
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=ChtWpBdaim7qQj6rSmzGYsF3m9MJy+iSr+Smx3gwHqxFhVaHQ+0C/v9RKsAaXgkV0dePYkBRvPQnUtX2AUBuHbQJWlxRW2nVIOfuu7uVJvdr9e8t4iML364xmcnnMWZj1hjlEsxQdU/1A+55FWp99ymtvYIOQyw4G0lwH5fRBQol/pEcjrVpg6sxXjOC8gJ/S1KldTCRxFm6rP8FbyeGFpDpaRvGh5LbJb67HYPz+6RyYfG8JfR7L9dW4ur/pQtBHOwYYFf3rVU0I6layD4oy6OotSZFG0FQCtIgyiqHtqgGSA+cmQ3wIXnRctdw6cUgOEcgRP9KrclVEgyMwGLKfw==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=Vybb1l2L69sNjn+C+62QxDNNI6j2Cb2pvdI2hAYMIz8=;
 b=hZOVJ5PN/+vj25OgXBfCqvkorYJiHxFWoN9u03dXLmMB3hlbNwN4E8fUqYqnBYEizOfDf+r7IGTfxZ6ND2SbRz1LLwxzUCCwTqyIA5t5uhoR1G8PX2gc5+qdwRq58VbTdR5urEzuQAV9cI/ue1jgwjDhZSUeujkEGkC5ZgQgmVvT4ec8W1lV1bd7iiIwvxwD8SzrEomKJcN4ND5/xof9oQvfbfibOo1M3Kr4r/3aVwlCGVM9Gckwe4LP9ohy/h78hqV8iNzgFGP48Vb/aOycMZJjkns+QgYLFVeRfEwkRYdpzeA4SPPa993xDYYNIJx4t7ppEiI5UzhKz63E9aX7Sg==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
 smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass
 header.d=fb.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com;
 s=selector2-fb-onmicrosoft-com;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=Vybb1l2L69sNjn+C+62QxDNNI6j2Cb2pvdI2hAYMIz8=;
 b=VRx4mMCrh1mNJvgrM0/j8/9brF6Meb4yMyBc9bjHJUfe/wO/gqO9nZA0tgYppguJ2ew0MHLYEKG1fwg+5LchAc3R60OJqaeatcuMO6jjIMrTfCRpBZPUInvDJcSF+am+L8X5nT98PuQEas/eiTlXA08KbFQRMrmVI9uvNJunoKc=
Authentication-Results: cloudflare.com; dkim=none (message not signed)
 header.d=none;cloudflare.com; dmarc=none action=none header.from=fb.com;
Received: from DM6PR15MB3580.namprd15.prod.outlook.com (2603:10b6:5:1f9::10)
 by DM5PR15MB1321.namprd15.prod.outlook.com (2603:10b6:3:b7::15) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3131.23; Fri, 26 Jun
 2020 01:38:04 +0000
Received: from DM6PR15MB3580.namprd15.prod.outlook.com
 ([fe80::c8f5:16eb:3f57:b3dc]) by DM6PR15MB3580.namprd15.prod.outlook.com
 ([fe80::c8f5:16eb:3f57:b3dc%5]) with mapi id 15.20.3131.021; Fri, 26 Jun 2020
 01:38:04 +0000
Date:   Thu, 25 Jun 2020 18:38:01 -0700
From:   Martin KaFai Lau <kafai@fb.com>
To:     Jakub Sitnicki <jakub@cloudflare.com>
CC:     <bpf@vger.kernel.org>, <netdev@vger.kernel.org>,
        <kernel-team@cloudflare.com>, Andrii Nakryiko <andriin@fb.com>
Subject: Re: [PATCH bpf-next v3 1/4] flow_dissector: Pull BPF program
 assignment up to bpf-netns
Message-ID: <20200626013801.2246uk4zh35ihbos@kafai-mbp.dhcp.thefacebook.com>
References: <20200625141357.910330-1-jakub@cloudflare.com>
 <20200625141357.910330-2-jakub@cloudflare.com>
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20200625141357.910330-2-jakub@cloudflare.com>
User-Agent: NeoMutt/20180716
X-ClientProxiedBy: BYAPR05CA0079.namprd05.prod.outlook.com
 (2603:10b6:a03:e0::20) To DM6PR15MB3580.namprd15.prod.outlook.com
 (2603:10b6:5:1f9::10)
MIME-Version: 1.0
X-MS-Exchange-MessageSentRepresentingType: 1
Received: from kafai-mbp.dhcp.thefacebook.com (2620:10d:c090:400::5:62ad) by BYAPR05CA0079.namprd05.prod.outlook.com (2603:10b6:a03:e0::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3153.10 via Frontend Transport; Fri, 26 Jun 2020 01:38:03 +0000
X-Originating-IP: [2620:10d:c090:400::5:62ad]
X-MS-PublicTrafficType: Email
X-MS-Office365-Filtering-Correlation-Id: f9ba6c36-37a8-4f44-93d3-08d8197191b8
X-MS-TrafficTypeDiagnostic: DM5PR15MB1321:
X-MS-Exchange-Transport-Forked: True
X-Microsoft-Antispam-PRVS: <DM5PR15MB1321EBF3ECB445724A0B8E45D5930@DM5PR15MB1321.namprd15.prod.outlook.com>
X-FB-Source: Internal
X-MS-Oob-TLC-OOBClassifiers: OLM:6790;
X-Forefront-PRVS: 0446F0FCE1
X-MS-Exchange-SenderADCheck: 1
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: pvEXmhRk9JEquFXCbGaSu8Uy6ASQqQkiPNDe4F1G1Cnpl7Dv4HMFxPIN63RBrBzS6NtcRtR4doXJvDA0mfTLmvORjRSl21wivMF7g5IMz/zdxLF2OEJnuO9ELoAATGjUFkhhX1Ot6AWMgg3UmIPT7WD+6gAYEhkWOT6C5Ge31tFFGS8XvI9TqUHkjYYzMYsVQdLkqI+OEJQqZiiiFrdPkGwzAS+9JOahC4/Js4h6eqrzszOSlCchnjgOoOmB+dQzCJtE6F0Lhdqo6VHRaQbcillDfgwFgCgcrdBN/UlMI5iZRqoR1oY2AIoQH7YIoDMndFaS2PRKDOPtVH8u4s9C5Q==
X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR15MB3580.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(396003)(346002)(376002)(136003)(366004)(39860400002)(316002)(6506007)(66476007)(66556008)(52116002)(66946007)(6916009)(16526019)(186003)(7696005)(8676002)(9686003)(4744005)(86362001)(4326008)(2906002)(83380400001)(1076003)(55016002)(8936002)(478600001)(5660300002);DIR:OUT;SFP:1102;
X-MS-Exchange-AntiSpam-MessageData:  
X-MS-Exchange-CrossTenant-Network-Message-Id: f9ba6c36-37a8-4f44-93d3-08d8197191b8
X-MS-Exchange-CrossTenant-AuthSource: DM6PR15MB3580.namprd15.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2020 01:38:04.5623
 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName: 1P1+AoyxDao8Pl4Z6f1X4VixlwB047brJZw7ZazTKYOoAZqGr++x4Gu4qJjJzyhN
X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR15MB1321
X-OriginatorOrg: fb.com
X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.216,18.0.687
 definitions=2020-06-25_20:2020-06-25,2020-06-25 signatures=0
X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 malwarescore=0
 adultscore=0 mlxlogscore=830 clxscore=1015 priorityscore=1501
 lowpriorityscore=0 cotscore=-2147483648 phishscore=0 spamscore=0
 mlxscore=0 bulkscore=0 impostorscore=0 suspectscore=0 classifier=spam
 adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000
 definitions=main-2006260009
X-FB-Internal: deliver
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

On Thu, Jun 25, 2020 at 04:13:54PM +0200, Jakub Sitnicki wrote:
> Prepare for using bpf_prog_array to store attached programs by moving out
> code that updates the attached program out of flow dissector.
> 
> Managing bpf_prog_array is more involved than updating a single bpf_prog
> pointer. This will let us do it all from one place, bpf/net_namespace.c, in
> the subsequent patch.
> 
> No functional change intended.
Acked-by: Martin KaFai Lau <kafai@fb.com>

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=o7vX=AH=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-8.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID,
	DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,
	MSGID_FROM_MTA_HEADER,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1
	autolearn=unavailable autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 3D423C433DF
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 05:41:26 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 0D8632078D
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 05:41:26 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="g21meisp";
	dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="fRT4pDoc"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S1726968AbgFZFlZ (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Fri, 26 Jun 2020 01:41:25 -0400
Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:2102 "EHLO
        mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK)
        by vger.kernel.org with ESMTP id S1725855AbgFZFlY (ORCPT
        <rfc822;bpf@vger.kernel.org>); Fri, 26 Jun 2020 01:41:24 -0400
Received: from pps.filterd (m0148460.ppops.net [127.0.0.1])
        by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 05Q5YcRM023374;
        Thu, 25 Jun 2020 22:41:23 -0700
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=date : from : to : cc :
 subject : message-id : references : content-type : in-reply-to :
 mime-version; s=facebook; bh=nhpRJo6y1oxwYMpONKwMh2Z6sHweTUMOZfKzzj8KB2Q=;
 b=g21meispwktOnZ7V053OZoOo12D1crGzxAl1lyoI7+98ZzlH77Ce9qS5Qac1UAwMXQ3M
 /t3M6KziJ2psEIU5B/T5CtZX//krG7NK0+ZGVjWK7/knq/4h6RGFtRXZU33kWm7qM25p
 bpku+Y9EfZbCNqG11UQFmA2JCYgtHcwVZjk= 
Received: from maileast.thefacebook.com ([163.114.130.16])
        by mx0a-00082601.pphosted.com with ESMTP id 31ux1eus39-1
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT);
        Thu, 25 Jun 2020 22:41:22 -0700
Received: from NAM02-SN1-obe.outbound.protection.outlook.com (100.104.31.183)
 by o365-in.thefacebook.com (100.104.35.172) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id
 15.1.1979.3; Thu, 25 Jun 2020 22:41:22 -0700
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=KIQoPMlKsCIPLR1YgpU8Dil6cVRPOUYd/fUH3msTLCxk8lpv1pToSn1IFZfW7UINBeBaeC7IWDaQDjStrrXzvx4tb/OeigHltSjlmbXJXSgCrXEonjcBzvKcRqbZzW93EbJyd94k/q5+FeZD1ji3hHGyCxtNmmYylG+W8W3yW+TowzW/roRJ3x60hyhTQcGyNUIvEY2jnboe+TtrdxBxAnbj/HRLBAOLjNCB3v5VDzBVDYU99NV+cEgV0Qw9wzyyLmYmu4YHnMyDadMN2i8ux5k7bGDf+y70grnvjQwMhCCjG18PKc6/wj4KtJHsqYONDbqvpmjdqkJVHNvp4ERbOg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=nhpRJo6y1oxwYMpONKwMh2Z6sHweTUMOZfKzzj8KB2Q=;
 b=Lx6U9ob31RiLAW4MgchsPDGkoqXkp3v1vHN1XvTCHst4pMDb+2cBX2xTdswCBsenAG9S2AP6RdAEG1GWuoWKx82SbToKm7X81u25ODeL1p9c8F5ix6Sx0YFjHk0CNp79Eh1u0KD8Q3FkxsXCwo+W4s+JEMGAfQ1m8Zxs7PlSFRtNqHBUJsl0UtQnUzytor3XAdP0PfXvBWsiQGIJHaNkKKj4GUZ5lB2LHwniMHVBGJnlezetQb6cgiFqEk7gGcDt7Yf8IPwTl6LK1H4jg0w733Rwel7dRXJAILuJ1CIaGGZdIG9Zh0jZlOkViqDbAnrWTh61PHa2B9fd3DE2zaGe/A==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
 smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass
 header.d=fb.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com;
 s=selector2-fb-onmicrosoft-com;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=nhpRJo6y1oxwYMpONKwMh2Z6sHweTUMOZfKzzj8KB2Q=;
 b=fRT4pDocAz4mUeILbm6AXkUBZk9zxWPtNutve0fCBtcr5qo5t8ch38y6qi/KbgaX75VNC1F19YdjiMnrjDLJhBO72qC3gb1qTp9FZNG3ljQGjmzbv5z3fo4Qpl0VaC+x/D+56DSrEkDdevXyMFKPZIWOBHMYECvJp8DBbggg9o8=
Authentication-Results: cloudflare.com; dkim=none (message not signed)
 header.d=none;cloudflare.com; dmarc=none action=none header.from=fb.com;
Received: from DM6PR15MB3580.namprd15.prod.outlook.com (2603:10b6:5:1f9::10)
 by DM6PR15MB2316.namprd15.prod.outlook.com (2603:10b6:5:8d::10) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3131.21; Fri, 26 Jun
 2020 05:41:07 +0000
Received: from DM6PR15MB3580.namprd15.prod.outlook.com
 ([fe80::c8f5:16eb:3f57:b3dc]) by DM6PR15MB3580.namprd15.prod.outlook.com
 ([fe80::c8f5:16eb:3f57:b3dc%5]) with mapi id 15.20.3131.021; Fri, 26 Jun 2020
 05:41:07 +0000
Date:   Thu, 25 Jun 2020 22:41:05 -0700
From:   Martin KaFai Lau <kafai@fb.com>
To:     Jakub Sitnicki <jakub@cloudflare.com>
CC:     <bpf@vger.kernel.org>, <netdev@vger.kernel.org>,
        <kernel-team@cloudflare.com>
Subject: Re: [PATCH bpf-next v3 2/4] bpf, netns: Keep attached programs in
 bpf_prog_array
Message-ID: <20200626054105.rpz6py7jqc34vzyl@kafai-mbp.dhcp.thefacebook.com>
References: <20200625141357.910330-1-jakub@cloudflare.com>
 <20200625141357.910330-3-jakub@cloudflare.com>
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20200625141357.910330-3-jakub@cloudflare.com>
User-Agent: NeoMutt/20180716
X-ClientProxiedBy: BYAPR03CA0005.namprd03.prod.outlook.com
 (2603:10b6:a02:a8::18) To DM6PR15MB3580.namprd15.prod.outlook.com
 (2603:10b6:5:1f9::10)
MIME-Version: 1.0
X-MS-Exchange-MessageSentRepresentingType: 1
Received: from kafai-mbp.dhcp.thefacebook.com (2620:10d:c090:400::5:7d5a) by BYAPR03CA0005.namprd03.prod.outlook.com (2603:10b6:a02:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3131.20 via Frontend Transport; Fri, 26 Jun 2020 05:41:06 +0000
X-Originating-IP: [2620:10d:c090:400::5:7d5a]
X-MS-PublicTrafficType: Email
X-MS-Office365-Filtering-Correlation-Id: e1072e61-4318-4fff-7b85-08d8199385de
X-MS-TrafficTypeDiagnostic: DM6PR15MB2316:
X-Microsoft-Antispam-PRVS: <DM6PR15MB23168FBF7D0424499C5D6D73D5930@DM6PR15MB2316.namprd15.prod.outlook.com>
X-FB-Source: Internal
X-MS-Oob-TLC-OOBClassifiers: OLM:9508;
X-Forefront-PRVS: 0446F0FCE1
X-MS-Exchange-SenderADCheck: 1
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: 5TSzwxRUIWGQx2PQoTXd+MDUM2i5cV97roSh4nnvbxAWs6JRh0hV/D6P/kodD12B+5kEM4ry5mQiAEKMWe+DRu1CwvC42QcEoWVBB2LBPMWr+lg2ooVj5E3AG+Um1X3jSp9cepSUxSjYthlRHMMb18UAYuAD5TNVb//MzsahY4SvXOorgYTiTN26H8VLOLTZn4q9dq5LPafJnt4RSjLlDYNLz7W91+LeS7AFbhm2pwV9H/NNvGUWsXQdPIwmttBgFydzyiT/AmljNdmnqQBYyIkvmsZXLv25WIipvIuIMCNtr5VOMK4L1vKOyFtxdQ0Vkq/QJT3C915ZBI57aL/lZQ==
X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR15MB3580.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(366004)(376002)(346002)(39860400002)(136003)(396003)(316002)(8936002)(6916009)(2906002)(9686003)(1076003)(66946007)(7696005)(4326008)(52116002)(66556008)(8676002)(66476007)(83380400001)(86362001)(16526019)(6506007)(478600001)(55016002)(186003)(5660300002);DIR:OUT;SFP:1102;
X-MS-Exchange-AntiSpam-MessageData:  
X-MS-Exchange-CrossTenant-Network-Message-Id: e1072e61-4318-4fff-7b85-08d8199385de
X-MS-Exchange-CrossTenant-AuthSource: DM6PR15MB3580.namprd15.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2020 05:41:07.6648
 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName: AZunjjoSoDgw1o44ETZA5mqdJVythLppKbdhacCv4LxCfv5nczIrMgylfdtQuvHP
X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR15MB2316
X-OriginatorOrg: fb.com
X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.216,18.0.687
 definitions=2020-06-26_01:2020-06-26,2020-06-26 signatures=0
X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 lowpriorityscore=0
 clxscore=1015 cotscore=-2147483648 spamscore=0 mlxlogscore=999 bulkscore=0
 adultscore=0 phishscore=0 priorityscore=1501 malwarescore=0 mlxscore=0
 suspectscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx
 scancount=1 engine=8.12.0-2004280000 definitions=main-2006260040
X-FB-Internal: deliver
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

On Thu, Jun 25, 2020 at 04:13:55PM +0200, Jakub Sitnicki wrote:
> Prepare for having multi-prog attachments for new netns attach types by
> storing programs to run in a bpf_prog_array, which is well suited for
> iterating over programs and running them in sequence.
> 
> After this change bpf(PROG_QUERY) may block to allocate memory in
> bpf_prog_array_copy_to_user() for collected program IDs. This forces a
> change in how we protect access to the attached program in the query
> callback. Because bpf_prog_array_copy_to_user() can sleep, we switch from
> an RCU read lock to holding a mutex that serializes updaters.
> 
> Because we allow only one BPF flow_dissector program to be attached to
> netns at all times, the bpf_prog_array pointed by net->bpf.run_array is
> always either detached (null) or one element long.
> 
> No functional changes intended.
> 
> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
> ---
>  include/net/netns/bpf.h    |   5 +-
>  kernel/bpf/net_namespace.c | 120 +++++++++++++++++++++++++------------
>  net/core/flow_dissector.c  |  19 +++---
>  3 files changed, 96 insertions(+), 48 deletions(-)
> 
> diff --git a/include/net/netns/bpf.h b/include/net/netns/bpf.h
> index a8dce2a380c8..a5015bda9979 100644
> --- a/include/net/netns/bpf.h
> +++ b/include/net/netns/bpf.h
> @@ -9,9 +9,12 @@
>  #include <linux/bpf-netns.h>
>  
>  struct bpf_prog;
> +struct bpf_prog_array;
>  
>  struct netns_bpf {
> -	struct bpf_prog __rcu *progs[MAX_NETNS_BPF_ATTACH_TYPE];
> +	/* Array of programs to run compiled from progs or links */
> +	struct bpf_prog_array __rcu *run_array[MAX_NETNS_BPF_ATTACH_TYPE];
> +	struct bpf_prog *progs[MAX_NETNS_BPF_ATTACH_TYPE];
>  	struct bpf_link *links[MAX_NETNS_BPF_ATTACH_TYPE];
With the new run_array, I think the "*progs[]" is not needed.
It seems the original "*progs[]" is only used to tell
if it is in the prog_attach mode or the newer link mode.
There is other ways to do that.

It is something to think about when there is more clarity on how
multi netns prog will look like in the next set.

Other lgtm,

Acked-by: Martin KaFai Lau <kafai@fb.com>

Please continue the other discussion.

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=o7vX=AH=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-2.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID,
	DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,
	MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 5D84CC433E1
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 05:43:51 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 3611E2078D
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 05:43:51 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="f6lJfYwR";
	dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="QewmyY8t"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S1728057AbgFZFnu (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Fri, 26 Jun 2020 01:43:50 -0400
Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:38694 "EHLO
        mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK)
        by vger.kernel.org with ESMTP id S1728051AbgFZFnu (ORCPT
        <rfc822;bpf@vger.kernel.org>); Fri, 26 Jun 2020 01:43:50 -0400
Received: from pps.filterd (m0044010.ppops.net [127.0.0.1])
        by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 05Q5aUlT009150;
        Thu, 25 Jun 2020 22:43:49 -0700
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=date : from : to : cc :
 subject : message-id : references : content-type : in-reply-to :
 mime-version; s=facebook; bh=gF+x2K9Nsd4Qt9ko1+uoJI6eKmaJKjIE/K+o4PwWbVg=;
 b=f6lJfYwR/0qpHDD8QkhZ1n/Vj/IOwGbhoL1LwkEA8gWd/mruzWRxF2kjDReleI5yOvgp
 +HPQPp8HLCPSnNcGkobSamSSR2xTv8MP+Y6sdk2dCzFDK/shYolaW9HNvdCs88BXUUda
 7TN5I+TTLyZIEnxuNnDe5ASzxKFz0GK5+JA= 
Received: from mail.thefacebook.com ([163.114.132.120])
        by mx0a-00082601.pphosted.com with ESMTP id 31w3w2hg80-1
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT);
        Thu, 25 Jun 2020 22:43:49 -0700
Received: from NAM02-SN1-obe.outbound.protection.outlook.com (100.104.98.9) by
 o365-in.thefacebook.com (100.104.94.198) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id
 15.1.1979.3; Thu, 25 Jun 2020 22:43:48 -0700
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=oQmtD/SwRqZJbWU5JYbufT1IwjxbMpnktOV52ay3SS7fJXmNyC54LY9NlBAqGdJCHnEuLdmLzNoubk5uxGHTQhbKmpyZmmHmkfGI3gfUuydtdTxFl1a8j3R9dVvrGZ+sQ7BKta0f8srrUI7za8+DgUJ/NdTfGdA+gtzVlAvm7rsR08T/rJRcxIX36xi5r5Ct9/O2/n5bYVD3Q5SrW0jr6jAT7JtUKtJ5GHI/qFDApkhyjSlubxkUFWrZgyB6Ra2PRHkvohOWSlXmsZSs+FijOoB0BbJsygVtX92LyW5SVJ6n15MhggdirH7hCbiSwRby2ukrKywSPC+LB9rwGDcXXQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=gF+x2K9Nsd4Qt9ko1+uoJI6eKmaJKjIE/K+o4PwWbVg=;
 b=m0myb3hBnMDT9IorxqrmZQLSTHA/1PMf/m84KXVULoQFpDQgEEc9/4O2sXo1WAfKUAjOuUscsxe5dBmN4O1NnR/xR7nSO3iqyCDwpuzNLd9D4YbIuLu6r8wnLcTB9sYdyM/F95O8L7sq03KCu7fFWIZT5G9fShusNc4FbeQs2avNX7IFGurTmlNvLAevDx7OI2olQbNPt7HlsNjjmYwb9ro45UtvHwJ3FndYrzlOgJuiYRWDLMk91FRt2sEtZJApTXNoZLColnamkwGnfckMSJ0NN+4wttPDxtW0shFx6FA7vH8jvpHsvtWORQGSNkRI89emy1rohljpDYGsSAlBSw==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
 smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass
 header.d=fb.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com;
 s=selector2-fb-onmicrosoft-com;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=gF+x2K9Nsd4Qt9ko1+uoJI6eKmaJKjIE/K+o4PwWbVg=;
 b=QewmyY8tjgi+TCYandRrX0SNSdWuMyJ+weBlcKOHd5/2imBjqwu3q7t6GSgujMeLaqOc1Zu/rW6WGKGtc8Sy0tskU6r6UH54kPj87nEqNYrsz2lHsqlKFOjQGq+mvM8rDmTYTY3EM4K+98TNFdsmhC1Cz4D4/R7YBM+knkmMxA8=
Authentication-Results: cloudflare.com; dkim=none (message not signed)
 header.d=none;cloudflare.com; dmarc=none action=none header.from=fb.com;
Received: from DM6PR15MB3580.namprd15.prod.outlook.com (2603:10b6:5:1f9::10)
 by DM6PR15MB2316.namprd15.prod.outlook.com (2603:10b6:5:8d::10) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3131.21; Fri, 26 Jun
 2020 05:43:47 +0000
Received: from DM6PR15MB3580.namprd15.prod.outlook.com
 ([fe80::c8f5:16eb:3f57:b3dc]) by DM6PR15MB3580.namprd15.prod.outlook.com
 ([fe80::c8f5:16eb:3f57:b3dc%5]) with mapi id 15.20.3131.021; Fri, 26 Jun 2020
 05:43:47 +0000
Date:   Thu, 25 Jun 2020 22:43:45 -0700
From:   Martin KaFai Lau <kafai@fb.com>
To:     Jakub Sitnicki <jakub@cloudflare.com>
CC:     <bpf@vger.kernel.org>, <netdev@vger.kernel.org>,
        <kernel-team@cloudflare.com>, Andrii Nakryiko <andriin@fb.com>
Subject: Re: [PATCH bpf-next v3 3/4] bpf, netns: Keep a list of attached
 bpf_link's
Message-ID: <20200626054345.rhcg2vyokvju5xps@kafai-mbp.dhcp.thefacebook.com>
References: <20200625141357.910330-1-jakub@cloudflare.com>
 <20200625141357.910330-4-jakub@cloudflare.com>
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20200625141357.910330-4-jakub@cloudflare.com>
User-Agent: NeoMutt/20180716
X-ClientProxiedBy: BYAPR06CA0068.namprd06.prod.outlook.com
 (2603:10b6:a03:14b::45) To DM6PR15MB3580.namprd15.prod.outlook.com
 (2603:10b6:5:1f9::10)
MIME-Version: 1.0
X-MS-Exchange-MessageSentRepresentingType: 1
Received: from kafai-mbp.dhcp.thefacebook.com (2620:10d:c090:400::5:7d5a) by BYAPR06CA0068.namprd06.prod.outlook.com (2603:10b6:a03:14b::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3131.21 via Frontend Transport; Fri, 26 Jun 2020 05:43:47 +0000
X-Originating-IP: [2620:10d:c090:400::5:7d5a]
X-MS-PublicTrafficType: Email
X-MS-Office365-Filtering-Correlation-Id: 5494d31d-9442-4625-1e2d-08d81993e58d
X-MS-TrafficTypeDiagnostic: DM6PR15MB2316:
X-MS-Exchange-Transport-Forked: True
X-Microsoft-Antispam-PRVS: <DM6PR15MB23164EF88B6D720BE6197389D5930@DM6PR15MB2316.namprd15.prod.outlook.com>
X-FB-Source: Internal
X-MS-Oob-TLC-OOBClassifiers: OLM:7219;
X-Forefront-PRVS: 0446F0FCE1
X-MS-Exchange-SenderADCheck: 1
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: 5c1QTMbecjjSrziYr6xAkDV6q2e3rZNmLLSVgEp03pYiceKA8UAG+zaQIWSeKVgtxgvQUz7wkAUxIgx8MfT3CIB49yfIctbhYFNeCbuMT5SoUR5PD78zhG1QmreKzXvYiV9N1ptJXQzrGCQRHz6tHjyY2y8MXpY6p2n2a/EVIO6fm4Mf4ysm/4UG2iD2KIbPVAmFGnjcepZ/cEEd0qYi2xZmgXtDyC9J5l62+bz+lT+zhmOR0/RDI9/y0ykoK1nE11WnVD1KsSk6E64UJPsEVXdsvXOK1xQHPZ3iq7lZo6XjC2aHg0nP2mcDbcitG2K7
X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR15MB3580.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(366004)(376002)(346002)(39860400002)(136003)(396003)(316002)(8936002)(6916009)(2906002)(9686003)(1076003)(66946007)(7696005)(4326008)(52116002)(66556008)(8676002)(66476007)(4744005)(86362001)(16526019)(6506007)(478600001)(55016002)(186003)(5660300002);DIR:OUT;SFP:1102;
X-MS-Exchange-AntiSpam-MessageData:  
X-MS-Exchange-CrossTenant-Network-Message-Id: 5494d31d-9442-4625-1e2d-08d81993e58d
X-MS-Exchange-CrossTenant-AuthSource: DM6PR15MB3580.namprd15.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2020 05:43:47.7121
 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName: UxjbtYTIY79PwCcfkltkEHO1ivZBhwCREUEkBEl9RyrKtQvnZSTORM9G7FMzghUt
X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR15MB2316
X-OriginatorOrg: fb.com
X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.216,18.0.687
 definitions=2020-06-26_01:2020-06-26,2020-06-26 signatures=0
X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 mlxscore=0
 priorityscore=1501 spamscore=0 bulkscore=0 adultscore=0 impostorscore=0
 lowpriorityscore=0 malwarescore=0 phishscore=0 clxscore=1015
 suspectscore=0 mlxlogscore=680 cotscore=-2147483648 classifier=spam
 adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000
 definitions=main-2006260040
X-FB-Internal: deliver
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

On Thu, Jun 25, 2020 at 04:13:56PM +0200, Jakub Sitnicki wrote:
> To support multi-prog link-based attachments for new netns attach types, we
> need to keep track of more than one bpf_link per attach type. Hence,
> convert net->bpf.links into a list, that currently can be either empty or
> have just one item.
> 
> Instead of reusing bpf_prog_list from bpf-cgroup, we link together
> bpf_netns_link's themselves. This makes list management simpler as we don't
> have to allocate, initialize, and later release list elements. We can do
> this because multi-prog attachment will be available only for bpf_link, and
> we don't need to build a list of programs attached directly and indirectly
> via links.
Acked-by: Martin KaFai Lau <kafai@fb.com>

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=o7vX=AH=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-2.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID,
	DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,
	MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 4E43BC433E0
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 05:52:32 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 2256A2076E
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 05:52:32 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="pQ8NQ+kJ";
	dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="IQOcOGu5"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S1728101AbgFZFwb (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Fri, 26 Jun 2020 01:52:31 -0400
Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:42796 "EHLO
        mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK)
        by vger.kernel.org with ESMTP id S1728012AbgFZFwb (ORCPT
        <rfc822;bpf@vger.kernel.org>); Fri, 26 Jun 2020 01:52:31 -0400
Received: from pps.filterd (m0044010.ppops.net [127.0.0.1])
        by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 05Q5oPCj030397;
        Thu, 25 Jun 2020 22:52:30 -0700
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=date : from : to : cc :
 subject : message-id : references : content-type : in-reply-to :
 mime-version; s=facebook; bh=YzK3jyTwhbIwU9wC3QehOV/RJ9Fwko5502SmOmBcI64=;
 b=pQ8NQ+kJWaoA/EWZrrThu+ABtk0X4gtf3yAG4XUDkGnGXI4CZxi47Tqk4HwE37GkHGEi
 Xp3jCiodMjsvFgYXCVCpIgFC2HuzwrhxwFzxdj2b2ZTEP63q61iWglgvUOSAajKkw+wO
 GS1HMofayRVR695xPoV2tHcfkWdrPgyxCbo= 
Received: from maileast.thefacebook.com ([163.114.130.16])
        by mx0a-00082601.pphosted.com with ESMTP id 31w3w2hhcs-1
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT);
        Thu, 25 Jun 2020 22:52:30 -0700
Received: from NAM12-BN8-obe.outbound.protection.outlook.com (100.104.31.183)
 by o365-in.thefacebook.com (100.104.36.102) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id
 15.1.1979.3; Thu, 25 Jun 2020 22:52:28 -0700
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=VdBeQhwWYZ1g29CivV1kMVizV1pOORvmHyVUQoTyODSLQ53MHiwa7g2G3R+giJh0I1zLgesDI/HQTYJDcl9AtqFpTSSCGgMvP7dDbUahuim0I7eBZs9wAhzPqxmS92Y1PP3sJ2gvBoveMmYPX/wAAFrtT1SVGEdmO/N9Yx47Rh/pTLphrSlsGujr2nG0jGyVVEresNyw3ArkmqjIxPKa7Rqsy4IasbGUkTWQVGlJsB0Mj+b5a8TGJO3nZ6oKgSmNsi6ccmlB1Mnml7l8rm8M3396HQQEaVHWHuCQR0ckQSHMG9W1XPh3cE2IAsecIPZt5wV7VwkqIsUbjvHBSpR43w==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=YzK3jyTwhbIwU9wC3QehOV/RJ9Fwko5502SmOmBcI64=;
 b=GHeVNOiN5wXXR/5r4BlfClgI8a1tSpoeL9jOwtC0mDFHtfF48xtvDjLwS77ExUsRAbQzUfrmNPrBcmKW54VgzBdQWt1TMBc8ILaoaQFg0Bbk0MkHThAUS8CP9m/WQp3doNfNJrFncaIDzMWmsdJb5SFIpzxEoLwEhUIJy2MGinn41zFZEnhulrTV1Pt6N+iGKlgHOfvExzAb6kVT3wpxVA/Dg4Mj0FD/OuigpzuFQEgAUvkVSne5MPJTjvvtECnv3LUPfuF6Zp5OATuLwgn9kOvHioI2YdBUL4vyc5P4QUVFQs/qJIP6Edz1vnCAIP7nAA26ZQ2vjmi8m4VXEzamSQ==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
 smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass
 header.d=fb.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com;
 s=selector2-fb-onmicrosoft-com;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=YzK3jyTwhbIwU9wC3QehOV/RJ9Fwko5502SmOmBcI64=;
 b=IQOcOGu5S+CDZ9h9Jh3QETgCl0dYoiwX4FzIaX2XPZM8Q+J910B3M64cGXQEC4D4bBobiuTXMlfqvGFMUvSR8KCyLUSh89qT7S5xoy0YdwvAZTeWVE7mNvllC4IFj6iqCqF6/u5rm8RYcMOhbkBNzul7l7Gjedk7bS1ICFLD/r4=
Authentication-Results: cloudflare.com; dkim=none (message not signed)
 header.d=none;cloudflare.com; dmarc=none action=none header.from=fb.com;
Received: from DM6PR15MB3580.namprd15.prod.outlook.com (2603:10b6:5:1f9::10)
 by DM5PR15MB1209.namprd15.prod.outlook.com (2603:10b6:3:bf::9) with Microsoft
 SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 15.20.3131.20; Fri, 26 Jun 2020 05:52:26 +0000
Received: from DM6PR15MB3580.namprd15.prod.outlook.com
 ([fe80::c8f5:16eb:3f57:b3dc]) by DM6PR15MB3580.namprd15.prod.outlook.com
 ([fe80::c8f5:16eb:3f57:b3dc%5]) with mapi id 15.20.3131.021; Fri, 26 Jun 2020
 05:52:26 +0000
Date:   Thu, 25 Jun 2020 22:52:24 -0700
From:   Martin KaFai Lau <kafai@fb.com>
To:     Jakub Sitnicki <jakub@cloudflare.com>
CC:     <bpf@vger.kernel.org>, <netdev@vger.kernel.org>,
        <kernel-team@cloudflare.com>
Subject: Re: [PATCH bpf-next v3 4/4] selftests/bpf: Test updating
 flow_dissector link with same program
Message-ID: <20200626055224.ublxa7oxkpkhdwjp@kafai-mbp.dhcp.thefacebook.com>
References: <20200625141357.910330-1-jakub@cloudflare.com>
 <20200625141357.910330-5-jakub@cloudflare.com>
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20200625141357.910330-5-jakub@cloudflare.com>
User-Agent: NeoMutt/20180716
X-ClientProxiedBy: BY3PR04CA0004.namprd04.prod.outlook.com
 (2603:10b6:a03:217::9) To DM6PR15MB3580.namprd15.prod.outlook.com
 (2603:10b6:5:1f9::10)
MIME-Version: 1.0
X-MS-Exchange-MessageSentRepresentingType: 1
Received: from kafai-mbp.dhcp.thefacebook.com (2620:10d:c090:400::5:7d5a) by BY3PR04CA0004.namprd04.prod.outlook.com (2603:10b6:a03:217::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3131.20 via Frontend Transport; Fri, 26 Jun 2020 05:52:25 +0000
X-Originating-IP: [2620:10d:c090:400::5:7d5a]
X-MS-PublicTrafficType: Email
X-MS-Office365-Filtering-Correlation-Id: e144a7b5-0dd5-4fe5-51db-08d819951aa6
X-MS-TrafficTypeDiagnostic: DM5PR15MB1209:
X-Microsoft-Antispam-PRVS: <DM5PR15MB12094A7FA1C7C75F674FE113D5930@DM5PR15MB1209.namprd15.prod.outlook.com>
X-FB-Source: Internal
X-MS-Oob-TLC-OOBClassifiers: OLM:7691;
X-Forefront-PRVS: 0446F0FCE1
X-MS-Exchange-SenderADCheck: 1
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: nh5XySxc0USPcfYPmiwfS7FbfDRxGmHDjC+P2CX5/cArLSl4pEZEGDYClJcV6YiDUa3JPUjukxid7qtqS+O5Vb+LXqBdHABRA9N7ltiYTiP7t7Q40pJq/aslCGkiOjlHB+l5ZuJMn/iHnppvQ9ZwZcebrejBaXz+dDF/o7pGsjfvvjjZVKEojuPNttiarS8Bz6vprRgA0o5mkhdL3FNqJwTTk6O6lZiqeUuTtOLvJaq1YT1ZhhGO6Haj/SkQT1Or6wrUFMHCBftZwcWhmbz5yT9UasBhQiXwVsfDcJB6pG2MBqH69lp/4ptAB2zeWTly4IL2lnrIWIzWQSTXmz6nyQ==
X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR15MB3580.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(39860400002)(396003)(136003)(346002)(376002)(366004)(316002)(186003)(16526019)(83380400001)(4744005)(1076003)(66476007)(66556008)(8676002)(8936002)(66946007)(5660300002)(6916009)(9686003)(6506007)(478600001)(86362001)(7696005)(2906002)(55016002)(52116002)(4326008);DIR:OUT;SFP:1102;
X-MS-Exchange-AntiSpam-MessageData:  
X-MS-Exchange-CrossTenant-Network-Message-Id: e144a7b5-0dd5-4fe5-51db-08d819951aa6
X-MS-Exchange-CrossTenant-AuthSource: DM6PR15MB3580.namprd15.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2020 05:52:26.2883
 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName: SVQ9lSxXnzvyl8gpqk3vAIES2lz0HyfU8oBi4zKB9jOIGPtuTQFk8OCSRazr5cyE
X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR15MB1209
X-OriginatorOrg: fb.com
X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.216,18.0.687
 definitions=2020-06-26_01:2020-06-26,2020-06-26 signatures=0
X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 mlxscore=0
 priorityscore=1501 spamscore=0 bulkscore=0 adultscore=0 impostorscore=0
 lowpriorityscore=0 malwarescore=0 phishscore=0 clxscore=1015
 suspectscore=0 mlxlogscore=742 cotscore=-2147483648 classifier=spam
 adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000
 definitions=main-2006260042
X-FB-Internal: deliver
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

On Thu, Jun 25, 2020 at 04:13:57PM +0200, Jakub Sitnicki wrote:
> This case, while not particularly useful, is worth covering because we
> expect the operation to succeed as opposed when re-attaching the same
> program directly with PROG_ATTACH.
> 
> While at it, update the tests summary that fell out of sync when tests
> extended to cover links.
Acked-by: Martin KaFai Lau <kafai@fb.com>

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=o7vX=AH=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-4.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,
	SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 208BCC433E0
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 09:45:53 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id E2CD621556
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 09:45:52 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="mJW7dH1l"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S1726838AbgFZJpw (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Fri, 26 Jun 2020 05:45:52 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56090 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S1725275AbgFZJpw (ORCPT <rfc822;bpf@vger.kernel.org>);
        Fri, 26 Jun 2020 05:45:52 -0400
Received: from mail-lj1-x241.google.com (mail-lj1-x241.google.com [IPv6:2a00:1450:4864:20::241])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBF60C08C5C1
        for <bpf@vger.kernel.org>; Fri, 26 Jun 2020 02:45:51 -0700 (PDT)
Received: by mail-lj1-x241.google.com with SMTP id i3so9666126ljg.3
        for <bpf@vger.kernel.org>; Fri, 26 Jun 2020 02:45:51 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=cloudflare.com; s=google;
        h=references:user-agent:from:to:cc:subject:in-reply-to:date
         :message-id:mime-version;
        bh=5XWtFWgwZnD66tBDM/3i+lmk/MTgyC/Rg07cXurUlV8=;
        b=mJW7dH1liKL64hBpVLiWMMacAp5RmxEToSnRzbALmrc8CTk7EhcIDsLbXtzD7HKjoG
         tFgSic4SueaSPjTyvSSc921loJilw6ile5F8SqNObI6Wqtk8pfyYZOKfQXD9ZmuZI6P6
         yowUcEJcL85BNQ60LXU8ymxE9NMnD6ySR9qGA=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:references:user-agent:from:to:cc:subject
         :in-reply-to:date:message-id:mime-version;
        bh=5XWtFWgwZnD66tBDM/3i+lmk/MTgyC/Rg07cXurUlV8=;
        b=At1D62iy5ReXo5a5Tm6C6xGf3St8OPLyiZi7Z5m7cIEjZZ3E1Vw/Z3IGC88ieedIVL
         G0yUVenxmZaDJld6kfBbxmkdo9HshNlnl9sSPqJV4ngm0xl/u8k37iepl/g5mZwubk6S
         5vZjEg5wIxukeny/nC51XtawJjRUYH6acVW4Yi5lL4JJ0Qg6T7+7n/CpU+YYsmNnqNQk
         fOSJaRKBknXKyclevhjTralkvefkZ+suyWMN9H8rq93yShs8qCgULeN2w5rL/+R1t+ID
         QzLheln+Bjr5MWNbSaaxW9UWAH3/w+euBKezdHPzqDNJ3xTu0XEa0iE7rIDeaib9WuT2
         ZAiQ==
X-Gm-Message-State: AOAM533sWSbc09LqcALiaqBEDS/iE5CePbWoglZw2UXBFjxVlt35dBbW
        lqb4Xqt8QE8cnCBX6ZaHHLrKRk8rxYdYsA==
X-Google-Smtp-Source: ABdhPJyQhsH57f44jLnzS4ztkkIc8i1c/lOljaceK1e1Z14DG2IMs8pEpfRegX8FeRB7md053E3jLg==
X-Received: by 2002:a2e:991:: with SMTP id 139mr1004349ljj.314.1593164750141;
        Fri, 26 Jun 2020 02:45:50 -0700 (PDT)
Received: from cloudflare.com ([2a02:a310:c262:aa00:b35e:8938:2c2a:ba8b])
        by smtp.gmail.com with ESMTPSA id n8sm6292176lji.126.2020.06.26.02.45.48
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Fri, 26 Jun 2020 02:45:49 -0700 (PDT)
References: <20200625141357.910330-1-jakub@cloudflare.com> <20200625141357.910330-3-jakub@cloudflare.com> <CAEf4Bzar93mCMm5vgMiYu6_m2N=icv2Wgmy2ohuKoQr810Kk1w@mail.gmail.com>
User-agent: mu4e 1.1.0; emacs 26.3
From:   Jakub Sitnicki <jakub@cloudflare.com>
To:     Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc:     bpf <bpf@vger.kernel.org>, Networking <netdev@vger.kernel.org>,
        kernel-team <kernel-team@cloudflare.com>
Subject: Re: [PATCH bpf-next v3 2/4] bpf, netns: Keep attached programs in bpf_prog_array
In-reply-to: <CAEf4Bzar93mCMm5vgMiYu6_m2N=icv2Wgmy2ohuKoQr810Kk1w@mail.gmail.com>
Date:   Fri, 26 Jun 2020 11:45:48 +0200
Message-ID: <87imfema7n.fsf@cloudflare.com>
MIME-Version: 1.0
Content-Type: text/plain
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

On Thu, Jun 25, 2020 at 10:50 PM CEST, Andrii Nakryiko wrote:
> On Thu, Jun 25, 2020 at 7:17 AM Jakub Sitnicki <jakub@cloudflare.com> wrote:
>>
>> Prepare for having multi-prog attachments for new netns attach types by
>> storing programs to run in a bpf_prog_array, which is well suited for
>> iterating over programs and running them in sequence.
>>
>> After this change bpf(PROG_QUERY) may block to allocate memory in
>> bpf_prog_array_copy_to_user() for collected program IDs. This forces a
>> change in how we protect access to the attached program in the query
>> callback. Because bpf_prog_array_copy_to_user() can sleep, we switch from
>> an RCU read lock to holding a mutex that serializes updaters.
>>
>> Because we allow only one BPF flow_dissector program to be attached to
>> netns at all times, the bpf_prog_array pointed by net->bpf.run_array is
>> always either detached (null) or one element long.
>>
>> No functional changes intended.
>>
>> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
>> ---
>
> I wonder if instead of NULL prog_array, it's better to just use a
> dummy empty (but allocated) array. Might help eliminate some of the
> IFs, maybe even in the hot path.

That was my initial approach, which I abandoned seeing that it leads to
replacing NULL prog_array checks in flow_dissector with
bpf_prog_array_is_empty() checks to determine which netns has a BPF
program attached. So no IFs gone there.

While on the hot path, where we run the program, we probably would still
be left with an IF checking for empty prog_array to avoid building the
context if no progs will RUN.

The checks I'm referring to are on attach path, in
flow_dissector_bpf_prog_attach_check(), and hot-path,
__skb_flow_dissect().

>
>
>>  include/net/netns/bpf.h    |   5 +-
>>  kernel/bpf/net_namespace.c | 120 +++++++++++++++++++++++++------------
>>  net/core/flow_dissector.c  |  19 +++---
>>  3 files changed, 96 insertions(+), 48 deletions(-)
>>
>
> [...]
>
>
>>
>> +/* Must be called with netns_bpf_mutex held. */
>> +static int __netns_bpf_prog_query(const union bpf_attr *attr,
>> +                                 union bpf_attr __user *uattr,
>> +                                 struct net *net,
>> +                                 enum netns_bpf_attach_type type)
>> +{
>> +       __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
>> +       struct bpf_prog_array *run_array;
>> +       u32 prog_cnt = 0, flags = 0;
>> +
>> +       run_array = rcu_dereference_protected(net->bpf.run_array[type],
>> +                                             lockdep_is_held(&netns_bpf_mutex));
>> +       if (run_array)
>> +               prog_cnt = bpf_prog_array_length(run_array);
>> +
>> +       if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
>> +               return -EFAULT;
>> +       if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
>> +               return -EFAULT;
>> +       if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
>> +               return 0;
>> +
>> +       return bpf_prog_array_copy_to_user(run_array, prog_ids,
>> +                                          attr->query.prog_cnt);
>
> It doesn't seem like bpf_prog_array_copy_to_user can handle NULL run_array

Correct. And we never invoke it when run_array is NULL because then
prog_cnt == 0.

>
>> +}
>> +
>>  int netns_bpf_prog_query(const union bpf_attr *attr,
>>                          union bpf_attr __user *uattr)
>>  {
>> -       __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
>> -       u32 prog_id, prog_cnt = 0, flags = 0;
>>         enum netns_bpf_attach_type type;
>> -       struct bpf_prog *attached;
>>         struct net *net;
>> +       int ret;
>>
>>         if (attr->query.query_flags)
>>                 return -EINVAL;
>
> [...]

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=o7vX=AH=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=unavailable
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id D232CC433DF
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 10:40:12 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id A88CE20C09
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 10:40:12 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (1024-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="Ud1H9p6X"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S1728027AbgFZKkL (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Fri, 26 Jun 2020 06:40:11 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36208 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S1726778AbgFZKkK (ORCPT <rfc822;bpf@vger.kernel.org>);
        Fri, 26 Jun 2020 06:40:10 -0400
Received: from mail-lj1-x243.google.com (mail-lj1-x243.google.com [IPv6:2a00:1450:4864:20::243])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23663C08C5DB
        for <bpf@vger.kernel.org>; Fri, 26 Jun 2020 03:40:10 -0700 (PDT)
Received: by mail-lj1-x243.google.com with SMTP id h22so2648996lji.9
        for <bpf@vger.kernel.org>; Fri, 26 Jun 2020 03:40:10 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=cloudflare.com; s=google;
        h=references:user-agent:from:to:cc:subject:in-reply-to:date
         :message-id:mime-version;
        bh=yB42FIdlaXuGcBA5JHEjJpviYDlsFT6iBhIqQaCjtTA=;
        b=Ud1H9p6XGO0mXDpH4vRAGBFoB/eqqlXgQdkzqDD3wG1nYJRo6YakRjSYmzge8d3IUz
         mwIdjt3Dv+jiXadPbvVMCPg52rNFbbNBYDW8Zt8h/OgHQO0vqoVrlQW6QY3U9dR8D0I6
         4NIKD3ZJA5iAkoqDEuSKnibNjzbsQ+sj2I0K0=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:references:user-agent:from:to:cc:subject
         :in-reply-to:date:message-id:mime-version;
        bh=yB42FIdlaXuGcBA5JHEjJpviYDlsFT6iBhIqQaCjtTA=;
        b=GS9QrAoXcQ7jiLBmCW7qeTn+z7pZr4nkf1SrV7DIoSq/gB+Q2CUUuv2MvakcWUrZyD
         XFcoMILvp+w7ypju9qqF+vZLiG6AqCDt2WICHw7pTtOQhY9Hu+Si5Xcs5a4cFWwB4wHZ
         sziaVfu3mMmGuxWBL3IjYCfDCioWTSvEVpOLCkisORZM+24gCYUxAnGNe/crZ5Cvlo6k
         Dgco/sH4vPCFK7+99DQjQuETT+kHdCZw9bFZ6lBIlbHvxKNatqPvjBtvB4E/AUlMVui8
         9/1M+BYEowV5719YuZADcLGIpAMrOyN86hvGbQBCBHiBHxkxVXeA4hZwgIwNa+WtTt61
         tfwQ==
X-Gm-Message-State: AOAM533nBgJxbxnOD0VWwB0FkHLjzkMx4WhApKD0rQXWrX0IHWz0lPBX
        vj/MZCboKXepF5dZUyNB/t+NRg==
X-Google-Smtp-Source: ABdhPJwbSNs8Dd+VNp9wiaxQWPJSM7GoaiYqRDDe7+akgUu9u952VEoB7wWN+UwKAfjpzlsW/6SzSg==
X-Received: by 2002:a2e:9a09:: with SMTP id o9mr1080190lji.323.1593168008344;
        Fri, 26 Jun 2020 03:40:08 -0700 (PDT)
Received: from cloudflare.com ([2a02:a310:c262:aa00:b35e:8938:2c2a:ba8b])
        by smtp.gmail.com with ESMTPSA id 193sm7805655lfa.90.2020.06.26.03.40.07
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Fri, 26 Jun 2020 03:40:07 -0700 (PDT)
References: <20200625141357.910330-1-jakub@cloudflare.com> <20200625141357.910330-3-jakub@cloudflare.com> <20200626054105.rpz6py7jqc34vzyl@kafai-mbp.dhcp.thefacebook.com>
User-agent: mu4e 1.1.0; emacs 26.3
From:   Jakub Sitnicki <jakub@cloudflare.com>
To:     Martin KaFai Lau <kafai@fb.com>
Cc:     bpf@vger.kernel.org, netdev@vger.kernel.org,
        kernel-team@cloudflare.com
Subject: Re: [PATCH bpf-next v3 2/4] bpf, netns: Keep attached programs in bpf_prog_array
In-reply-to: <20200626054105.rpz6py7jqc34vzyl@kafai-mbp.dhcp.thefacebook.com>
Date:   Fri, 26 Jun 2020 12:40:06 +0200
Message-ID: <87h7uym7p5.fsf@cloudflare.com>
MIME-Version: 1.0
Content-Type: text/plain
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

On Fri, Jun 26, 2020 at 07:41 AM CEST, Martin KaFai Lau wrote:
> On Thu, Jun 25, 2020 at 04:13:55PM +0200, Jakub Sitnicki wrote:
>> Prepare for having multi-prog attachments for new netns attach types by
>> storing programs to run in a bpf_prog_array, which is well suited for
>> iterating over programs and running them in sequence.
>>
>> After this change bpf(PROG_QUERY) may block to allocate memory in
>> bpf_prog_array_copy_to_user() for collected program IDs. This forces a
>> change in how we protect access to the attached program in the query
>> callback. Because bpf_prog_array_copy_to_user() can sleep, we switch from
>> an RCU read lock to holding a mutex that serializes updaters.
>>
>> Because we allow only one BPF flow_dissector program to be attached to
>> netns at all times, the bpf_prog_array pointed by net->bpf.run_array is
>> always either detached (null) or one element long.
>>
>> No functional changes intended.
>>
>> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
>> ---
>>  include/net/netns/bpf.h    |   5 +-
>>  kernel/bpf/net_namespace.c | 120 +++++++++++++++++++++++++------------
>>  net/core/flow_dissector.c  |  19 +++---
>>  3 files changed, 96 insertions(+), 48 deletions(-)
>>
>> diff --git a/include/net/netns/bpf.h b/include/net/netns/bpf.h
>> index a8dce2a380c8..a5015bda9979 100644
>> --- a/include/net/netns/bpf.h
>> +++ b/include/net/netns/bpf.h
>> @@ -9,9 +9,12 @@
>>  #include <linux/bpf-netns.h>
>>
>>  struct bpf_prog;
>> +struct bpf_prog_array;
>>
>>  struct netns_bpf {
>> -	struct bpf_prog __rcu *progs[MAX_NETNS_BPF_ATTACH_TYPE];
>> +	/* Array of programs to run compiled from progs or links */
>> +	struct bpf_prog_array __rcu *run_array[MAX_NETNS_BPF_ATTACH_TYPE];
>> +	struct bpf_prog *progs[MAX_NETNS_BPF_ATTACH_TYPE];
>>  	struct bpf_link *links[MAX_NETNS_BPF_ATTACH_TYPE];
> With the new run_array, I think the "*progs[]" is not needed.
> It seems the original "*progs[]" is only used to tell
> if it is in the prog_attach mode or the newer link mode.
> There is other ways to do that.
>
> It is something to think about when there is more clarity on how
> multi netns prog will look like in the next set.

Having just the run_array without *progs[] is something I've tried
initially but ended up rewriting it. The end result was confusing to me.
I couldn't convince myself to sign off on it and present it.

Adding back the pointer to bpf_prog was counterintutivive, because it is
wasteful, but it actually made the code readable.

Best I can articulate why it didn't work out great (should have tagged
the branch...) is that without *progs[] the run_array holds bpf_prog
pointers sometimes with ref-count on the prog (old mode), and sometimes
without one (new mode). This mixed state manifests itself mostly on
netns teardown, where we need to access the prog_array to put the prog,
but only if we're not using links.

Then again, perhaps I simply messsed up the code back then, and it
deserves another shot. Either way, getting rid of *progs[] is a
potential optimization.

[...]

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=o7vX=AH=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-4.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID,
	DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,
	SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 2B3B3C433E0
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 22:13:53 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 014DE20663
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 22:13:53 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="b/2ZlOrf"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S1725832AbgFZWNw (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Fri, 26 Jun 2020 18:13:52 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58792 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S1725803AbgFZWNw (ORCPT <rfc822;bpf@vger.kernel.org>);
        Fri, 26 Jun 2020 18:13:52 -0400
Received: from mail-qk1-x744.google.com (mail-qk1-x744.google.com [IPv6:2607:f8b0:4864:20::744])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47B98C03E979;
        Fri, 26 Jun 2020 15:13:52 -0700 (PDT)
Received: by mail-qk1-x744.google.com with SMTP id b4so10215619qkn.11;
        Fri, 26 Jun 2020 15:13:52 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=mime-version:references:in-reply-to:from:date:message-id:subject:to
         :cc;
        bh=DFFDjNNXEfjCWIRy9/fBc5oNNA3yVMHmIODUCwXA8NA=;
        b=b/2ZlOrfQLgTNlE/XSG0YqSv/rhPBHTR/T++5VP25mygu27YN7S/y2ZJC1jpD6gAYa
         gBI4lLmERUU4g4unuP8zHsgRWDftp/KiHVnLBdGPlBUXBQP956GM2nRG1jKY6qz4mEVl
         EGoHUAZNB+M0IywcRn4EIwUMP5oPjO4psWGPGa5xEm8dAuWlXrh8MTVGGntDNBL07yxL
         0wKWp0vuZ4f0tcHwwanvTomeVB4N1AL49e4Piw9Mj+5CEv9ogYG63TlFg+ycqy1VH5R2
         zQplpBBT2Rg2LGSfkLb0CxKW6rmsiyRXhpCZGhwiIbYSdSOk0AAc9TMyp0tkeI+Rzxhf
         7rfg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:references:in-reply-to:from:date
         :message-id:subject:to:cc;
        bh=DFFDjNNXEfjCWIRy9/fBc5oNNA3yVMHmIODUCwXA8NA=;
        b=Bqh65+FOJhbs4+ZsVZGk6UzKGbN5Rgu09PiRFtcdoeEoA/bUM3IiNd/Lk75e8JKWMt
         6rF9z7wQXK6bl3ilbxubM1XQ2/8yCy6g3wyjlgXkYvIJxp1W6EV9L/CKLiYvVOoVGmJq
         eG/ygvNNOYWUEacE6NKWFudIjnBIHOQlGuLkAH1Igc3efiHdTzmuECNU5cb3sGQWIt3D
         rpYLRLTqQIZR2xqCqf/5TTwx2ugekwWkVHEY2zPLMUBhg5W193PtwhJYIc6k/eFJFl0a
         r8cFwnPhrx3sifOy66fcr4/waRWEkSMWZIM61xjCQWPEmrSHKVXk3uWI+gnTdb6V5YeL
         ERfA==
X-Gm-Message-State: AOAM5320D8sNu8MEIQwQLrMHjrEDKf+krhgus8U2M2FDtLLQ/R6JQyol
        B/L3Bk4ACgqvsxIPUAt0gmZZST0M8/r8w7JxVOA=
X-Google-Smtp-Source: ABdhPJzDBfJN4g5bLZW/TxdRek21XEd8/ob9dpigYZ3pZ50064nenlx+h7aOS+yCHXv6B42ZB7JmBA9UntirSkhV5jE=
X-Received: by 2002:a05:620a:12d2:: with SMTP id e18mr5062397qkl.437.1593209631410;
 Fri, 26 Jun 2020 15:13:51 -0700 (PDT)
MIME-Version: 1.0
References: <20200625141357.910330-1-jakub@cloudflare.com> <20200625141357.910330-3-jakub@cloudflare.com>
 <CAEf4Bzar93mCMm5vgMiYu6_m2N=icv2Wgmy2ohuKoQr810Kk1w@mail.gmail.com> <87imfema7n.fsf@cloudflare.com>
In-Reply-To: <87imfema7n.fsf@cloudflare.com>
From:   Andrii Nakryiko <andrii.nakryiko@gmail.com>
Date:   Fri, 26 Jun 2020 15:13:40 -0700
Message-ID: <CAEf4BzbW+xVRmxhmm35CbArzXTTaXJ_ByK2UKB3XCnvwhNE7xg@mail.gmail.com>
Subject: Re: [PATCH bpf-next v3 2/4] bpf, netns: Keep attached programs in bpf_prog_array
To:     Jakub Sitnicki <jakub@cloudflare.com>
Cc:     bpf <bpf@vger.kernel.org>, Networking <netdev@vger.kernel.org>,
        kernel-team <kernel-team@cloudflare.com>
Content-Type: text/plain; charset="UTF-8"
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

On Fri, Jun 26, 2020 at 2:45 AM Jakub Sitnicki <jakub@cloudflare.com> wrote:
>
> On Thu, Jun 25, 2020 at 10:50 PM CEST, Andrii Nakryiko wrote:
> > On Thu, Jun 25, 2020 at 7:17 AM Jakub Sitnicki <jakub@cloudflare.com> wrote:
> >>
> >> Prepare for having multi-prog attachments for new netns attach types by
> >> storing programs to run in a bpf_prog_array, which is well suited for
> >> iterating over programs and running them in sequence.
> >>
> >> After this change bpf(PROG_QUERY) may block to allocate memory in
> >> bpf_prog_array_copy_to_user() for collected program IDs. This forces a
> >> change in how we protect access to the attached program in the query
> >> callback. Because bpf_prog_array_copy_to_user() can sleep, we switch from
> >> an RCU read lock to holding a mutex that serializes updaters.
> >>
> >> Because we allow only one BPF flow_dissector program to be attached to
> >> netns at all times, the bpf_prog_array pointed by net->bpf.run_array is
> >> always either detached (null) or one element long.
> >>
> >> No functional changes intended.
> >>
> >> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
> >> ---
> >
> > I wonder if instead of NULL prog_array, it's better to just use a
> > dummy empty (but allocated) array. Might help eliminate some of the
> > IFs, maybe even in the hot path.
>
> That was my initial approach, which I abandoned seeing that it leads to
> replacing NULL prog_array checks in flow_dissector with
> bpf_prog_array_is_empty() checks to determine which netns has a BPF
> program attached. So no IFs gone there.
>
> While on the hot path, where we run the program, we probably would still
> be left with an IF checking for empty prog_array to avoid building the
> context if no progs will RUN.
>
> The checks I'm referring to are on attach path, in
> flow_dissector_bpf_prog_attach_check(), and hot-path,
> __skb_flow_dissect().
>

Fair enough.

> >
> >
> >>  include/net/netns/bpf.h    |   5 +-
> >>  kernel/bpf/net_namespace.c | 120 +++++++++++++++++++++++++------------
> >>  net/core/flow_dissector.c  |  19 +++---
> >>  3 files changed, 96 insertions(+), 48 deletions(-)
> >>
> >
> > [...]
> >
> >
> >>
> >> +/* Must be called with netns_bpf_mutex held. */
> >> +static int __netns_bpf_prog_query(const union bpf_attr *attr,
> >> +                                 union bpf_attr __user *uattr,
> >> +                                 struct net *net,
> >> +                                 enum netns_bpf_attach_type type)
> >> +{
> >> +       __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
> >> +       struct bpf_prog_array *run_array;
> >> +       u32 prog_cnt = 0, flags = 0;
> >> +
> >> +       run_array = rcu_dereference_protected(net->bpf.run_array[type],
> >> +                                             lockdep_is_held(&netns_bpf_mutex));
> >> +       if (run_array)
> >> +               prog_cnt = bpf_prog_array_length(run_array);
> >> +
> >> +       if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
> >> +               return -EFAULT;
> >> +       if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
> >> +               return -EFAULT;
> >> +       if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
> >> +               return 0;
> >> +
> >> +       return bpf_prog_array_copy_to_user(run_array, prog_ids,
> >> +                                          attr->query.prog_cnt);
> >
> > It doesn't seem like bpf_prog_array_copy_to_user can handle NULL run_array
>
> Correct. And we never invoke it when run_array is NULL because then
> prog_cnt == 0.

Oh, that !prog_cnt above, right.. it's easy to miss.

>
> >
> >> +}
> >> +
> >>  int netns_bpf_prog_query(const union bpf_attr *attr,
> >>                          union bpf_attr __user *uattr)
> >>  {
> >> -       __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
> >> -       u32 prog_id, prog_cnt = 0, flags = 0;
> >>         enum netns_bpf_attach_type type;
> >> -       struct bpf_prog *attached;
> >>         struct net *net;
> >> +       int ret;
> >>
> >>         if (attr->query.query_flags)
> >>                 return -EINVAL;
> >
> > [...]

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <SRS0=o7vX=AH=vger.kernel.org=bpf-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-4.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID,
	DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,
	SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 5B51FC433E0
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 22:15:53 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 337B1208C7
	for <bpf@archiver.kernel.org>; Fri, 26 Jun 2020 22:15:53 +0000 (UTC)
Authentication-Results: mail.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iXSbgvSs"
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S1725909AbgFZWPw (ORCPT <rfc822;bpf@archiver.kernel.org>);
        Fri, 26 Jun 2020 18:15:52 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59104 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S1725852AbgFZWPw (ORCPT <rfc822;bpf@vger.kernel.org>);
        Fri, 26 Jun 2020 18:15:52 -0400
Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 468F4C03E979;
        Fri, 26 Jun 2020 15:15:52 -0700 (PDT)
Received: by mail-qt1-x843.google.com with SMTP id o38so8665102qtf.6;
        Fri, 26 Jun 2020 15:15:52 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=mime-version:references:in-reply-to:from:date:message-id:subject:to
         :cc;
        bh=tvKAOhqSkH1kVxnAWsMOooM0tK/9pcmWBxtwkslUDWY=;
        b=iXSbgvSsrpHE8VacUnaenru4F7slMEtM0HZaPWGM59r0G8qHw11QqhXM0RjXdjsol6
         lv/1zlB4x8r2CEfiix03k53AGqsZZbSypbvyO9aE6ptvjS90pkpfCFk+DmaJ0Hsrqg6s
         YBEKG/weT6ntsYUtzS60k52v0BOQSycPfCqrdkl0tstKHqow2RmCU0SlI7jRcgVd0TqJ
         CWkypRYyckJP2fsuISJkLsvrTyVP6hnlaWscUUs4A9x+lQ6bfEBRzSvelpZxlVtc3B0u
         uwWu9fG2rm4XgupMBr2RblfGqTtubBAlIBBBU4N1VSQvoQUNK0fINJtR2C/M8ub1CPza
         3/vA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:references:in-reply-to:from:date
         :message-id:subject:to:cc;
        bh=tvKAOhqSkH1kVxnAWsMOooM0tK/9pcmWBxtwkslUDWY=;
        b=NZAD1BJ6g79zAUpHLeh7RKWP3yy4Zf3P06wjKpKRyXlouL022mSakvuvJ4YwDID/Xk
         HmsgO6te21MhEMA2D1Rzpthy5CC0WTt2KQU+ulGoKGhjJI/i+fW/rwM9gx0ThCa1ZIyJ
         znireZZtemtbE4Za5X9i4n/zKupTmriwcsZxDa42mhIu2hMXvuoImbwKUi8VT0cN+xUZ
         yes75IAkHizOlaNUWFCYO/siJ17SD5ojOYytglZnaLpSM2CyPzRH7iSIiOCWQPIojTwE
         iuXYw5U85pGCRM12EQ9tMArHznhEDlPH/aZGV+blx3GDp31ljXZpJv4/FtUZJqNnTlIG
         CDeQ==
X-Gm-Message-State: AOAM5319962nxDbzMZlB4P02Ng8M8mnT9ACGheXLO0+yUM7b/YJVkTAV
        ghGyyyMT59ecJKvrwD9NzlBdPhrUbkJq0gwA7g4=
X-Google-Smtp-Source: ABdhPJzsJj/tilxMC0Huj//M6MSqliTjJ59CWX7mmx0iG1P+YOSvTp7bMrOsLvYjGCb0etsRk4BnDDJruic1+tvzg78=
X-Received: by 2002:ac8:19c4:: with SMTP id s4mr800782qtk.117.1593209751465;
 Fri, 26 Jun 2020 15:15:51 -0700 (PDT)
MIME-Version: 1.0
References: <20200625141357.910330-1-jakub@cloudflare.com> <20200625141357.910330-3-jakub@cloudflare.com>
 <CAEf4Bzar93mCMm5vgMiYu6_m2N=icv2Wgmy2ohuKoQr810Kk1w@mail.gmail.com>
 <87imfema7n.fsf@cloudflare.com> <CAEf4BzbW+xVRmxhmm35CbArzXTTaXJ_ByK2UKB3XCnvwhNE7xg@mail.gmail.com>
In-Reply-To: <CAEf4BzbW+xVRmxhmm35CbArzXTTaXJ_ByK2UKB3XCnvwhNE7xg@mail.gmail.com>
From:   Andrii Nakryiko <andrii.nakryiko@gmail.com>
Date:   Fri, 26 Jun 2020 15:15:40 -0700
Message-ID: <CAEf4Bza+LMb+ewvsVasOJhv-KLvri-wHS64ndZmuYf7hD_Oo9Q@mail.gmail.com>
Subject: Re: [PATCH bpf-next v3 2/4] bpf, netns: Keep attached programs in bpf_prog_array
To:     Jakub Sitnicki <jakub@cloudflare.com>
Cc:     bpf <bpf@vger.kernel.org>, Networking <netdev@vger.kernel.org>,
        kernel-team <kernel-team@cloudflare.com>
Content-Type: text/plain; charset="UTF-8"
Sender: bpf-owner@vger.kernel.org
Precedence: bulk
List-ID: <bpf.vger.kernel.org>
X-Mailing-List: bpf@vger.kernel.org

On Fri, Jun 26, 2020 at 3:13 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Jun 26, 2020 at 2:45 AM Jakub Sitnicki <jakub@cloudflare.com> wrote:
> >
> > On Thu, Jun 25, 2020 at 10:50 PM CEST, Andrii Nakryiko wrote:
> > > On Thu, Jun 25, 2020 at 7:17 AM Jakub Sitnicki <jakub@cloudflare.com> wrote:
> > >>
> > >> Prepare for having multi-prog attachments for new netns attach types by
> > >> storing programs to run in a bpf_prog_array, which is well suited for
> > >> iterating over programs and running them in sequence.
> > >>
> > >> After this change bpf(PROG_QUERY) may block to allocate memory in
> > >> bpf_prog_array_copy_to_user() for collected program IDs. This forces a
> > >> change in how we protect access to the attached program in the query
> > >> callback. Because bpf_prog_array_copy_to_user() can sleep, we switch from
> > >> an RCU read lock to holding a mutex that serializes updaters.
> > >>
> > >> Because we allow only one BPF flow_dissector program to be attached to
> > >> netns at all times, the bpf_prog_array pointed by net->bpf.run_array is
> > >> always either detached (null) or one element long.
> > >>
> > >> No functional changes intended.
> > >>
> > >> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
> > >> ---
> > >
> > > I wonder if instead of NULL prog_array, it's better to just use a
> > > dummy empty (but allocated) array. Might help eliminate some of the
> > > IFs, maybe even in the hot path.
> >
> > That was my initial approach, which I abandoned seeing that it leads to
> > replacing NULL prog_array checks in flow_dissector with
> > bpf_prog_array_is_empty() checks to determine which netns has a BPF
> > program attached. So no IFs gone there.
> >
> > While on the hot path, where we run the program, we probably would still
> > be left with an IF checking for empty prog_array to avoid building the
> > context if no progs will RUN.
> >
> > The checks I'm referring to are on attach path, in
> > flow_dissector_bpf_prog_attach_check(), and hot-path,
> > __skb_flow_dissect().
> >
>
> Fair enough.
>

Acked-by: Andrii Nakryiko <andriin@fb.com>

