Skip to contentSkip to conversation

net: filter: BPF 'JIT' compiler for PPC64

PATCH Superseded Matt Evans 5 participants 17 messages Jul 18, 2011 – Jul 21, 2011

Subsystems (5)
lore
Share & tools
lore

Matt Evans

author
#0 lore raw

PATCH 1/1 net: filter: BPF 'JIT' compiler for PPC64

An implementation of a code generator for BPF programs to speed up packet filtering on PPC64, inspired by Eric Dumazet's x86-64 version.

Filter code is generated as an ABI-compliant function in module_alloc()'d mem with stackframe & prologue/epilogue generated if required (simple filters don't need anything more than an li/blr). The filter's local variables, M[], live in registers. Supports all BPF opcodes, although "complicated" loads from negative packet offsets (e.g. SKF_LL_OFF) are not yet supported.

There are a couple of further optimisations left for future work; many-pass assembly with branch-reach reduction and a register allocator to push M[] variables into volatile registers would improve the code quality further.

This currently supports big-endian 64-bit PowerPC only (but is fairly simple to port to PPC32 or LE!).

Enabled in the same way as x86-64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or, enabled with extra debug output:

echo 2 > /proc/sys/net/core/bpf_jit_enable

---

Since the RFC post, this has incorporated the bugfixes/tidies from review plus a couple more found in further testing, plus some general/comment tidies.

 arch/powerpc/Kconfig                  |    1 +
 arch/powerpc/Makefile                 |    3 +-
 arch/powerpc/include/asm/ppc-opcode.h |   40 ++
 arch/powerpc/net/Makefile             |    4 +
 arch/powerpc/net/bpf_jit.S            |  138 +++++++
 arch/powerpc/net/bpf_jit.h            |  227 +++++++++++
 arch/powerpc/net/bpf_jit_comp.c       |  694 +++++++++++++++++++++++++++++++++
 7 files changed, 1106 insertions(+), 1 deletions(-)
arch/powerpc/Kconfig +1 -0
index 2729c66..39860fc 100644
@@ -134,6 +134,7 @@ config PPC
 	select GENERIC_IRQ_SHOW_LEVEL
 	select HAVE_RCU_TABLE_FREE if SMP
 	select HAVE_SYSCALL_TRACEPOINTS
+	select HAVE_BPF_JIT if PPC64
 
 config EARLY_PRINTK
 	bool
arch/powerpc/Makefile +2 -1
index b7212b6..b94740f 100644
@@ -154,7 +154,8 @@ core-y				+= arch/powerpc/kernel/ \
 				   arch/powerpc/lib/ \
 				   arch/powerpc/sysdev/ \
 				   arch/powerpc/platforms/ \
-				   arch/powerpc/math-emu/
+				   arch/powerpc/math-emu/ \
+				   arch/powerpc/net/
 core-$(CONFIG_XMON)		+= arch/powerpc/xmon/
 core-$(CONFIG_KVM) 		+= arch/powerpc/kvm/
 
arch/powerpc/include/asm/ppc-opcode.h +40 -0
index e472659..e980faa 100644
@@ -71,6 +71,42 @@
 #define PPC_INST_ERATSX			0x7c000126
 #define PPC_INST_ERATSX_DOT		0x7c000127
 
+/* Misc instructions for BPF compiler */
+#define PPC_INST_LD			0xe8000000
+#define PPC_INST_LHZ			0xa0000000
+#define PPC_INST_LWZ			0x80000000
+#define PPC_INST_STD			0xf8000000
+#define PPC_INST_STDU			0xf8000001
+#define PPC_INST_MFLR			0x7c0802a6
+#define PPC_INST_MTLR			0x7c0803a6
+#define PPC_INST_CMPWI			0x2c000000
+#define PPC_INST_CMPDI			0x2c200000
+#define PPC_INST_CMPLW			0x7c000040
+#define PPC_INST_CMPLWI			0x28000000
+#define PPC_INST_ADDI			0x38000000
+#define PPC_INST_ADDIS			0x3c000000
+#define PPC_INST_ADD			0x7c000214
+#define PPC_INST_SUB			0x7c000050
+#define PPC_INST_BLR			0x4e800020
+#define PPC_INST_BLRL			0x4e800021
+#define PPC_INST_MULLW			0x7c0001d6
+#define PPC_INST_MULHWU			0x7c000016
+#define PPC_INST_MULLI			0x1c000000
+#define PPC_INST_DIVWU			0x7c0003d6
+#define PPC_INST_RLWINM			0x54000000
+#define PPC_INST_RLDICR			0x78000004
+#define PPC_INST_SLW			0x7c000030
+#define PPC_INST_SRW			0x7c000430
+#define PPC_INST_AND			0x7c000038
+#define PPC_INST_ANDDOT			0x7c000039
+#define PPC_INST_OR			0x7c000378
+#define PPC_INST_ANDI			0x70000000
+#define PPC_INST_ORI			0x60000000
+#define PPC_INST_ORIS			0x64000000
+#define PPC_INST_NEG			0x7c0000d0
+#define PPC_INST_BRANCH			0x48000000
+#define PPC_INST_BRANCH_COND		0x40800000
+
 /* macros to insert fields into opcodes */
 #define __PPC_RA(a)	(((a) & 0x1f) << 16)
 #define __PPC_RB(b)	(((b) & 0x1f) << 11)
