bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] powerpc/bpf: fix tail call implementation
@ 2019-11-01  3:34 Eric Dumazet
  2019-11-01  4:39 ` David Miller
  2019-11-02  0:05 ` Daniel Borkmann
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Dumazet @ 2019-11-01  3:34 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann
  Cc: David S . Miller, netdev, Eric Dumazet, Eric Dumazet, bpf,
	Naveen N. Rao, Sandipan Das, Benjamin Herrenschmidt,
	Paul Mackerras, Michael Ellerman, Martin KaFai Lau, Song Liu,
	Yonghong Song

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")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: "Naveen N. Rao" <naveen.n.rao@linux.ibm.com>
Cc: Sandipan Das <sandipan@linux.ibm.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Song Liu <songliubraving@fb.com>
Cc: Yonghong Song <yhs@fb.com>
Cc: netdev@vger.kernel.org
Cc: bpf@vger.kernel.org
---
 arch/powerpc/net/bpf_jit_comp64.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c
index 02a59946a78af46416e9d949047ad3fefe3fc159..be3517ef0574d0911f6d63b530b33954c8ca343d 100644
--- a/arch/powerpc/net/bpf_jit_comp64.c
+++ b/arch/powerpc/net/bpf_jit_comp64.c
@@ -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
-- 
2.24.0.rc1.363.gb1bccd3e3d-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net] powerpc/bpf: fix tail call implementation
  2019-11-01  3:34 [PATCH net] powerpc/bpf: fix tail call implementation Eric Dumazet
@ 2019-11-01  4:39 ` David Miller
  2019-11-02  0:05 ` Daniel Borkmann
  1 sibling, 0 replies; 3+ messages in thread
From: David Miller @ 2019-11-01  4:39 UTC (permalink / raw)
  To: edumazet
  Cc: ast, daniel, netdev, eric.dumazet, bpf, naveen.n.rao, sandipan,
	benh, paulus, mpe, kafai, songliubraving, yhs

From: Eric Dumazet <edumazet@google.com>
Date: Thu, 31 Oct 2019 20:34:44 -0700

> 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")
> Signed-off-by: Eric Dumazet <edumazet@google.com>

I am anticipating this will go via the bpf tree.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net] powerpc/bpf: fix tail call implementation
  2019-11-01  3:34 [PATCH net] powerpc/bpf: fix tail call implementation Eric Dumazet
  2019-11-01  4:39 ` David Miller
@ 2019-11-02  0:05 ` Daniel Borkmann
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Borkmann @ 2019-11-02  0:05 UTC (permalink / raw)
  To: Eric Dumazet, Alexei Starovoitov
  Cc: David S . Miller, netdev, Eric Dumazet, bpf, Naveen N. Rao,
	Sandipan Das, Benjamin Herrenschmidt, Paul Mackerras,
	Michael Ellerman, Martin KaFai Lau, Song Liu, Yonghong Song

On 11/1/19 4:34 AM, Eric Dumazet wrote:
> 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")
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Cc: "Naveen N. Rao" <naveen.n.rao@linux.ibm.com>
> Cc: Sandipan Das <sandipan@linux.ibm.com>
> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
> Cc: Paul Mackerras <paulus@samba.org>
> Cc: Michael Ellerman <mpe@ellerman.id.au>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: Martin KaFai Lau <kafai@fb.com>
> Cc: Song Liu <songliubraving@fb.com>
> Cc: Yonghong Song <yhs@fb.com>
> Cc: netdev@vger.kernel.org
> Cc: bpf@vger.kernel.org

Applied, thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-11-02  0:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-01  3:34 [PATCH net] powerpc/bpf: fix tail call implementation Eric Dumazet
2019-11-01  4:39 ` David Miller
2019-11-02  0:05 ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).