Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1941736ybl; Tue, 3 Dec 2019 15:22:55 -0800 (PST) X-Google-Smtp-Source: APXvYqw5ycuptQe0Bk26mBPBhslGiIQUV4f5QQAv9avEvA/1HxlLBq2pjmf7oQv5z/1WyrJ7IiN+ X-Received: by 2002:aca:758c:: with SMTP id q134mr140967oic.33.1575415375169; Tue, 03 Dec 2019 15:22:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575415375; cv=none; d=google.com; s=arc-20160816; b=QnRS1ZlaIW2bpUNaXrRvbiouExD0AqmQhD5u5lhv6HDihpSkvLIQFL4tDu80v3LiQY xrtUbMNr+730Ob0kxGMJHKOYHVKg6jqAWzOBip++fHKRLtrOQ37AAQ0Q2RAXjeCql7OC I+9L3N6d4R7zpBi9PeMsuycY9esXCELRwZMiXoQNr00SlQgPp2bg0tvjTfnlZVWxEfCK 9V/6Hs+e+bh2d1M4z9CfB69ZeMn0TqJEDmFa2XtNaRoIJE4kC2Jb8OlEKutp1U78fspg Un33YBfBA50yRWN0seQBlnmUD563n8v0/MmXvFaOFHHWDqjukJAxcmTbB2k7P1qQKzhI o0MQ== 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=RGwbtCP9ke81vuOrHo7/Wunjl1T4gCJloay2259Xuos=; b=XJJd0X+B6aPOIXfraQdibBZXLHX5mYnQWcZJyhhFKMBg1EUQsLRVyX85skrLNaHB7J 1mLcio2L0wdBS1WhIMD/St7JNZXzeEpOZllVp0zmAfuWS+PulQdQNgf2ig0kmcsMfxhZ dQjjsA3cG/4nzG/VHGwISx81bvH23cL84L7idNb4iSPGSQnFarLdOZHLznfgY7/pTygh dtyjDxBYo3urp45dA1gOYFj76er64jEnUBjlOtT+3HaWkAH/RP+bD4ILIAVHN4wOtUUA hzBsCSLBpAGYieIjwI5iHtM2GK34h1YIDnJIggqmz/2EeDS1xp886UeJPsiWh2E3rspy 2OEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tnOqC0AJ; 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 f3si2304738oia.264.2019.12.03.15.22.38; Tue, 03 Dec 2019 15:22:55 -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=tnOqC0AJ; 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 S1728493AbfLCXLs (ORCPT + 99 others); Tue, 3 Dec 2019 18:11:48 -0500 Received: from mail.kernel.org ([198.145.29.99]:54022 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728431AbfLCWkk (ORCPT ); Tue, 3 Dec 2019 17:40:40 -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 D5D3420684; Tue, 3 Dec 2019 22:40:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575412839; bh=YKtNUsduV5isZO5nsHf4mSCvJD88xZ0PmVpG0Onr6VU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tnOqC0AJovc/gldiqjhvixymM8HP1g1DjZxDyLGfuYoJLCppu4PCTiHN2hvWANLgX 5KrWSr/a9lGx//ByRt4XextXjkdjtt5LMWPPAmF4MfSklFm/HQk+5AhWfd8Q55UbXl s/9NH/CFayiqpCVoxYoHyI7wvgR3vcjBgB45nPt0= 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 5.3 035/135] powerpc/bpf: Fix tail call implementation Date: Tue, 3 Dec 2019 23:34:35 +0100 Message-Id: <20191203213013.128883661@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191203213005.828543156@linuxfoundation.org> References: <20191203213005.828543156@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 02a59946a78af..be3517ef0574d 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.20.1