Skip to contentSkip to conversation

bpf: split eBPF out of NET

PATCH In review Alexei Starovoitov 5 participants 8 messages Oct 24, 2014 – Oct 28, 2014

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: split eBPF out of NET

introduce two configs:
- hidden CONFIG_BPF to select eBPF interpreter that classic socket filters depend on
- visible CONFIG_BPF_SYSCALL (default off) that tracing and sockets can use

that solves several problems:
- tracing and others that wish to use eBPF don't need to depend on NET. They can use BPF_SYSCALL to allow loading from userspace or select BPF to use it directly from kernel in NET-less configs.
- in 3.18 programs cannot be attached to events yet, so don't force it on
- when the rest of eBPF infra is there in 3.19+, it's still useful to switch it off to minimize kernel size

---

bloat-o-meter on x64 shows:
add/remove: 0/60 grow/shrink: 0/2 up/down: 0/-15601 (-15601)

tested with many different config combinations. Hopefully didn't miss anything.

 init/Kconfig        |   14 ++++++++++++++
 kernel/Makefile     |    2 +-
 kernel/bpf/Makefile |    6 +++---
 kernel/bpf/core.c   |    9 +++++++++
 net/Kconfig         |    2 +-
 5 files changed, 28 insertions(+), 5 deletions(-)
init/Kconfig +14 -0
index 3ee28ae..340fd92 100644
@@ -1341,6 +1341,10 @@ config SYSCTL_ARCH_UNALIGN_ALLOW
 config HAVE_PCSPKR_PLATFORM
 	bool
 
+# interpreter that classic socket filters depend on
+config BPF
+	boolean
+
 menuconfig EXPERT
 	bool "Configure standard kernel features (expert users)"
 	# Unhide debug options, to make the on-by-default options visible
@@ -1521,6 +1525,16 @@ config EVENTFD
 
 	  If unsure, say Y.
 
+# syscall, maps, verifier
+config BPF_SYSCALL
+	bool "Enable bpf() system call" if EXPERT
+	select ANON_INODES
+	select BPF
+	default n
+	help
+	  Enable the bpf() system call that allows to manipulate eBPF
+	  programs and maps via file descriptors.
+
 config SHMEM
 	bool "Use full shmem filesystem" if EXPERT
 	default y
kernel/Makefile +1 -1
index dc5c775..17ea6d4 100644
@@ -86,7 +86,7 @@ obj-$(CONFIG_RING_BUFFER) += trace/
 obj-$(CONFIG_TRACEPOINTS) += trace/
 obj-$(CONFIG_IRQ_WORK) += irq_work.o
 obj-$(CONFIG_CPU_PM) += cpu_pm.o
-obj-$(CONFIG_NET) += bpf/
+obj-$(CONFIG_BPF) += bpf/
 
 obj-$(CONFIG_PERF_EVENTS) += events/
 
kernel/bpf/Makefile +3 -3
index 4542723..0daf7f6 100644
@@ -1,5 +1,5 @@
-obj-y := core.o syscall.o verifier.o
-
+obj-y := core.o
+obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o
 ifdef CONFIG_TEST_BPF
-obj-y += test_stub.o
+obj-$(CONFIG_BPF_SYSCALL) += test_stub.o
 endif
kernel/bpf/core.c +9 -0
index f0c30c5..d6594e4 100644
@@ -655,3 +655,12 @@ void bpf_prog_free(struct bpf_prog *fp)
 	schedule_work(&aux->work);
 }
 EXPORT_SYMBOL_GPL(bpf_prog_free);
+
+/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
+ * skb_copy_bits(), so provide a weak definition of it for NET-less config.
+ */
+int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
+			 int len)
+{
+	return -EFAULT;
+}
net/Kconfig +1 -1
index 6272420..99815b5 100644
@@ -6,7 +6,7 @@ menuconfig NET
 	bool "Networking support"
 	select NLATTR
 	select GENERIC_NET_UTILS
