From patchwork Wed Dec 3 08:38:01 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Shen Lim X-Patchwork-Id: 417290 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 988131400A0 for ; Wed, 3 Dec 2014 19:38:42 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751990AbaLCIi2 (ORCPT ); Wed, 3 Dec 2014 03:38:28 -0500 Received: from mail-pa0-f48.google.com ([209.85.220.48]:46118 "EHLO mail-pa0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751285AbaLCIi1 (ORCPT ); Wed, 3 Dec 2014 03:38:27 -0500 Received: by mail-pa0-f48.google.com with SMTP id rd3so15346681pab.21 for ; Wed, 03 Dec 2014 00:38:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=Gc3rG23mAKkUYrg3/vjZKwzNxsP4d2XIx4e7jtWJn+A=; b=wL7ykxSH5LHWMUsGCSDYFFL/19nCLKlDolmZu57pEsWCqkbzgzecif06MM4s/m//m5 EoyRXW2dqMRKRnsH2v+6Ms2PUJItSVSgBN8MepSeo3pMYuFRIY4O1W6NSXiQ0eVhltwt uzqbq+WtfWYcfns8wLvrcHaao6U9f3lU1DfF5m77HDzitMBiRRpEcbNynfnxyFAW8/zu 5usQOpiwuG56Uo43EY85S9zZP9gePBOBg3c1qLrTLxI3fDsvIkwIBtvW9UXGg7hZzucM bJtOgA5nLHLLfspF4V3YUpX1q6we8cESjdEKT9v6xDgrkZScQXJLSiyPoHfsIqXrxTOe YUvg== X-Received: by 10.70.91.208 with SMTP id cg16mr6418186pdb.144.1417595906593; Wed, 03 Dec 2014 00:38:26 -0800 (PST) Received: from gup76.hsd1.ca.comcast.net ([98.234.176.204]) by mx.google.com with ESMTPSA id v8sm12602890pdp.94.2014.12.03.00.38.24 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 03 Dec 2014 00:38:25 -0800 (PST) From: Zi Shen Lim To: Alexei Starovoitov , "David S. Miller" , Catalin Marinas , Will Deacon Cc: Zi Shen Lim , Daniel Borkmann , netdev@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] bpf: arm64: lift restriction on last instruction Date: Wed, 3 Dec 2014 00:38:01 -0800 Message-Id: <1417595881-32218-1-git-send-email-zlim.lnx@gmail.com> X-Mailer: git-send-email 1.9.1 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Earlier implementation assumed last instruction is BPF_EXIT. Since this is no longer a restriction in eBPF, we remove this limitation. Per Alexei Starovoitov [1]: > classic BPF has a restriction that last insn is always BPF_RET. > eBPF doesn't have BPF_RET instruction and this restriction. > It has BPF_EXIT insn which can appear anywhere in the program > one or more times and it doesn't have to be last insn. [1] https://lkml.org/lkml/2014/11/27/2 Fixes: e54bcde3d69d ("arm64: eBPF JIT compiler") Signed-off-by: Zi Shen Lim Acked-by: Alexei Starovoitov --- arch/arm64/net/bpf_jit_comp.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 41f1e3e..edba042 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -60,7 +60,7 @@ struct jit_ctx { const struct bpf_prog *prog; int idx; int tmp_used; - int body_offset; + int epilogue_offset; int *offset; u32 *image; }; @@ -130,8 +130,8 @@ static void jit_fill_hole(void *area, unsigned int size) static inline int epilogue_offset(const struct jit_ctx *ctx) { - int to = ctx->offset[ctx->prog->len - 1]; - int from = ctx->idx - ctx->body_offset; + int to = ctx->epilogue_offset; + int from = ctx->idx; return to - from; } @@ -463,6 +463,8 @@ emit_cond_jmp: } /* function return */ case BPF_JMP | BPF_EXIT: + /* Optimization: when last instruction is EXIT, + simply fallthrough to epilogue. */ if (i == ctx->prog->len - 1) break; jmp_offset = epilogue_offset(ctx); @@ -685,11 +687,13 @@ void bpf_int_jit_compile(struct bpf_prog *prog) /* 1. Initial fake pass to compute ctx->idx. */ - /* Fake pass to fill in ctx->offset. */ + /* Fake pass to fill in ctx->offset and ctx->tmp_used. */ if (build_body(&ctx)) goto out; build_prologue(&ctx); + + ctx.epilogue_offset = ctx.idx; build_epilogue(&ctx); /* Now we know the actual image size. */ @@ -706,7 +710,6 @@ void bpf_int_jit_compile(struct bpf_prog *prog) build_prologue(&ctx); - ctx.body_offset = ctx.idx; if (build_body(&ctx)) { bpf_jit_binary_free(header); goto out;