Skip to contentSkip to conversation

powerpc/bpf: fix tail call implementation

PATCH Applied Eric Dumazet 3 participants 3 messages Nov 1, 2019 – Nov 2, 2019

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

PATCH 1/1 powerpc/bpf: fix tail call implementation

We have seen many crashes on powerpc hosts while loading bpf programs.

The problem here is that bpf_int_jit_compile() does a first pass to compute the program length.

Then it allocates memory to store the generated program and calls bpf_jit_build_body() a second time (and a third time later)

What I have observed is that the second bpf_jit_build_body() could end up using few more words than expected.

If bpf_jit_binary_alloc() put the space for the program at the end of the allocated page, we then write on a non mapped memory.

It appears that bpf_jit_emit_tail_call() calls bpf_jit_emit_common_epilogue() while ctx->seen might not be stable.

Only after the second pass we can be sure ctx->seen wont be changed.

Trying to avoid a second pass seems quite complex and probably not worth it.

Fixes ce0761419faef ("powerpc/bpf: Implement support for tail calls")
Cc "Naveen N. Rao" <[email protected]>
Cc Sandipan Das <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
---
 arch/powerpc/net/bpf_jit_comp64.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/powerpc/net/bpf_jit_comp64.c +13 -0
index 02a59946a78af46416e9d949047ad3fefe3fc159..be3517ef0574d0911f6d63b530b33954c8ca343d 100644
@@ -1141,6 +1141,19 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
 		goto out_addrs;
 	}
 
+	/*
+	 * If we have seen a tail call, we need a second pass.
+	 * This is because bpf_jit_emit_common_epilogue() is called
+	 * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen.
+	 */
+	if (cgctx.seen & SEEN_TAILCALL) {
+		cgctx.idx = 0;
+		if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) {
+			fp = org_fp;
+			goto out_addrs;
+		}
+	}
+
 	/*
 	 * Pretend to build prologue, given the features we've seen.  This will
 	 * update ctgtx.idx as it pretends to output instructions, then we can
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
maintainer
#1 lore raw

From: Eric Dumazet <[email protected]>
Date: Thu, 31 Oct 2019 20:34:44 -0700

Quoted 20 lines

We have seen many crashes on powerpc hosts while loading bpf programs.

The problem here is that bpf_int_jit_compile() does a first pass to compute the program length.

Then it allocates memory to store the generated program and calls bpf_jit_build_body() a second time (and a third time later)

What I have observed is that the second bpf_jit_build_body() could end up using few more words than expected.

If bpf_jit_binary_alloc() put the space for the program at the end of the allocated page, we then write on a non mapped memory.

It appears that bpf_jit_emit_tail_call() calls bpf_jit_emit_common_epilogue() while ctx->seen might not be stable.

Quoted 7 lines

Only after the second pass we can be sure ctx->seen wont be changed.

Trying to avoid a second pass seems quite complex and probably not worth it.

Fixes ce0761419faef ("powerpc/bpf: Implement support for tail calls")

I am anticipating this will go via the bpf tree.

maintainer Applied
#2 lore raw
In reply to Eric Dumazet
Quoted 25 lines · Eric Dumazet

We have seen many crashes on powerpc hosts while loading bpf programs.

The problem here is that bpf_int_jit_compile() does a first pass to compute the program length.

Then it allocates memory to store the generated program and calls bpf_jit_build_body() a second time (and a third time later)

What I have observed is that the second bpf_jit_build_body() could end up using few more words than expected.

If bpf_jit_binary_alloc() put the space for the program at the end of the allocated page, we then write on a non mapped memory.

It appears that bpf_jit_emit_tail_call() calls bpf_jit_emit_common_epilogue() while ctx->seen might not be stable.

Only after the second pass we can be sure ctx->seen wont be changed.

Trying to avoid a second pass seems quite complex and probably not worth it.

Quoted 14 lines
Fixes ce0761419faef ("powerpc/bpf: Implement support for tail calls")
Cc "Naveen N. Rao" <[email protected]>
Cc Sandipan Das <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>

Applied, thanks!