From patchwork Thu Jan 14 07:33:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Shen Lim X-Patchwork-Id: 8030451 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D03309FC69 for ; Thu, 14 Jan 2016 07:35:55 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 02CA02049C for ; Thu, 14 Jan 2016 07:35:55 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 30C4720490 for ; Thu, 14 Jan 2016 07:35:54 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1aJcQH-0000bZ-H3; Thu, 14 Jan 2016 07:34:21 +0000 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1aJcQE-0000Xw-5S for linux-arm-kernel@lists.infradead.org; Thu, 14 Jan 2016 07:34:19 +0000 Received: by mail-pf0-x241.google.com with SMTP id 65so7038288pfd.1 for ; Wed, 13 Jan 2016 23:33:57 -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:in-reply-to:references; bh=NUlP0DTOjhVrGayNE5XjUZXEc820odv4aphVrQ73qzo=; b=OaUm6TPWDDWVD9lqnUu1jLKLkGvQv70q6GkxfVC6LZH1dxdp9Souwme3Zf3NaWZDO6 A56y8wG28M+uVilj/YjgRRAO3DOnGYl1U97ci+V3+pLhPyrG3ApU/cxU6/ikck60PVjF Fu+f17lC8FuDUTBscgpNrkTl0VgISE1Z0eE7U7JGSPInYECvVVldPuBMN7ywJaAejT/T DtJU4kwyKO5UR1U3+s3w/u1TVLah8kVqN2qDMhRynL7TyfVP+In7IzQi4qLU9oM91nwY mOYFq4/Z+fsZqQm1Ygjo2ApibceUehlUz7yBJb9tUMzkOmZpkk6P1K+OAPqC+YbGFKMD /C4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=NUlP0DTOjhVrGayNE5XjUZXEc820odv4aphVrQ73qzo=; b=M5hV7WhxzI7P6FI8LkrDcN2ANAojqE6d/PVGVTa0lC7b85xDX3e9WaFDUTapK9+BF/ ksdsCWeBYuaKWrTLCBGKC7fMdqxv0HGB8UTM5yqsJt7IjtqMU/C3K7Fg3+3jn9n+pC8m 15TH+E69KXQgSV3N0btRRGbYseYRoFW08ovhFYhzJPvUQyWNhxuDZ6ui+f+rq9dBoMXF 0oB3gpaz9qIMZfkUtzcraruiDuo945rHLTQPr2kZ/vz61mJt+JXJYMc4Txg2ECPw/Rxn jnwTmDmD1YnQ/Lreb+isQZgyOfdP5KbKOz3PKKZ5vb3qGAXjgFgXuKbWVnDyYJyWzzFc tSqw== X-Gm-Message-State: ALoCoQnFq0VomufHoLVAr7Y6q8b9/ym8FBO1t13t0X226Bwlo5KzyG66UrSQ1mK1bz8Nn0wfxnZw2ZLfov6xaFo/2LI/Xg6/jw== X-Received: by 10.98.15.207 with SMTP id 76mr3870718pfp.60.1452756837333; Wed, 13 Jan 2016 23:33:57 -0800 (PST) Received: from localhost.localdomain (c-73-223-118-172.hsd1.ca.comcast.net. [73.223.118.172]) by smtp.gmail.com with ESMTPSA id q27sm7021578pfi.80.2016.01.13.23.33.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 13 Jan 2016 23:33:56 -0800 (PST) From: Zi Shen Lim To: Alexei Starovoitov , Will Deacon , "David S. Miller" , Catalin Marinas Subject: [PATCH 2/2] arm64: bpf: add extra pass to handle faulty codegen Date: Wed, 13 Jan 2016 23:33:22 -0800 Message-Id: <1452756802-16511-2-git-send-email-zlim.lnx@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1452756802-16511-1-git-send-email-zlim.lnx@gmail.com> References: <1452756802-16511-1-git-send-email-zlim.lnx@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160113_233418_269356_666E6FB3 X-CRM114-Status: GOOD ( 15.73 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Rabin Vincent , Zi Shen Lim , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Code generation functions in arch/arm64/kernel/insn.c previously BUG_ON invalid parameters. Following change of that behavior, now we need to handle the error case where AARCH64_BREAK_FAULT is returned. Instead of error-handling on every emit() in JIT, we add a new validation pass at the end of JIT compilation. There's no point in running JITed code at run-time only to trap due to AARCH64_BREAK_FAULT. Instead, we drop this failed JIT compilation and allow the system to gracefully fallback on the BPF interpreter. Signed-off-by: Zi Shen Lim Suggested-by: Alexei Starovoitov Acked-by: Alexei Starovoitov --- Per discussion here: http://www.spinics.net/lists/arm-kernel/msg474179.html arch/arm64/net/bpf_jit_comp.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index d6a53ef..d66bc1f 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -1,7 +1,7 @@ /* * BPF JIT compiler for ARM64 * - * Copyright (C) 2014-2015 Zi Shen Lim + * Copyright (C) 2014-2016 Zi Shen Lim * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -726,6 +726,20 @@ static int build_body(struct jit_ctx *ctx) return 0; } +static int validate_code(struct jit_ctx *ctx) +{ + int i; + + for (i = 0; i < ctx->idx; i++) { + u32 a64_insn = le32_to_cpu(ctx->image[i]); + + if (a64_insn == AARCH64_BREAK_FAULT) + return -1; + } + + return 0; +} + static inline void bpf_flush_icache(void *start, void *end) { flush_icache_range((unsigned long)start, (unsigned long)end); @@ -788,6 +802,12 @@ void bpf_int_jit_compile(struct bpf_prog *prog) build_epilogue(&ctx); + /* 3. Extra pass to validate JITed code. */ + if (validate_code(&ctx)) { + bpf_jit_binary_free(header); + goto out; + } + /* And we're done. */ if (bpf_jit_enable > 1) bpf_jit_dump(prog->len, image_size, 2, ctx.image);