Skip to contentSkip to conversation

bpf: remove the cgroup -> bpf header dependecy

PATCH Superseded Jakub Kicinski 3 participants 8 messages Dec 15, 2021

Subsystems (4)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover bpf: remove the cgroup -> bpf header dependecy

Changes to bpf.h tend to clog up our build systems. The netdev/bpf build bot does incremental builds to save time (reusing the build directory to only rebuild changed objects).

This is the rough breakdown of how many objects needs to be rebuilt based on file touched:

kernel.h      40633
bpf.h         17881
bpf-cgroup.h  17875
skbuff.h      10696
bpf-netns.h    7604
netdevice.h    7452
filter.h       5003
tcp.h          4048
sock.h         4959

As the stats show touching bpf.h is _very_ expensive.

Bulk of the objects get rebuilt because MM includes cgroup headers. Luckily bpf-cgroup.h does not fundamentally depend on bpf.h so we can break that dependency and reduce the number of objects.

With the patches applied touching bpf.h causes 5019 objects to be rebuilt (17881 / 5019 = 3.56x). That's pretty much down to filter.h plus noise.

v2:
Try to make the new headers wider in scope. Collapse bpf-link and bpf-cgroup-types into one header, which may serve as "BPF kernel API" header in the future if needed. Rename bpf-cgroup-storage.h to bpf-inlines.h.

Add a fix for the s390 build issue.

Jakub Kicinski (4):
bpf: add header for enum bpf_cgroup_storage_type and bpf_link
bpf: create a header for inline helpers
add includes masked by cgroup -> bpf dependency
bpf: remove the cgroup -> bpf header dependecy

 arch/s390/mm/hugetlbpage.c       |  1 +
 include/linux/bpf-cgroup-types.h | 29 +++++++++++++++++++++++++++++
 include/linux/bpf-cgroup.h       | 13 +++----------
 include/linux/bpf-inlines.h      | 17 +++++++++++++++++
 include/linux/bpf.h              | 18 +-----------------
 kernel/bpf/helpers.c             |  1 +
 kernel/bpf/local_storage.c       |  1 +
 7 files changed, 53 insertions(+), 27 deletions(-)
 create mode 100644 include/linux/bpf-cgroup-types.h
 create mode 100644 include/linux/bpf-inlines.h
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/4 bpf: add header for enum bpf_cgroup_storage_type and bpf_link

enum bpf_cgroup_storage_type and struct bpf_link are needed both in bpf.h and bpf-cgroup.h. Since we want to break the cgroup -> bpf dependency we need to place it in its own header.

We can transform this header into some form of "kernel API" header for BPF if more places want to include a lightweight version of bpf.h. I'm calling it bpf-cgroup-types.h for now mostly because naming is hard...

---
 include/linux/bpf-cgroup-types.h | 29 +++++++++++++++++++++++++++++
 include/linux/bpf.h              | 18 +-----------------
 2 files changed, 30 insertions(+), 17 deletions(-)
 create mode 100644 include/linux/bpf-cgroup-types.h
include/linux/bpf-cgroup-types.h new file +29 -0
new file mode 100644
index 000000000000..c916fd3f4a0f
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _BPF_CGROUP_TYPES_H
+#define _BPF_CGROUP_TYPES_H
+
+#include <uapi/linux/bpf.h>
+
+#include <linux/workqueue.h>
+
+struct bpf_prog;
+struct bpf_link_ops;
+
+struct bpf_link {
+	atomic64_t refcnt;
+	u32 id;
+	enum bpf_link_type type;
+	const struct bpf_link_ops *ops;
+	struct bpf_prog *prog;
+	struct work_struct work;
+};
+
+enum bpf_cgroup_storage_type {
+	BPF_CGROUP_STORAGE_SHARED,
+	BPF_CGROUP_STORAGE_PERCPU,
+	__BPF_CGROUP_STORAGE_MAX
+};
+
+#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
+
+#endif
include/linux/bpf.h +1 -17
index 965fffaf0308..1e16243623fe 100644
@@ -22,6 +22,7 @@
 #include <linux/sched/mm.h>
 #include <linux/slab.h>
 #include <linux/percpu-refcount.h>
+#include <linux/bpf-cgroup-types.h>
 #include <linux/bpfptr.h>
 
 struct bpf_verifier_env;
@@ -550,14 +551,6 @@ struct bpf_prog_offload {
 	u32			jited_len;
 };
 
-enum bpf_cgroup_storage_type {
-	BPF_CGROUP_STORAGE_SHARED,
-	BPF_CGROUP_STORAGE_PERCPU,
-	__BPF_CGROUP_STORAGE_MAX
-};
-
-#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
-
 /* The longest tracepoint has 12 args.
  * See include/trace/bpf_probe.h
  */