-	select ANON_INODES
+	select BPF
 	---help---
 	  Unless you really know what you are doing, you should say Y here.
 	  The reason is that some programs need kernel networking support even
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 13 lines · Alexei Starovoitov

introduce two configs:
- hidden CONFIG_BPF to select eBPF interpreter that classic socket filters depend on
- visible CONFIG_BPF_SYSCALL (default off) that tracing and sockets can use

that solves several problems:
- tracing and others that wish to use eBPF don't need to depend on NET. They can use BPF_SYSCALL to allow loading from userspace or select BPF to use it directly from kernel in NET-less configs.
- in 3.18 programs cannot be attached to events yet, so don't force it on
- when the rest of eBPF infra is there in 3.19+, it's still useful to switch it off to minimize kernel size

Quoted 1 line

Thanks for working on this! A few nits below, but otherwise this looks good to me. Once this gets appropriate reviews from net and bpf folks, please let me know if you want this to go through the net tree, the tiny tree, or some other tree.

Quoted 3 lines

bloat-o-meter on x64 shows:
add/remove: 0/60 grow/shrink: 0/2 up/down: 0/-15601 (-15601)

Very nice! Please do include the bloat-o-meter stats in the commit message.

Quoted 14 lines

tested with many different config combinations. Hopefully didn't miss anything.

 init/Kconfig        |   14 ++++++++++++++
 kernel/Makefile     |    2 +-
 kernel/bpf/Makefile |    6 +++---
 kernel/bpf/core.c   |    9 +++++++++
 net/Kconfig         |    2 +-
 5 files changed, 28 insertions(+), 5 deletions(-)
init/Kconfig +0 -0
index 3ee28ae..340fd92 100644

init/Kconfig config SYSCTL_ARCH_UNALIGN_ALLOW · line 1346

Quoted 6 lines
init/Kconfig +3 -0
@@ -1341,6 +1341,10 @@ config SYSCTL_ARCH_UNALIGN_ALLOW
 config HAVE_PCSPKR_PLATFORM
 	bool
 
+# interpreter that classic socket filters depend on
+config BPF
+	boolean

s/boolean/bool/

Quoted 7 lines
init/Kconfig +1 -0
+
 menuconfig EXPERT
 	bool "Configure standard kernel features (expert users)"
 	# Unhide debug options, to make the on-by-default options visible
@@ -1521,6 +1525,16 @@ config EVENTFD
 
 	  If unsure, say Y.

init/Kconfig config EVENTFD · line 1536

Quoted 10 lines
init/Kconfig +9 -0
 
+# syscall, maps, verifier
+config BPF_SYSCALL
+	bool "Enable bpf() system call" if EXPERT
+	select ANON_INODES
+	select BPF
+	default n
+	help
+	  Enable the bpf() system call that allows to manipulate eBPF
+	  programs and maps via file descriptors.

Not sure this one goes under EXPERT, especially since it currently has "default n".

Quoted 5 lines
kernel/bpf/core.c +0 -0
@@ -655,3 +655,12 @@ void bpf_prog_free(struct bpf_prog *fp)
 	schedule_work(&aux->work);
 }

kernel/bpf/core.c void bpf_prog_free(struct bpf_prog *fp) · line 666

Quoted 10 lines
kernel/bpf/core.c +9 -0
 EXPORT_SYMBOL_GPL(bpf_prog_free);
+
+/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
+ * skb_copy_bits(), so provide a weak definition of it for NET-less config.
+ */
+int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
+			 int len)
+{
+	return -EFAULT;
+}

Please discuss this in the commit message. What are the implications of ending up with this implementation that always returns -EFAULT?

net/Kconfig menuconfig NET · line 9

Quoted 10 lines
net/Kconfig +1 -1
index 6272420..99815b5 100644
@@ -6,7 +6,7 @@ menuconfig NET
 	bool "Networking support"
 	select NLATTR
 	select GENERIC_NET_UTILS
