Skip to contentSkip to conversation

s390/bpf: implement bpf_tail_call JIT support

PATCH In review Alexei Starovoitov 2 participants 6 messages May 30, 2015 – Jun 2, 2015

Subsystems (5)
Networking Network drivers BPF s390 +1 more subsystems
lore
Share & tools
lore
#0 lore raw

cover s390/bpf: implement bpf_tail_call JIT support

This set is for net-next tree.

Patch 3 adds bpf_tail_call() support for s390x JIT. It has a dependency on patches 1 and 2 that will also be submitted to stable via Martin Schwidefsky.

Michael Holzheu (3):
s390/bpf: fix stack allocation
s390/bpf: fix bpf frame pointer setup
s390/bpf: implement bpf_tail_call() helper

 arch/s390/net/bpf_jit.h      |   12 ++++-
 arch/s390/net/bpf_jit_comp.c |  117 +++++++++++++++++++++++++++++++++++++++---
 2 files changed, 121 insertions(+), 8 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/3 s390/bpf: fix stack allocation

From: Michael Holzheu <[email protected]>

On s390x we have to provide 160 bytes stack space before we can call the next function. From the 160 bytes that we got from the previous function we only use 11 * 8 bytes and have 160 - 11 * 8 bytes left. Currently for BPF we allocate additional 160 - 11 * 8 bytes for the next function. This is wrong because then the next function only gets:

(160 - 11 * 8) + (160 - 11 * 8) = 2 * 72 = 144 bytes

Fix this and allocate enough memory for the next function.

---
 arch/s390/net/bpf_jit.h |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/s390/net/bpf_jit.h +3 -1
index ba8593a515ba..de156ba3bd71 100644
@@ -48,7 +48,9 @@ extern u8 sk_load_word[], sk_load_half[], sk_load_byte[];
  * We get 160 bytes stack space from calling function, but only use
  * 11 * 8 byte (old backchain + r15 - r6) for storing registers.
  */
-#define STK_OFF (MAX_BPF_STACK + 8 + 4 + 4 + (160 - 11 * 8))
+#define STK_SPACE	(MAX_BPF_STACK + 8 + 4 + 4 + 160)
+#define STK_160_UNUSED	(160 - 11 * 8)
+#define STK_OFF		(STK_SPACE - STK_160_UNUSED)
 #define STK_OFF_TMP	160	/* Offset of tmp buffer on stack */
 #define STK_OFF_HLEN	168	/* Offset of SKB header length on stack */
 
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 2/3 s390/bpf: fix bpf frame pointer setup

In reply to Alexei Starovoitov

From: Michael Holzheu <[email protected]>

Currently the bpf frame pointer is set to the old r15. This is wrong because of packed stack. Fix this and adjust the frame pointer to respect packed stack. This now generates a prolog like the following:

 3ff8001c3fa: eb67f0480024   stmg    %r6,%r7,72(%r15)
 3ff8001c400: ebcff0780024   stmg    %r12,%r15,120(%r15)
 3ff8001c406: b904001f       lgr     %r1,%r15      <- load backchain
 3ff8001c40a: 41d0f048       la      %r13,72(%r15) <- load adjusted bfp
 3ff8001c40e: a7fbfd98       aghi    %r15,-616
 3ff8001c412: e310f0980024   stg     %r1,152(%r15) <- save backchain
---
 arch/s390/net/bpf_jit_comp.c |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