@@ -958,15 +951,6 @@ struct bpf_array_aux {
 	struct work_struct work;
 };
 
-struct bpf_link {
-	atomic64_t refcnt;
-	u32 id;
-	enum bpf_link_type type;
-	const struct bpf_link_ops *ops;
-	struct bpf_prog *prog;
-	struct work_struct work;
-};
-
 struct bpf_link_ops {
 	void (*release)(struct bpf_link *link);
 	void (*dealloc)(struct bpf_link *link);
Signature · 1 line
2.31.1
#2 lore raw

PATCH v2 2/4 bpf: create a header for inline helpers

In reply to Jakub Kicinski

static inlines are notorious for creating inter-header dependencies. Create a dedicated header where we can place include-heavy inlines for bpf.

Put cgroup_storage_type() there. It needs to deference bpf_map and access cgroup types thus creating a dependency between cgroup and bpf.

---
 include/linux/bpf-cgroup.h  |  9 ---------
 include/linux/bpf-inlines.h | 17 +++++++++++++++++
 kernel/bpf/helpers.c        |  1 +
 kernel/bpf/local_storage.c  |  1 +
 4 files changed, 19 insertions(+), 9 deletions(-)
 create mode 100644 include/linux/bpf-inlines.h
include/linux/bpf-cgroup.h +0 -9
index 11820a430d6c..12474516e0be 100644
@@ -194,15 +194,6 @@ int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
 					    int optname, void *optval,
 					    int *optlen, int retval);
 
-static inline enum bpf_cgroup_storage_type cgroup_storage_type(
-	struct bpf_map *map)
-{
-	if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
-		return BPF_CGROUP_STORAGE_PERCPU;
-
-	return BPF_CGROUP_STORAGE_SHARED;
-}
-
 struct bpf_cgroup_storage *
 cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
 		      void *key, bool locked);
include/linux/bpf-inlines.h new file +17 -0
new file mode 100644
index 000000000000..73d8429d22f1
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _BPF_INLINES_H
+#define _BPF_INLINES_H
+
+#include <linux/bpf.h>
+#include <linux/bpf-cgroup.h>
+
+static inline enum bpf_cgroup_storage_type cgroup_storage_type(
+	struct bpf_map *map)
+{
+	if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
+		return BPF_CGROUP_STORAGE_PERCPU;
+
+	return BPF_CGROUP_STORAGE_SHARED;
+}
+
+#endif
kernel/bpf/helpers.c +1 -0
index 8babae03d30a..d33216287b4d 100644
@@ -2,6 +2,7 @@
 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  */
 #include <linux/bpf.h>
+#include <linux/bpf-inlines.h>
 #include <linux/rcupdate.h>
 #include <linux/random.h>
 #include <linux/smp.h>
kernel/bpf/local_storage.c +1 -0
index 035e9e3a7132..bc8cfe03a37e 100644
@@ -1,5 +1,6 @@
 //SPDX-License-Identifier: GPL-2.0
 #include <linux/bpf-cgroup.h>
+#include <linux/bpf-inlines.h>
 #include <linux/bpf.h>
 #include <linux/bpf_local_storage.h>
 #include <linux/btf.h>
Signature · 1 line
2.31.1
#3 lore raw

PATCH v2 3/4 add includes masked by cgroup -> bpf dependency

In reply to Jakub Kicinski

cgroup pulls in BPF which pulls in a lot of includes. We're about to break that chain so fix those who were depending on it.

---
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 arch/s390/mm/hugetlbpage.c | 1 +
 1 file changed, 1 insertion(+)
arch/s390/mm/hugetlbpage.c +1 -0
index da36d13ffc16..082793d497ec 100644
@@ -9,6 +9,7 @@
 #define KMSG_COMPONENT "hugetlb"
 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
 
+#include <asm/pgalloc.h>
 #include <linux/mm.h>
 #include <linux/hugetlb.h>
 #include <linux/mman.h>
Signature · 1 line
2.31.1
#4 lore raw

PATCH v2 4/4 bpf: remove the cgroup -> bpf header dependecy

In reply to Jakub Kicinski

Now that the stage has been set and actors are in place remove the header dependency between cgroup and bpf.h.

This reduces the incremental build size of x86 allmodconfig after bpf.h was touched from ~17k objects rebuilt to ~5k objects. bpf.h is 2.2kLoC and is modified relatively often.

---
 include/linux/bpf-cgroup.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/bpf-cgroup.h +3 -1
index 12474516e0be..4c932d47e7f2 100644
@@ -2,7 +2,8 @@
 #ifndef _BPF_CGROUP_H
 #define _BPF_CGROUP_H
 
-#include <linux/bpf.h>
+#include <linux/bpf-cgroup-types.h>
+#include <linux/bpf-link.h>
 #include <linux/errno.h>
 #include <linux/jump_label.h>
 #include <linux/percpu.h>