@@ -83,6 +119,10 @@
 #define __PPC_T_TLB(t)	(((t) & 0x3) << 21)
 #define __PPC_WC(w)	(((w) & 0x3) << 21)
 #define __PPC_WS(w)	(((w) & 0x1f) << 11)
+#define __PPC_SH(s)	__PPC_WS(s)
+#define __PPC_MB(s)	(((s) & 0x1f) << 6)
+#define __PPC_ME(s)	(((s) & 0x1f) << 1)
+#define __PPC_BI(s)	(((s) & 0x1f) << 16)
 
 /*
  * Only use the larx hint bit on 64bit CPUs. e500v1/v2 based CPUs will treat a
arch/powerpc/net/Makefile new file +4 -0
new file mode 100644
index 0000000..90568c3
@@ -0,0 +1,4 @@
+#
+# Arch-specific network modules
+#
+obj-$(CONFIG_BPF_JIT) += bpf_jit.o bpf_jit_comp.o
arch/powerpc/net/bpf_jit.S new file +138 -0

138 lines not on this page · raw message

arch/powerpc/net/bpf_jit.h new file +227 -0

227 lines not on this page · raw message

arch/powerpc/net/bpf_jit_comp.c new file +694 -0

694 lines not on this page · raw message

#1 lore raw
Quoted 22 lines · Matt Evans

An implementation of a code generator for BPF programs to speed up packet filtering on PPC64, inspired by Eric Dumazet's x86-64 version.

Filter code is generated as an ABI-compliant function in module_alloc()'d mem with stackframe & prologue/epilogue generated if required (simple filters don't need anything more than an li/blr). The filter's local variables, M[], live in registers. Supports all BPF opcodes, although "complicated" loads from negative packet offsets (e.g. SKF_LL_OFF) are not yet supported.

There are a couple of further optimisations left for future work; many-pass assembly with branch-reach reduction and a register allocator to push M[] variables into volatile registers would improve the code quality further.

This currently supports big-endian 64-bit PowerPC only (but is fairly simple to port to PPC32 or LE!).

Enabled in the same way as x86-64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or, enabled with extra debug output:

Quoted 7 lines

echo 2 > /proc/sys/net/core/bpf_jit_enable

---

Since the RFC post, this has incorporated the bugfixes/tidies from review plus a couple more found in further testing, plus some general/comment tidies.

Hi Matt

A small note about SEEN_XREG usage in PPC against x86_64 :

In x86_64, XREG is stored in EBX : I had to save/restore it in function prologue epilogue. And set it to zero in prologue to avoid leak of kernel information.

In PPC, you chose a scratch register, so you only have to zero it in function prologue, if X is ever read.

So in PPC SEEN_XREG only is to be set of X is read, not if written.

So you dont have to set SEEN_XREG bit in this part :

arch/powerpc/net/bpf_jit_comp.c line 294

Quoted 4 lines
arch/powerpc/net/bpf_jit_comp.c new file +4 -0
+		case BPF_S_MISC_TAX: /* X = A */
+			ctx->seen |= SEEN_XREG;
+			PPC_MR(r_X, r_A);
+			break;

and in this part :

arch/powerpc/net/bpf_jit_comp.c line 307

Quoted 4 lines
arch/powerpc/net/bpf_jit_comp.c new file +4 -0
+		case BPF_S_LDX_IMM: /* X = K */
+			ctx->seen |= SEEN_XREG;
+			PPC_LI32(r_X, K);
+			break;

and :

arch/powerpc/net/bpf_jit_comp.c line 315

Quoted 4 lines
arch/powerpc/net/bpf_jit_comp.c new file +4 -0
+		case BPF_S_LDX_MEM: /* X = mem[K] */
+			PPC_MR(r_X, r_M + (K & 0xf));
+			ctx->seen |= SEEN_XREG | SEEN_MEM | (1<<(K & 0xf));
+			break;

and :

Quoted 5 lines
arch/powerpc/net/bpf_jit_comp.c new file +5 -0
+		case BPF_S_LDX_W_LEN: /* X = skb->len; */
+			ctx->seen |= SEEN_XREG;
+			PPC_LWZ_OFFS(r_X, r_skb, offsetof(struct sk_buff, len));
+			break;
+
maintainer
#2 lore raw

From: Eric Dumazet <[email protected]>
Date: Mon, 18 Jul 2011 10:39:35 +0200

Quoted 3 lines

So in PPC SEEN_XREG only is to be set of X is read, not if written.

So you dont have to set SEEN_XREG bit in this part :

Matt, do you want to integrate changes based upon Eric's feedback here or do you want me to apply your patch as-is for now?

Thanks.

#3 lore raw
Quoted 10 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Mon, 18 Jul 2011 10:39:35 +0200

Quoted 3 lines

So in PPC SEEN_XREG only is to be set of X is read, not if written.

So you dont have to set SEEN_XREG bit in this part :

Matt, do you want to integrate changes based upon Eric's feedback here or do you want me to apply your patch as-is for now?

This was a really minor point, so Matt feel free to ask an immediate inclusion ;)

author
#4 lore raw
In reply to David Miller
Quoted 9 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Mon, 18 Jul 2011 10:39:35 +0200

Quoted 3 lines

So in PPC SEEN_XREG only is to be set of X is read, not if written.

So you dont have to set SEEN_XREG bit in this part :

Matt, do you want to integrate changes based upon Eric's feedback here or do you want me to apply your patch as-is for now?

Thanks, but no worries; I will send a v2 in a sec. Eric's comments are spot-on, and there are a couple of other areas that the brainfart should really be polished out of, too. :-)