-	select ANON_INODES
+	select BPF

Why does this not need to select ANON_INODES anymore? Did *only* BPF use that, so it only needs to occur via BPF_SYSCALL? If so, can you document that in the commit message?

Quoted 6 lines
net/Kconfig +0 -0
 	---help---
 	  Unless you really know what you are doing, you should say Y here.
 	  The reason is that some programs need kernel networking support even
Signature · 1 line
1.7.9.5
#2 lore raw
Quoted 14 lines · Josh Triplett
Quoted 13 lines · Alexei Starovoitov

introduce two configs:
- hidden CONFIG_BPF to select eBPF interpreter that classic socket filters depend on
- visible CONFIG_BPF_SYSCALL (default off) that tracing and sockets can use

that solves several problems:
- tracing and others that wish to use eBPF don't need to depend on NET. They can use BPF_SYSCALL to allow loading from userspace or select BPF to use it directly from kernel in NET-less configs.
- in 3.18 programs cannot be attached to events yet, so don't force it on
- when the rest of eBPF infra is there in 3.19+, it's still useful to switch it off to minimize kernel size

Quoted 6 lines
Quoted 1 line

Thanks for working on this! A few nits below, but otherwise this looks good to me. Once this gets appropriate reviews from net and bpf folks, please let me know if you want this to go through the net tree, the tiny tree, or some other tree.

Thanks :)
I've sent it to Dave and marked it as 'net', so it's for his net tree. I don't mind if he decides to steer it into net-next when it opens, since changing Kconfig is always tricky. I just felt that this patch deserves to be in 'net' and in 3.18-rc

Quoted 5 lines
Quoted 2 lines

bloat-o-meter on x64 shows:
add/remove: 0/60 grow/shrink: 0/2 up/down: 0/-15601 (-15601)

Very nice! Please do include the bloat-o-meter stats in the commit message.

I don't think that's necessary. eBPF is in early stages of adoption. More things to come, so bloat-o-meter stats will be obsolete very quickly.

Quoted 5 lines

init/Kconfig config SYSCTL_ARCH_UNALIGN_ALLOW · line 1346

Quoted 3 lines
init/Kconfig +3 -0
+# interpreter that classic socket filters depend on
+config BPF
+     boolean

s/boolean/bool/

Is there a difference? I thought it's an alias.

Quoted 11 lines

init/Kconfig config EVENTFD · line 1536

Quoted 8 lines
init/Kconfig +8 -0
+config BPF_SYSCALL
+     bool "Enable bpf() system call" if EXPERT
+     select ANON_INODES
+     select BPF
+     default n
+     help
+       Enable the bpf() system call that allows to manipulate eBPF
+       programs and maps via file descriptors.

Not sure this one goes under EXPERT, especially since it currently has "default n".

I followed the same style as EPOLL, EVENTFD and others in the same category.

Quoted 11 lines

kernel/bpf/core.c void bpf_prog_free(struct bpf_prog *fp) · line 666

Quoted 8 lines
kernel/bpf/core.c +8 -0
+/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
+ * skb_copy_bits(), so provide a weak definition of it for NET-less config.
+ */
+int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
+                      int len)
+{
+     return -EFAULT;
+}

Please discuss this in the commit message. What are the implications of ending up with this implementation that always returns -EFAULT?

because that's what real skb_copy_bits() would return. In this case it's actually irrelevant, since non-socket programs are not allowed to have LD_ABS/LD_IND instructions and I'm only resolving linker error here.
But returning negative error helps prevent bugs in cases where verifier or some in-kernel generated program uses LD_ABS by mistake.
I don't think these type of explanations are necessary in commit logs.

Quoted 10 lines

net/Kconfig menuconfig NET · line 9