@@ -16,6 +17,7 @@ struct cgroup;
 struct sk_buff;
 struct bpf_map;
 struct bpf_prog;
+struct bpf_prog_aux;
 struct bpf_sock_ops_kern;
 struct bpf_cgroup_storage;
 struct ctl_table;
Signature · 1 line
2.31.1
#5 lore raw
Quoted 18 lines · Jakub Kicinski

Now that the stage has been set and actors are in place remove the header dependency between cgroup and bpf.h.

This reduces the incremental build size of x86 allmodconfig after bpf.h was touched from ~17k objects rebuilt to ~5k objects. bpf.h is 2.2kLoC and is modified relatively often.

---
 include/linux/bpf-cgroup.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/bpf-cgroup.h +0 -0
index 12474516e0be..4c932d47e7f2 100644

include/linux/bpf-cgroup.h line 6

Quoted 6 lines
include/linux/bpf-cgroup.h +2 -1
@@ -2,7 +2,8 @@
 #ifndef _BPF_CGROUP_H
 #define _BPF_CGROUP_H
 
-#include <linux/bpf.h>
+#include <linux/bpf-cgroup-types.h>
+#include <linux/bpf-link.h>

Borked rebase with stale header I guess ?

Could you try just patch 1 with bpf-cgroup-types.h and do s/bpf.h/bpf-cgroup-types.h/ here in bpf-cgroup.h as patch 2
while moving cgroup_storage_type() function to bpf.h ? Patch 1 will add +#include <linux/bpf-cgroup-types.h> to linux/bpf.h,
so cgroup_storage_type() will have everything it needs there.

I could be still missing something.

#6 lore raw
Quoted 25 lines · On Tue, 14 Dec 2021 21:15:15 -0800 Alexei Starovoitov wrote:

include/linux/bpf-cgroup.h line 6

Quoted 13 lines · Jakub Kicinski

Now that the stage has been set and actors are in place remove the header dependency between cgroup and bpf.h.

This reduces the incremental build size of x86 allmodconfig after bpf.h was touched from ~17k objects rebuilt to ~5k objects. bpf.h is 2.2kLoC and is modified relatively often.

#ifndef _BPF_CGROUP_H
#define _BPF_CGROUP_H

include/linux/bpf-cgroup.h +2 -1
-#include <linux/bpf.h>
+#include <linux/bpf-cgroup-types.h>
+#include <linux/bpf-link.h>  

Borked rebase with stale header I guess ?

Could you try just patch 1 with bpf-cgroup-types.h and do s/bpf.h/bpf-cgroup-types.h/ here in bpf-cgroup.h as patch 2
while moving cgroup_storage_type() function to bpf.h ? Patch 1 will add +#include <linux/bpf-cgroup-types.h> to linux/bpf.h,
so cgroup_storage_type() will have everything it needs there.

Quoted 1 line

I could be still missing something.

Right, I must misremembered the reasoning, move to bpf.h should work just fine.

#7 lore raw
In reply to Jakub Kicinski
I love your patch! Yet something to improve:

Hi Jakub,

I love your patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Jakub-Kicinski/bpf-remove-the-cgroup-bpf-header-dependecy/20211215-103243
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: nds32-allnoconfig (https://download.01.org/0day-ci/archive/20211215/202112151324.LTeefTnQ-lkp(a)intel.com/config)
compiler: nds32le-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/09a6dd8be59ad147fccc06cec41a89cca25fb5da
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Jakub-Kicinski/bpf-remove-the-cgroup-bpf-header-dependecy/20211215-103243 git checkout 09a6dd8be59ad147fccc06cec41a89cca25fb5da
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=nds32 SHELL=/bin/bash drivers/of/

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from include/linux/cgroup-defs.h:22,
from include/linux/cgroup.h:28, from include/linux/memcontrol.h:13, from include/linux/swap.h:9,
from include/linux/suspend.h:5, from include/linux/regulator/consumer.h:35, from include/linux/amba/bus.h:19, from drivers/of/platform.c:14:

>> include/linux/bpf-cgroup.h:6:10: fatal error: linux/bpf-link.h: No such file or directory
       6 | #include <linux/bpf-link.h>
         |          ^~~~~~~~~~~~~~~~~~

compilation terminated.

vim +6 include/linux/bpf-cgroup.h

4
5 #include <linux/bpf-cgroup-types.h>

   > 6	#include <linux/bpf-link.h>
     7	#include <linux/errno.h>
     8	#include <linux/jump_label.h>
     9	#include <linux/percpu.h>
    10	#include <linux/percpu-refcount.h>
    11	#include <linux/rbtree.h>
    12	#include <uapi/linux/bpf.h>
    13	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org