Cheers,

Matt

author
#5 lore raw

PATCH v2 1/1 net: filter: BPF 'JIT' compiler for PPC64

In reply to Matt Evans

An implementation of a code generator for BPF programs to speed up packet filtering on PPC64, inspired by Eric Dumazet's x86-64 version.

Filter code is generated as an ABI-compliant function in module_alloc()'d mem with stackframe & prologue/epilogue generated if required (simple filters don't need anything more than an li/blr). The filter's local variables, M[], live in registers. Supports all BPF opcodes, although "complicated" loads from negative packet offsets (e.g. SKF_LL_OFF) are not yet supported.

There are a couple of further optimisations left for future work; many-pass assembly with branch-reach reduction and a register allocator to push M[] variables into volatile registers would improve the code quality further.

This currently supports big-endian 64-bit PowerPC only (but is fairly simple to port to PPC32 or LE!).

Enabled in the same way as x86-64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or, enabled with extra debug output:

echo 2 > /proc/sys/net/core/bpf_jit_enable

---

V2: Removed some cut/paste woe in setting SEEN_X even on writes.
Merci for le review, Eric!

 arch/powerpc/Kconfig                  |    1 +
 arch/powerpc/Makefile                 |    3 +-
 arch/powerpc/include/asm/ppc-opcode.h |   40 ++
 arch/powerpc/net/Makefile             |    4 +
 arch/powerpc/net/bpf_jit.S            |  138 +++++++
 arch/powerpc/net/bpf_jit.h            |  227 +++++++++++
 arch/powerpc/net/bpf_jit_comp.c       |  690 +++++++++++++++++++++++++++++++++
 7 files changed, 1102 insertions(+), 1 deletions(-)
arch/powerpc/Kconfig +1 -0
index 2729c66..39860fc 100644
@@ -134,6 +134,7 @@ config PPC
 	select GENERIC_IRQ_SHOW_LEVEL
 	select HAVE_RCU_TABLE_FREE if SMP
 	select HAVE_SYSCALL_TRACEPOINTS
+	select HAVE_BPF_JIT if PPC64
 
 config EARLY_PRINTK
 	bool
arch/powerpc/Makefile +2 -1
index b7212b6..b94740f 100644
@@ -154,7 +154,8 @@ core-y				+= arch/powerpc/kernel/ \
 				   arch/powerpc/lib/ \
 				   arch/powerpc/sysdev/ \
 				   arch/powerpc/platforms/ \
-				   arch/powerpc/math-emu/
+				   arch/powerpc/math-emu/ \
+				   arch/powerpc/net/
 core-$(CONFIG_XMON)		+= arch/powerpc/xmon/
 core-$(CONFIG_KVM) 		+= arch/powerpc/kvm/
 
arch/powerpc/include/asm/ppc-opcode.h +40 -0
index e472659..e980faa 100644
@@ -71,6 +71,42 @@
 #define PPC_INST_ERATSX			0x7c000126
 #define PPC_INST_ERATSX_DOT		0x7c000127
 
+/* Misc instructions for BPF compiler */
+#define PPC_INST_LD			0xe8000000
+#define PPC_INST_LHZ			0xa0000000
+#define PPC_INST_LWZ			0x80000000
+#define PPC_INST_STD			0xf8000000
+#define PPC_INST_STDU			0xf8000001
+#define PPC_INST_MFLR			0x7c0802a6
+#define PPC_INST_MTLR			0x7c0803a6
+#define PPC_INST_CMPWI			0x2c000000
+#define PPC_INST_CMPDI			0x2c200000
+#define PPC_INST_CMPLW			0x7c000040
+#define PPC_INST_CMPLWI			0x28000000
+#define PPC_INST_ADDI			0x38000000
+#define PPC_INST_ADDIS			0x3c000000
+#define PPC_INST_ADD			0x7c000214
+#define PPC_INST_SUB			0x7c000050
+#define PPC_INST_BLR			0x4e800020
+#define PPC_INST_BLRL			0x4e800021
+#define PPC_INST_MULLW			0x7c0001d6
+#define PPC_INST_MULHWU			0x7c000016
+#define PPC_INST_MULLI			0x1c000000
+#define PPC_INST_DIVWU			0x7c0003d6
+#define PPC_INST_RLWINM			0x54000000
+#define PPC_INST_RLDICR			0x78000004
+#define PPC_INST_SLW			0x7c000030
+#define PPC_INST_SRW			0x7c000430
+#define PPC_INST_AND			0x7c000038
+#define PPC_INST_ANDDOT			0x7c000039
+#define PPC_INST_OR			0x7c000378
+#define PPC_INST_ANDI			0x70000000
+#define PPC_INST_ORI			0x60000000
+#define PPC_INST_ORIS			0x64000000
+#define PPC_INST_NEG			0x7c0000d0
+#define PPC_INST_BRANCH			0x48000000
+#define PPC_INST_BRANCH_COND		0x40800000
+
 /* macros to insert fields into opcodes */
 #define __PPC_RA(a)	(((a) & 0x1f) << 16)
 #define __PPC_RB(b)	(((b) & 0x1f) << 11)
@@ -83,6 +119,10 @@
 #define __PPC_T_TLB(t)	(((t) & 0x3) << 21)
 #define __PPC_WC(w)	(((w) & 0x3) << 21)
 #define __PPC_WS(w)	(((w) & 0x1f) << 11)
