Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1927505ybl; Tue, 3 Dec 2019 15:09:07 -0800 (PST) X-Google-Smtp-Source: APXvYqx9MJJLRsFg9heKenup0FcK/WNpxwEvsTMqOz6kTq8NBRGeqOYeReGJTtPqiIdfE9lD3Ore X-Received: by 2002:a05:6808:b18:: with SMTP id s24mr75865oij.31.1575414547496; Tue, 03 Dec 2019 15:09:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575414547; cv=none; d=google.com; s=arc-20160816; b=pN533EvJVUmWwU6J8dXnDL4yaYJdhQh93SetEd0B5Wk2AoWUMwOp+1L5n/C6QBeX7y 6YlKUNhWnolFBDJRx0SohhvVcP6WrmCPBbPVy/ZXn+gO9v6a0IEgGcbO73R/xht5pFCa fJNtirDGHtnlfSI/k6RQVgwW6Mlr1L18ChY/o7/hf84YRfyRR/lXdDwE4Lia94GJLmGF 6VDM0vDLobWZ0BGOxY/tSHixdxmORZ8Fc0o+2XLR81l896tpWdJU1rBuumjOZiYHOEFy pI9Pk8G6v6PtVsDK54gaRxeFJTqjmkuIJyNS/DjOE2QVq0yvYQ9HgPrrqrYRAYRkibR5 Dvow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Grs5tyZ/7y/amLnLTk6iZ/1EjkF0pLqUio5bylGxzPg=; b=s4II6RMCFn/0v7EIayrWfMeCx4AyrfIG95/oiAu1UZ4IbDa7HdsfsUIMiqDjNXMQgQ YjDafuSJrFB/6iu5CtOaF5ICfq5lY5aDBmk0RaAIQ3mwOW9cmr+hyyvtLZwxkzag9arn NK90h6SNyradIR0JQ7b29r0z/w82sHADRDD57ooqhhG9TqVyU18p0pXm7ujb4J36uz/m m8/IyD0T5jzuZ1++F95o7lcZPgSVppsVTbhpzC46uNzAeK4cpyHvlHa3ZehPk95mmFCV USmbAI7j1Z0Ekk7qMetWylrzVkvRscR3PR8TShLgTrQr6P3ovmU5512a944HeckrmSD8 G06A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Hzf2MFFg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v19si2172070otn.313.2019.12.03.15.08.55; Tue, 03 Dec 2019 15:09:07 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Hzf2MFFg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729359AbfLCXIW (ORCPT + 99 others); Tue, 3 Dec 2019 18:08:22 -0500 Received: from mail.kernel.org ([198.145.29.99]:34696 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729106AbfLCWpt (ORCPT ); Tue, 3 Dec 2019 17:45:49 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 843432084B; Tue, 3 Dec 2019 22:45:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575413149; bh=2gy4TInhbQX++Sx+cyKimzTAGXbO8qqAErtJUsX9N+U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hzf2MFFgnqtCBUn5baEdyVYYzgJG77EyHwbmV5Ml2IV3BPk9TbKkvKuMOngn6BtoG S9zBGjIelC0gFCnNSyX722zMQNF9l0C8KEhNpDxcAMNlMvyoHSwr0HXDI8ujj60d+c qRcXReOB7HqfAgTUgFexT2ZlQGabqRJgXbK1enL0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , Daniel Borkmann , "Naveen N. Rao" , Sandipan Das , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , Martin KaFai Lau , Song Liu , Yonghong Song , Sasha Levin Subject: [PATCH 4.19 018/321] powerpc/bpf: Fix tail call implementation Date: Tue, 3 Dec 2019 23:31:24 +0100 Message-Id: <20191203223428.068100947@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191203223427.103571230@linuxfoundation.org> References: <20191203223427.103571230@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet [ Upstream commit 7de086909365cd60a5619a45af3f4152516fd75c ] 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 Signed-off-by: Daniel Borkmann Cc: Naveen N. Rao Cc: Sandipan Das Cc: Benjamin Herrenschmidt Cc: Paul Mackerras Cc: Michael Ellerman Cc: Martin KaFai Lau Cc: Song Liu Cc: Yonghong Song Link: https://lore.kernel.org/bpf/20191101033444.143741-1-edumazet@google.com Signed-off-by: Sasha Levin --- 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 279a51bf94d05..7e3ab477f67fe 100644 --- a/arch/powerpc/net/bpf_jit_comp64.c +++ b/arch/powerpc/net/bpf_jit_comp64.c @@ -949,6 +949,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.20.1