Quoted 6 lines
net/Kconfig +1 -1
@@ -6,7 +6,7 @@ menuconfig NET
      bool "Networking support"
      select NLATTR
      select GENERIC_NET_UTILS
-     select ANON_INODES
+     select BPF

Why does this not need to select ANON_INODES anymore? Did *only* BPF use that, so it only needs to occur via BPF_SYSCALL? If so, can you document that in the commit message?

I hope that folks who were following this work on netdev remember commit 38b3629adb8c04 that added it.
So here I'm actually removing this ANON_INODES dependency from NET and moving it into BPF_SYSCALL where it belongs.

btw, the goal of this patch is not tinification, but rather being good citizen and not forcing new syscall on everyone. It was tested with upcoming tracing patches that select BPF instead of NET.
It will also help parallelize the development, since my old predicate-tree into eBPF optimization for vanilla tracing filters:
http://lwn.net/Articles/598545/
can potentially go into tip tree a release earlier. Back then full NET dependency was a show stopper.
This patch finally addresses it.

#3 lore raw
Quoted 22 lines · Alexei Starovoitov
Quoted 14 lines · Josh Triplett
Quoted 13 lines · Alexei Starovoitov

introduce two configs:
- hidden CONFIG_BPF to select eBPF interpreter that classic socket filters depend on
- visible CONFIG_BPF_SYSCALL (default off) that tracing and sockets can use

that solves several problems:
- tracing and others that wish to use eBPF don't need to depend on NET. They can use BPF_SYSCALL to allow loading from userspace or select BPF to use it directly from kernel in NET-less configs.
- in 3.18 programs cannot be attached to events yet, so don't force it on
- when the rest of eBPF infra is there in 3.19+, it's still useful to switch it off to minimize kernel size

Quoted 6 lines
Quoted 1 line

Thanks for working on this! A few nits below, but otherwise this looks good to me. Once this gets appropriate reviews from net and bpf folks, please let me know if you want this to go through the net tree, the tiny tree, or some other tree.

Quoted 5 lines

Thanks :)
I've sent it to Dave and marked it as 'net', so it's for his net tree. I don't mind if he decides to steer it into net-next when it opens, since changing Kconfig is always tricky. I just felt that this patch deserves to be in 'net' and in 3.18-rc

Ah, nice; yes, getting it into 3.18-rc would be excellent if possible.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

bloat-o-meter on x64 shows:
add/remove: 0/60 grow/shrink: 0/2 up/down: 0/-15601 (-15601)

Very nice! Please do include the bloat-o-meter stats in the commit message.

I don't think that's necessary. eBPF is in early stages of adoption. More things to come, so bloat-o-meter stats will be obsolete very quickly.

I don't mean the full list of symbols, just the summary saying this saves 15k.

Quoted 7 lines
Quoted 5 lines

init/Kconfig config SYSCTL_ARCH_UNALIGN_ALLOW · line 1346

Quoted 3 lines
init/Kconfig +3 -0
+# interpreter that classic socket filters depend on
+config BPF
+     boolean

s/boolean/bool/

Is there a difference? I thought it's an alias.

It's an alias, but almost everything uses "bool":

~/src/linux$ git grep -w bool -- '*Kconfig*' | wc -l 7064
~/src/linux$ git grep -w boolean -- '*Kconfig*' | wc -l 94

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
init/Kconfig +8 -0
+config BPF_SYSCALL
+     bool "Enable bpf() system call" if EXPERT
+     select ANON_INODES
+     select BPF
+     default n
+     help
+       Enable the bpf() system call that allows to manipulate eBPF
+       programs and maps via file descriptors.
Quoted 5 lines
Quoted 2 lines

Not sure this one goes under EXPERT, especially since it currently has "default n".

I followed the same style as EPOLL, EVENTFD and others in the same category.

I was thinking of CROSS_MEMORY_ATTACH and FHANDLE in the same file.

Quoted 12 lines
Quoted 11 lines