+#define __PPC_SH(s)	__PPC_WS(s)
+#define __PPC_MB(s)	(((s) & 0x1f) << 6)
+#define __PPC_ME(s)	(((s) & 0x1f) << 1)
+#define __PPC_BI(s)	(((s) & 0x1f) << 16)
 
 /*
  * Only use the larx hint bit on 64bit CPUs. e500v1/v2 based CPUs will treat a
arch/powerpc/net/Makefile new file +4 -0
new file mode 100644
index 0000000..90568c3
@@ -0,0 +1,4 @@
+#
+# Arch-specific network modules
+#
+obj-$(CONFIG_BPF_JIT) += bpf_jit.o bpf_jit_comp.o
arch/powerpc/net/bpf_jit.S new file +138 -0

138 lines not on this page · raw message

arch/powerpc/net/bpf_jit.h new file +227 -0

227 lines not on this page · raw message

arch/powerpc/net/bpf_jit_comp.c new file +690 -0

690 lines not on this page · raw message

#6 lore raw
Quoted 40 lines · Matt Evans

An implementation of a code generator for BPF programs to speed up packet filtering on PPC64, inspired by Eric Dumazet's x86-64 version.

Filter code is generated as an ABI-compliant function in module_alloc()'d mem with stackframe & prologue/epilogue generated if required (simple filters don't need anything more than an li/blr). The filter's local variables, M[], live in registers. Supports all BPF opcodes, although "complicated" loads from negative packet offsets (e.g. SKF_LL_OFF) are not yet supported.

There are a couple of further optimisations left for future work; many-pass assembly with branch-reach reduction and a register allocator to push M[] variables into volatile registers would improve the code quality further.

This currently supports big-endian 64-bit PowerPC only (but is fairly simple to port to PPC32 or LE!).

Enabled in the same way as x86-64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or, enabled with extra debug output:

echo 2 > /proc/sys/net/core/bpf_jit_enable

---

V2: Removed some cut/paste woe in setting SEEN_X even on writes.
Merci for le review, Eric!

 arch/powerpc/Kconfig                  |    1 +
 arch/powerpc/Makefile                 |    3 +-
 arch/powerpc/include/asm/ppc-opcode.h |   40 ++
 arch/powerpc/net/Makefile             |    4 +
 arch/powerpc/net/bpf_jit.S            |  138 +++++++
 arch/powerpc/net/bpf_jit.h            |  227 +++++++++++
 arch/powerpc/net/bpf_jit_comp.c       |  690 +++++++++++++++++++++++++++++++++
 7 files changed, 1102 insertions(+), 1 deletions(-)

arch/powerpc/net/bpf_jit_comp.c line 382

Quoted 6 lines
arch/powerpc/net/bpf_jit_comp.c new file +6 -0
+		case BPF_S_ANC_CPU:
+#ifdef CONFIG_SMP
+			/*
+			 * PACA ptr is r13:
+			 * raw_smp_processor_id() = local_paca->paca_index
+			 */

This could break if one day linux supports more than 65536 cpus :)

Quoted 4 lines
arch/powerpc/net/bpf_jit_comp.c new file +4 -0
+			PPC_LHZ_OFFS(r_A, 13,
+				     offsetof(struct paca_struct, paca_index));
+#else
+			PPC_LI(r_A, 0);

arch/powerpc/net/bpf_jit_comp.c line 448

Quoted 10 lines
arch/powerpc/net/bpf_jit_comp.c new file +10 -0
+#endif
+			break;
+
+
+		case BPF_S_LDX_B_MSH:
+			/*
+			 * x86 version drops packet (RET 0) when K<0, whereas
+			 * interpreter does allow K<0 (__load_pointer, special
+			 * ancillary data).
+			 */

Hmm, thanks I'll take a look at this.

arch/powerpc/net/bpf_jit_comp.c line 535

Quoted 13 lines
arch/powerpc/net/bpf_jit_comp.c new file +12 -0
+			func = sk_load_byte_msh;
+			goto common_load;
+			break;
+
+			/*** Jump and branches ***/
 
+		default:
+			/* The filter contains something cruel & unusual.
+			 * We don't handle it, but also there shouldn't be
+			 * anything missing from our list.
+			 */
+			pr_err("BPF filter opcode %04x (@%d) unsupported\n",
+			       filter[i].code, i);

You should at least ratelimit this message ?

On x86_64 I chose to silently fall back to interpretor for a "complex filter" or "unsupported opcode".

Quoted 10 lines
arch/powerpc/net/bpf_jit_comp.c new file +10 -0
+			return -ENOTSUPP;
+		}
+
+	}
+	/* Set end-of-body-code address for exit. */
+	addrs[i] = ctx->idx * 4;
+
+	return 0;
+}
+
#10 lore raw
In reply to Matt Evans
Quoted 1 line · Matt Evans

An implementation of a code generator for BPF programs to speed up =

packet

Quoted 3 lines

filtering on PPC64, inspired by Eric Dumazet's x86-64 version. =20
Filter code is generated as an ABI-compliant function in =

module_alloc()'d mem

Quoted 1 line

with stackframe & prologue/epilogue generated if required (simple =

filters don't

Quoted 1 line

need anything more than an li/blr). The filter's local variables, =

M[], live in

Quoted 1 line

registers. Supports all BPF opcodes, although "complicated" loads =

from negative

Quoted 3 lines

packet offsets (e.g. SKF_LL_OFF) are not yet supported. =20
There are a couple of further optimisations left for future work; =