arch/s390/net/bpf_jit_comp.c +7 -4
index 20c146d1251a..55423d8be580 100644
@@ -384,13 +384,16 @@ static void bpf_jit_prologue(struct bpf_jit *jit)
 	}
 	/* Setup stack and backchain */
 	if (jit->seen & SEEN_STACK) {
-		/* lgr %bfp,%r15 (BPF frame pointer) */
-		EMIT4(0xb9040000, BPF_REG_FP, REG_15);
+		if (jit->seen & SEEN_FUNC)
+			/* lgr %w1,%r15 (backchain) */
+			EMIT4(0xb9040000, REG_W1, REG_15);
+		/* la %bfp,STK_160_UNUSED(%r15) (BPF frame pointer) */
+		EMIT4_DISP(0x41000000, BPF_REG_FP, REG_15, STK_160_UNUSED);
 		/* aghi %r15,-STK_OFF */
 		EMIT4_IMM(0xa70b0000, REG_15, -STK_OFF);
 		if (jit->seen & SEEN_FUNC)
-			/* stg %bfp,152(%r15) (backchain) */
-			EMIT6_DISP_LH(0xe3000000, 0x0024, BPF_REG_FP, REG_0,
+			/* stg %w1,152(%r15) (backchain) */
+			EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1, REG_0,
 				      REG_15, 152);
 	}
 	/*
Signature · 1 line
1.7.9.5
#3 lore raw

PATCH 3/3 s390/bpf: implement bpf_tail_call() helper

In reply to Alexei Starovoitov

From: Michael Holzheu <[email protected]>

bpf_tail_call() arguments:

- ctx......: Context pointer
- jmp_table: One of BPF_MAP_TYPE_PROG_ARRAY maps used as the jump table
- index....: Index in the jump table

In this implementation s390x JIT does stack unwinding and jumps into the callee program prologue. Caller and callee use the same stack.

With this patch a tail call generates the following code on s390x:

if (index >= array->map.max_entries)
goto out

 000003ff8001c7e4: e31030100016   llgf    %r1,16(%r3)
 000003ff8001c7ea: ec41001fa065   clgrj   %r4,%r1,10,3ff8001c828

if (tail_call_cnt++ > MAX_TAIL_CALL_CNT)

         goto out;
 000003ff8001c7f0: a7080001       lhi     %r0,1
 000003ff8001c7f4: eb10f25000fa   laal    %r1,%r0,592(%r15)
 000003ff8001c7fa: ec120017207f   clij    %r1,32,2,3ff8001c828

prog = array->prog[index];
if (prog == NULL)

         goto out;
 000003ff8001c800: eb140003000d   sllg    %r1,%r4,3
 000003ff8001c806: e31310800004   lg      %r1,128(%r3,%r1)
 000003ff8001c80c: ec18000e007d   clgij   %r1,0,8,3ff8001c828

Restore registers before calling function

 000003ff8001c812: eb68f2980004   lmg     %r6,%r8,664(%r15)
 000003ff8001c818: ebbff2c00004   lmg     %r11,%r15,704(%r15)

goto *(prog->bpf_func + tail_call_start);

 000003ff8001c81e: e31100200004   lg      %r1,32(%r1,%r0)
 000003ff8001c824: 47f01006       bc      15,6(%r1)
---
 arch/s390/net/bpf_jit.h      |   10 +++-
 arch/s390/net/bpf_jit_comp.c |  106 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 112 insertions(+), 4 deletions(-)
arch/s390/net/bpf_jit.h +8 -2
index de156ba3bd71..f6498eec9ee1 100644
@@ -28,6 +28,9 @@ extern u8 sk_load_word[], sk_load_half[], sk_load_byte[];
  *	      | old backchain |     |
  *	      +---------------+     |
  *	      |   r15 - r6    |     |
+ *	      +---------------+     |
+ *	      | 4 byte align  |     |
+ *	      | tail_call_cnt |     |
  * BFP	   -> +===============+     |
  *	      |		      |     |
  *	      |   BPF stack   |     |
@@ -46,14 +49,17 @@ extern u8 sk_load_word[], sk_load_half[], sk_load_byte[];
  * R15	   -> +---------------+     + low
  *
  * We get 160 bytes stack space from calling function, but only use
- * 11 * 8 byte (old backchain + r15 - r6) for storing registers.
+ * 12 * 8 byte for old backchain, r15..r6, and tail_call_cnt.
  */
 #define STK_SPACE	(MAX_BPF_STACK + 8 + 4 + 4 + 160)
-#define STK_160_UNUSED	(160 - 11 * 8)
+#define STK_160_UNUSED	(160 - 12 * 8)
 #define STK_OFF		(STK_SPACE - STK_160_UNUSED)
 #define STK_OFF_TMP	160	/* Offset of tmp buffer on stack */
 #define STK_OFF_HLEN	168	/* Offset of SKB header length on stack */
 
+#define STK_OFF_R6	(160 - 11 * 8)	/* Offset of r6 on stack */
+#define STK_OFF_TCCNT	(160 - 12 * 8)	/* Offset of tail_call_cnt on stack */
+
 /* Offset to skip condition code check */
 #define OFF_OK		4
 
arch/s390/net/bpf_jit_comp.c +104 -2

160 lines not on this page · raw message

Signature · 1 line
1.7.9.5
maintainer
#4 lore raw
In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>
Date: Sat, 30 May 2015 10:42:08 -0700

Quoted 5 lines

This set is for net-next tree.

Patch 3 adds bpf_tail_call() support for s390x JIT. It has a dependency on patches 1 and 2 that will also be submitted to stable via Martin Schwidefsky.

In this kind of scenerio, patches #1 and #2 should go via my tree (somehow), and then when I next merge 'net' into 'net-next' you can submit patch #3.

#5 lore raw
Quoted 5 lines · David Miller

From: Alexei Starovoitov <[email protected]>
Date: Sat, 30 May 2015 10:42:08 -0700

Quoted 2 lines

This set is for net-next tree.

Quoted 7 lines
Quoted 3 lines

Patch 3 adds bpf_tail_call() support for s390x JIT. It has a dependency on patches 1 and 2 that will also be submitted to stable via Martin Schwidefsky.

In this kind of scenerio, patches #1 and #2 should go via my tree (somehow), and then when I next merge 'net' into 'net-next' you can submit patch #3.

all makes sense. Just resubmitted #1 and #2 against 'net' with proper 'Fixes' tag.