kernel/bpf/core.c void bpf_prog_free(struct bpf_prog *fp) · line 666

Quoted 8 lines
kernel/bpf/core.c +8 -0
+/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
+ * skb_copy_bits(), so provide a weak definition of it for NET-less config.
+ */
+int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
+                      int len)
+{
+     return -EFAULT;
+}

Please discuss this in the commit message. What are the implications of ending up with this implementation that always returns -EFAULT?

Quoted 7 lines

because that's what real skb_copy_bits() would return. In this case it's actually irrelevant, since non-socket programs are not allowed to have LD_ABS/LD_IND instructions and I'm only resolving linker error here.
But returning negative error helps prevent bugs in cases where verifier or some in-kernel generated program uses LD_ABS by mistake.

Makes sense.

Quoted 14 lines

I don't think these type of explanations are necessary in commit logs.

Quoted 10 lines

net/Kconfig menuconfig NET · line 9

Quoted 6 lines
net/Kconfig +1 -1
@@ -6,7 +6,7 @@ menuconfig NET
      bool "Networking support"
      select NLATTR
      select GENERIC_NET_UTILS
-     select ANON_INODES
+     select BPF

Why does this not need to select ANON_INODES anymore? Did *only* BPF use that, so it only needs to occur via BPF_SYSCALL? If so, can you document that in the commit message?

Quoted 4 lines

I hope that folks who were following this work on netdev remember commit 38b3629adb8c04 that added it.
So here I'm actually removing this ANON_INODES dependency from NET and moving it into BPF_SYSCALL where it belongs.

Thanks for the clarification.

Quoted 2 lines

btw, the goal of this patch is not tinification, but rather being good citizen and not forcing new syscall on everyone.

A critical part of the tinification effort is not having the kernel get gratuitously bigger in other areas while we're trying to shrink it. So, I really appreciate your work. :)

- Josh Triplett

#4 lore raw
Quoted 9 lines · Josh Triplett
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
init/Kconfig +8 -0
+config BPF_SYSCALL
+     bool "Enable bpf() system call" if EXPERT
+     select ANON_INODES
+     select BPF
+     default n
+     help
+       Enable the bpf() system call that allows to manipulate eBPF
+       programs and maps via file descriptors.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Not sure this one goes under EXPERT, especially since it currently has "default n".

I followed the same style as EPOLL, EVENTFD and others in the same category.

I was thinking of CROSS_MEMORY_ATTACH and FHANDLE in the same file.

Those indeed look like better examples.
With if EXPERT and default n, you need to enable EXPERT before you can enable the syscall, which is probably not what you want.

Thanks!

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#5 lore raw
In reply to Josh Triplett
Quoted 23 lines · Josh Triplett
Quoted 22 lines · Alexei Starovoitov
Quoted 20 lines · Josh Triplett
Quoted 13 lines · Alexei Starovoitov

introduce two configs:
- hidden CONFIG_BPF to select eBPF interpreter that classic socket filters depend on
- visible CONFIG_BPF_SYSCALL (default off) that tracing and sockets can use

that solves several problems:
- tracing and others that wish to use eBPF don't need to depend on NET. They can use BPF_SYSCALL to allow loading from userspace or select BPF to use it directly from kernel in NET-less configs.
- in 3.18 programs cannot be attached to events yet, so don't force it on
- when the rest of eBPF infra is there in 3.19+, it's still useful to switch it off to minimize kernel size

Quoted 1 line

Thanks for working on this! A few nits below, but otherwise this looks good to me. Once this gets appropriate reviews from net and bpf folks, please let me know if you want this to go through the net tree, the tiny tree, or some other tree.

Quoted 7 lines
Quoted 5 lines

Thanks :)
I've sent it to Dave and marked it as 'net', so it's for his net tree. I don't mind if he decides to steer it into net-next when it opens, since changing Kconfig is always tricky. I just felt that this patch deserves to be in 'net' and in 3.18-rc