many-pass

Quoted 1 line

assembly with branch-reach reduction and a register allocator to push =

M[]

Quoted 1 line

variables into volatile registers would improve the code quality =

further.

Quoted 2 lines

=20
This currently supports big-endian 64-bit PowerPC only (but is fairly =

simple

Quoted 16 lines

to port to PPC32 or LE!).
=20
Enabled in the same way as x86-64:
=20
echo 1 > /proc/sys/net/core/bpf_jit_enable
=20
Or, enabled with extra debug output:
=20
echo 2 > /proc/sys/net/core/bpf_jit_enable
=20

---
=20
V2: Removed some cut/paste woe in setting SEEN_X even on writes.
Merci for le review, Eric!
=20

Quoted 5 lines
arch/powerpc/Kconfig                  |    1 +
arch/powerpc/Makefile                 |    3 +-
arch/powerpc/include/asm/ppc-opcode.h |   40 ++
arch/powerpc/net/Makefile             |    4 +
arch/powerpc/net/bpf_jit.S            |  138 +++++++

can we rename to bpf_jit_64.S, since this doesn't work on PPC32.

Quoted 2 lines
arch/powerpc/net/bpf_jit.h            |  227 +++++++++++
arch/powerpc/net/bpf_jit_comp.c       |  690 =

+++++++++++++++++++++++++++++++++

same here, or split between bpf_jit_comp.c (shared between ppc32 & = ppc64) and bpf_jit_comp_64.c

- k

author
#11 lore raw
Quoted 39 lines · Kumar Gala
Quoted 27 lines · Matt Evans

An implementation of a code generator for BPF programs to speed up packet filtering on PPC64, inspired by Eric Dumazet's x86-64 version.

Filter code is generated as an ABI-compliant function in module_alloc()'d mem with stackframe & prologue/epilogue generated if required (simple filters don't need anything more than an li/blr). The filter's local variables, M[], live in registers. Supports all BPF opcodes, although "complicated" loads from negative packet offsets (e.g. SKF_LL_OFF) are not yet supported.

There are a couple of further optimisations left for future work; many-pass assembly with branch-reach reduction and a register allocator to push M[] variables into volatile registers would improve the code quality further.

This currently supports big-endian 64-bit PowerPC only (but is fairly simple to port to PPC32 or LE!).

Enabled in the same way as x86-64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or, enabled with extra debug output:

echo 2 > /proc/sys/net/core/bpf_jit_enable

---

Quoted 8 lines

V2: Removed some cut/paste woe in setting SEEN_X even on writes.
Merci for le review, Eric!

arch/powerpc/Kconfig                  |    1 +
arch/powerpc/Makefile                 |    3 +-
arch/powerpc/include/asm/ppc-opcode.h |   40 ++
arch/powerpc/net/Makefile             |    4 +
arch/powerpc/net/bpf_jit.S            |  138 +++++++
Quoted 7 lines

can we rename to bpf_jit_64.S, since this doesn't work on PPC32.

Quoted 2 lines
arch/powerpc/net/bpf_jit.h            |  227 +++++++++++
arch/powerpc/net/bpf_jit_comp.c       |  690 +++++++++++++++++++++++++++++++++

same here, or split between bpf_jit_comp.c (shared between ppc32 & ppc64) and bpf_jit_comp_64.c

A reasonable suggestion -- bpf_jit_64.S certainly. I think it may not be worth splitting bpf_jit_comp.c until we support both tho? (I'm thinking bpf_jit_comp_{32,64}.c would just house the stackframe generation code which is the main difference, plus compile-time switched macros for the odd LD vs LWZ.)

Sorry it's not 32bit-friendly just yet (I knew you'd ask, hehe), I've postponed that for when I get a mo :-)

Cheers,

Matt

#12 lore raw
Quoted 5 lines · Matt Evans
Quoted 4 lines · Kumar Gala

=20
On Jul 18, 2011, at 9:13 PM, Matt Evans wrote:
=20

Quoted 1 line

An implementation of a code generator for BPF programs to speed up =

packet

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

filtering on PPC64, inspired by Eric Dumazet's x86-64 version. =20
Filter code is generated as an ABI-compliant function in =

module_alloc()'d mem

Quoted 1 line
Quoted 1 line
Quoted 1 line

with stackframe & prologue/epilogue generated if required (simple =

filters don't

Quoted 1 line
Quoted 1 line
Quoted 1 line

need anything more than an li/blr). The filter's local variables, =

M[], live in

Quoted 1 line
Quoted 1 line
Quoted 1 line

registers. Supports all BPF opcodes, although "complicated" loads =

from negative

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

packet offsets (e.g. SKF_LL_OFF) are not yet supported. =20
There are a couple of further optimisations left for future work; =

many-pass

Quoted 1 line
Quoted 1 line
Quoted 1 line

assembly with branch-reach reduction and a register allocator to =

push M[]

Quoted 1 line
Quoted 1 line
Quoted 1 line

variables into volatile registers would improve the code quality =

further.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

=20
This currently supports big-endian 64-bit PowerPC only (but is =

fairly simple

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

to port to PPC32 or LE!).
=20
Enabled in the same way as x86-64:
=20
echo 1 > /proc/sys/net/core/bpf_jit_enable
=20
Or, enabled with extra debug output:
=20
echo 2 > /proc/sys/net/core/bpf_jit_enable
=20

---
=20
V2: Removed some cut/paste woe in setting SEEN_X even on writes.
Merci for le review, Eric!
=20

arch/powerpc/Kconfig                  |    1 +
arch/powerpc/Makefile                 |    3 +-
arch/powerpc/include/asm/ppc-opcode.h |   40 ++
arch/powerpc/net/Makefile             |    4 +
arch/powerpc/net/bpf_jit.S            |  138 +++++++
Quoted 5 lines
Quoted 5 lines

=20
can we rename to bpf_jit_64.S, since this doesn't work on PPC32. =20

Quoted 2 lines
arch/powerpc/net/bpf_jit.h            |  227 +++++++++++
arch/powerpc/net/bpf_jit_comp.c       |  690 =

+++++++++++++++++++++++++++++++++

Quoted 2 lines
Quoted 2 lines

=20
same here, or split between bpf_jit_comp.c (shared between ppc32 & =

ppc64) and

Quoted 3 lines
Quoted 1 line

bpf_jit_comp_64.c

=20
A reasonable suggestion -- bpf_jit_64.S certainly. I think it may not =

be worth

Quoted 2 lines

splitting bpf_jit_comp.c until we support both tho? (I'm thinking bpf_jit_comp_{32,64}.c would just house the stackframe generation code =

which is

Quoted 1 line

the main difference, plus compile-time switched macros for the odd LD =

vs LWZ.)

If its most 64-bit specific than just go with bpf_jit_comp_64.c for now. =
We can refactor later.

Quoted 2 lines

=20
Sorry it's not 32bit-friendly just yet (I knew you'd ask, hehe), I've =

postponed

Quoted 1 line

that for when I get a mo :-)

:)

author
#13 lore raw
Quoted 26 lines · Kumar Gala
Quoted 23 lines · Matt Evans
Quoted 21 lines · Kumar Gala
Quoted 10 lines · Matt Evans

[snip]

V2: Removed some cut/paste woe in setting SEEN_X even on writes.
Merci for le review, Eric!

arch/powerpc/Kconfig                  |    1 +
arch/powerpc/Makefile                 |    3 +-
arch/powerpc/include/asm/ppc-opcode.h |   40 ++
arch/powerpc/net/Makefile             |    4 +
arch/powerpc/net/bpf_jit.S            |  138 +++++++

can we rename to bpf_jit_64.S, since this doesn't work on PPC32.

Quoted 2 lines
arch/powerpc/net/bpf_jit.h            |  227 +++++++++++
arch/powerpc/net/bpf_jit_comp.c       |  690 +++++++++++++++++++++++++++++++++

same here, or split between bpf_jit_comp.c (shared between ppc32 & ppc64) and bpf_jit_comp_64.c

Quoted 6 lines
Quoted 4 lines

A reasonable suggestion -- bpf_jit_64.S certainly. I think it may not be worth splitting bpf_jit_comp.c until we support both tho? (I'm thinking bpf_jit_comp_{32,64}.c would just house the stackframe generation code which is the main difference, plus compile-time switched macros for the odd LD vs LWZ.)

If its most 64-bit specific than just go with bpf_jit_comp_64.c for now. We can refactor later.

Nah, other way round -- it's almost all agnostic but with a couple of functions that I was recommending moving out to a _64.c and _32.c later, leaving the bulk still in bpf_jit_comp.c.

Matt

#7 lore raw
In reply to Eric Dumazet
Quoted 9 lines · Eric Dumazet

arch/powerpc/net/bpf_jit_comp.c line 382

Quoted 6 lines
arch/powerpc/net/bpf_jit_comp.c new file +6 -0
+		case BPF_S_ANC_CPU:
+#ifdef CONFIG_SMP
+			/*
+			 * PACA ptr is r13:
+			 * raw_smp_processor_id() = local_paca->paca_index
+			 */

This could break if one day linux supports more than 65536 cpus :)

Quoted 6 lines

arch/powerpc/net/bpf_jit_comp.c line 388

Quoted 6 lines
arch/powerpc/net/bpf_jit_comp.c new file +6 -0
+			PPC_LHZ_OFFS(r_A, 13,
+				     offsetof(struct paca_struct, paca_index));
+#else
+			PPC_LI(r_A, 0);
+#endif
+			break;

As would our implementation of raw_smp_processor_id() and our spinlocks :-) I don't think we need to fix that -now- but you are welcome to add something like a BUILD_BUG_ON(sizeof(local_paca->paca_index) != 2); as a reminder :-)

Cheers,
Ben.

#8 lore raw
Quoted 11 lines · Benjamin Herrenschmidt
Quoted 9 lines · Eric Dumazet

arch/powerpc/net/bpf_jit_comp.c line 382

Quoted 6 lines
arch/powerpc/net/bpf_jit_comp.c new file +6 -0
+		case BPF_S_ANC_CPU:
+#ifdef CONFIG_SMP
+			/*
+			 * PACA ptr is r13:
+			 * raw_smp_processor_id() = local_paca->paca_index
+			 */

This could break if one day linux supports more than 65536 cpus :)

Quoted 11 lines
Quoted 6 lines

arch/powerpc/net/bpf_jit_comp.c line 388

Quoted 6 lines
arch/powerpc/net/bpf_jit_comp.c new file +6 -0
+			PPC_LHZ_OFFS(r_A, 13,
+				     offsetof(struct paca_struct, paca_index));
+#else
+			PPC_LI(r_A, 0);
+#endif
+			break;