Ah, nice; yes, getting it into 3.18-rc would be excellent if possible.

Fully agreed, BPF_SYSCALL defaulting to 'n' _for the time being_ would also give an option for reducing exposure until the API is further stabilized and in a ready-to-use state.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

bloat-o-meter on x64 shows:
add/remove: 0/60 grow/shrink: 0/2 up/down: 0/-15601 (-15601)

Very nice! Please do include the bloat-o-meter stats in the commit message.

Quoted 6 lines
Quoted 3 lines

I don't think that's necessary. eBPF is in early stages of adoption. More things to come, so bloat-o-meter stats will be obsolete very quickly.

I don't mean the full list of symbols, just the summary saying this saves 15k.

It might probably help to more easily identify from the log which commits are related to a tinyfication perspective. Perhaps Dave can still squash that into the commit log.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

init/Kconfig config SYSCTL_ARCH_UNALIGN_ALLOW · line 1346

Quoted 3 lines
init/Kconfig +3 -0
+# interpreter that classic socket filters depend on
+config BPF
+     boolean

s/boolean/bool/

Quoted 8 lines
Quoted 1 line

Is there a difference? I thought it's an alias.

It's an alias, but almost everything uses "bool":

~/src/linux$ git grep -w bool -- '*Kconfig*' | wc -l 7064
~/src/linux$ git grep -w boolean -- '*Kconfig*' | wc -l 94

Actually, shouldn't we get rid of the alias then? Same accounts for def_bool and def_boolean ... it would help to avoid confusion to just have a single term for each.

Anyway, the rest looks good to me, thanks.

I am totally fine of having it under EXPERT for now for the reasons mentioned above. This can still be lifted later on.

maintainer
#6 lore raw
In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>
Date: Thu, 23 Oct 2014 18:41:08 -0700

Quoted 13 lines

introduce two configs:
- hidden CONFIG_BPF to select eBPF interpreter that classic socket filters depend on
- visible CONFIG_BPF_SYSCALL (default off) that tracing and sockets can use

that solves several problems:
- tracing and others that wish to use eBPF don't need to depend on NET. They can use BPF_SYSCALL to allow loading from userspace or select BPF to use it directly from kernel in NET-less configs.
- in 3.18 programs cannot be attached to events yet, so don't force it on
- when the rest of eBPF infra is there in 3.19+, it's still useful to switch it off to minimize kernel size

Quoted 7 lines

---

bloat-o-meter on x64 shows:
add/remove: 0/60 grow/shrink: 0/2 up/down: 0/-15601 (-15601)

tested with many different config combinations. Hopefully didn't miss anything.

Applied with two changes:

1) boolean --> bool
2) Moved bloat-o-meter and testing information into commit message.

Thanks.

#7 lore raw
Quoted 22 lines · David Miller

From: Alexei Starovoitov <[email protected]>
Date: Thu, 23 Oct 2014 18:41:08 -0700

Quoted 19 lines

introduce two configs:
- hidden CONFIG_BPF to select eBPF interpreter that classic socket filters depend on
- visible CONFIG_BPF_SYSCALL (default off) that tracing and sockets can use

that solves several problems:
- tracing and others that wish to use eBPF don't need to depend on NET. They can use BPF_SYSCALL to allow loading from userspace or select BPF to use it directly from kernel in NET-less configs.
- in 3.18 programs cannot be attached to events yet, so don't force it on
- when the rest of eBPF infra is there in 3.19+, it's still useful to switch it off to minimize kernel size

---

bloat-o-meter on x64 shows:
add/remove: 0/60 grow/shrink: 0/2 up/down: 0/-15601 (-15601)

Quoted 8 lines
Quoted 1 line

tested with many different config combinations. Hopefully didn't miss anything.

Applied with two changes:

1) boolean --> bool
2) Moved bloat-o-meter and testing information into commit message.

Thanks.

Thank you for taking care of it!