As would our implementation of raw_smp_processor_id() and our spinlocks :-) I don't think we need to fix that -now- but you are welcome to add something like a BUILD_BUG_ON(sizeof(local_paca->paca_index) != 2); as a reminder :-)

Please Matt add to your next version this check. I dont think I have to submit a one line patch later...

On x86_64, cpu_number field is already 32bit, we have some time before it becomes 64bit ;)

We probably should add some extra check to make sure segment doesnt change (%gs on x86_64, r13 on ppc64) on a future linux version.

#9 lore raw
Quoted 9 lines · Eric Dumazet

Please Matt add to your next version this check. I dont think I have to
submit a one line patch later...

On x86_64, cpu_number field is already 32bit, we have some time before it becomes 64bit ;)

We probably should add some extra check to make sure segment doesnt change (%gs on x86_64, r13 on ppc64) on a future linux version.

r13 isn't really a "segment" per-se, it can change if you aren't preempt disabled, it's the PACA pointer, which is basically a low level per-cpu data structure, so it will change if you migrate around (but if you are in softirq context like most of the network stack, then it won't change).

As for the CPU number being 16-bit, this is really due to how our spinlocks work, using that as a lock token within the same 32-bit word as the lock flag etc... We can easily fix that later if needed but I don't see any emergency for us.

Cheers,
Ben.

author
#14 lore raw

PATCH v3 1/1 net: filter: BPF 'JIT' compiler for PPC64

In reply to Matt Evans

An implementation of a code generator for BPF programs to speed up packet filtering on PPC64, inspired by Eric Dumazet's x86-64 version.

Filter code is generated as an ABI-compliant function in module_alloc()'d mem with stackframe & prologue/epilogue generated if required (simple filters don't need anything more than an li/blr). The filter's local variables, M[], live in registers. Supports all BPF opcodes, although "complicated" loads from negative packet offsets (e.g. SKF_LL_OFF) are not yet supported.

There are a couple of further optimisations left for future work; many-pass assembly with branch-reach reduction and a register allocator to push M[] variables into volatile registers would improve the code quality further.

This currently supports big-endian 64-bit PowerPC only (but is fairly simple to port to PPC32 or LE!).

Enabled in the same way as x86-64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or, enabled with extra debug output:

echo 2 > /proc/sys/net/core/bpf_jit_enable

---

V3: Added BUILD_BUG_ON to assert PACA CPU ID is 16bits, made a comment (in
LD_MSH) a bit clearer, ratelimited "Unknown opcode" error and moved bpf_jit.S to bpf_jit_64.S (it doesn't make sense to rename bpf_jit_comp.c as small portions will eventually get split out into _32/_64.c files when we do 32bit support).

 arch/powerpc/Kconfig                  |    1 +
 arch/powerpc/Makefile                 |    3 +-
 arch/powerpc/include/asm/ppc-opcode.h |   40 ++
 arch/powerpc/net/Makefile             |    4 +
 arch/powerpc/net/bpf_jit.h            |  227 +++++++++++
 arch/powerpc/net/bpf_jit_64.S         |  138 +++++++
 arch/powerpc/net/bpf_jit_comp.c       |  694 +++++++++++++++++++++++++++++++++
 7 files changed, 1106 insertions(+), 1 deletions(-)
arch/powerpc/Kconfig +1 -0
index 2729c66..39860fc 100644
@@ -134,6 +134,7 @@ config PPC
 	select GENERIC_IRQ_SHOW_LEVEL
 	select HAVE_RCU_TABLE_FREE if SMP
 	select HAVE_SYSCALL_TRACEPOINTS
+	select HAVE_BPF_JIT if PPC64
 
 config EARLY_PRINTK
 	bool
arch/powerpc/Makefile +2 -1
index b7212b6..b94740f 100644
@@ -154,7 +154,8 @@ core-y				+= arch/powerpc/kernel/ \
 				   arch/powerpc/lib/ \
 				   arch/powerpc/sysdev/ \
 				   arch/powerpc/platforms/ \
-				   arch/powerpc/math-emu/
+				   arch/powerpc/math-emu/ \
+				   arch/powerpc/net/
 core-$(CONFIG_XMON)		+= arch/powerpc/xmon/
 core-$(CONFIG_KVM) 		+= arch/powerpc/kvm/
 
arch/powerpc/include/asm/ppc-opcode.h +40 -0
index e472659..e980faa 100644
@@ -71,6 +71,42 @@
 #define PPC_INST_ERATSX			0x7c000126
 #define PPC_INST_ERATSX_DOT		0x7c000127
 
+/* Misc instructions for BPF compiler */
+#define PPC_INST_LD			0xe8000000
+#define PPC_INST_LHZ			0xa0000000
+#define PPC_INST_LWZ			0x80000000
+#define PPC_INST_STD			0xf8000000
+#define PPC_INST_STDU			0xf8000001
+#define PPC_INST_MFLR			0x7c0802a6
+#define PPC_INST_MTLR			0x7c0803a6
+#define PPC_INST_CMPWI			0x2c000000
+#define PPC_INST_CMPDI			0x2c200000
+#define PPC_INST_CMPLW			0x7c000040
+#define PPC_INST_CMPLWI			0x28000000
+#define PPC_INST_ADDI			0x38000000
+#define PPC_INST_ADDIS			0x3c000000
+#define PPC_INST_ADD			0x7c000214
+#define PPC_INST_SUB			0x7c000050
+#define PPC_INST_BLR			0x4e800020
+#define PPC_INST_BLRL			0x4e800021
+#define PPC_INST_MULLW			0x7c0001d6
+#define PPC_INST_MULHWU			0x7c000016
+#define PPC_INST_MULLI			0x1c000000
+#define PPC_INST_DIVWU			0x7c0003d6
+#define PPC_INST_RLWINM			0x54000000
+#define PPC_INST_RLDICR			0x78000004
+#define PPC_INST_SLW			0x7c000030
+#define PPC_INST_SRW			0x7c000430
+#define PPC_INST_AND			0x7c000038
+#define PPC_INST_ANDDOT			0x7c000039
+#define PPC_INST_OR			0x7c000378
+#define PPC_INST_ANDI			0x70000000
+#define PPC_INST_ORI			0x60000000
+#define PPC_INST_ORIS			0x64000000
+#define PPC_INST_NEG			0x7c0000d0
+#define PPC_INST_BRANCH			0x48000000
+#define PPC_INST_BRANCH_COND		0x40800000
+
 /* macros to insert fields into opcodes */
 #define __PPC_RA(a)	(((a) & 0x1f) << 16)
 #define __PPC_RB(b)	(((b) & 0x1f) << 11)
@@ -83,6 +119,10 @@
 #define __PPC_T_TLB(t)	(((t) & 0x3) << 21)
 #define __PPC_WC(w)	(((w) & 0x3) << 21)
 #define __PPC_WS(w)	(((w) & 0x1f) << 11)
+#define __PPC_SH(s)	__PPC_WS(s)
+#define __PPC_MB(s)	(((s) & 0x1f) << 6)
+#define __PPC_ME(s)	(((s) & 0x1f) << 1)
+#define __PPC_BI(s)	(((s) & 0x1f) << 16)
 
 /*
  * Only use the larx hint bit on 64bit CPUs. e500v1/v2 based CPUs will treat a
arch/powerpc/net/Makefile new file +4 -0
new file mode 100644
index 0000000..266b395
@@ -0,0 +1,4 @@
+#
+# Arch-specific network modules
+#
+obj-$(CONFIG_BPF_JIT) += bpf_jit_64.o bpf_jit_comp.o
arch/powerpc/net/bpf_jit.h new file +227 -0

227 lines not on this page · raw message

arch/powerpc/net/bpf_jit_64.S new file +138 -0

138 lines not on this page · raw message

arch/powerpc/net/bpf_jit_comp.c new file +694 -0

694 lines not on this page · raw message

Acked-by on v3 1/1
#15 lore raw
Quoted 33 lines · Matt Evans

An implementation of a code generator for BPF programs to speed up packet filtering on PPC64, inspired by Eric Dumazet's x86-64 version.

Filter code is generated as an ABI-compliant function in module_alloc()'d mem with stackframe & prologue/epilogue generated if required (simple filters don't need anything more than an li/blr). The filter's local variables, M[], live in registers. Supports all BPF opcodes, although "complicated" loads from negative packet offsets (e.g. SKF_LL_OFF) are not yet supported.

There are a couple of further optimisations left for future work; many-pass assembly with branch-reach reduction and a register allocator to push M[] variables into volatile registers would improve the code quality further.

This currently supports big-endian 64-bit PowerPC only (but is fairly simple to port to PPC32 or LE!).

Enabled in the same way as x86-64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or, enabled with extra debug output:

echo 2 > /proc/sys/net/core/bpf_jit_enable

---

V3: Added BUILD_BUG_ON to assert PACA CPU ID is 16bits, made a comment (in
LD_MSH) a bit clearer, ratelimited "Unknown opcode" error and moved bpf_jit.S to bpf_jit_64.S (it doesn't make sense to rename bpf_jit_comp.c as small portions will eventually get split out into _32/_64.c files when we do 32bit support).

Quoted 8 lines
 arch/powerpc/Kconfig                  |    1 +
 arch/powerpc/Makefile                 |    3 +-
 arch/powerpc/include/asm/ppc-opcode.h |   40 ++
 arch/powerpc/net/Makefile             |    4 +
 arch/powerpc/net/bpf_jit.h            |  227 +++++++++++
 arch/powerpc/net/bpf_jit_64.S         |  138 +++++++
 arch/powerpc/net/bpf_jit_comp.c       |  694 +++++++++++++++++++++++++++++++++
 7 files changed, 1106 insertions(+), 1 deletions(-)

Nice work Matt ;)

Thanks

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

maintainer Applied
#16 lore raw
In reply to Matt Evans

From: Matt Evans <[email protected]>
Date: Thu, 21 Jul 2011 11:51:00 +1000

Quoted 18 lines

An implementation of a code generator for BPF programs to speed up packet filtering on PPC64, inspired by Eric Dumazet's x86-64 version.

Filter code is generated as an ABI-compliant function in module_alloc()'d mem with stackframe & prologue/epilogue generated if required (simple filters don't need anything more than an li/blr). The filter's local variables, M[], live in registers. Supports all BPF opcodes, although "complicated" loads from negative packet offsets (e.g. SKF_LL_OFF) are not yet supported.

There are a couple of further optimisations left for future work; many-pass assembly with branch-reach reduction and a register allocator to push M[] variables into volatile registers would improve the code quality further.

This currently supports big-endian 64-bit PowerPC only (but is fairly simple to port to PPC32 or LE!).

Enabled in the same way as x86-64:

Quoted 7 lines

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or, enabled with extra debug output:

echo 2 > /proc/sys/net/core/bpf_jit_enable

Applied.