From patchwork Wed Jan 23 06:45:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 1029696 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=netronome.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=netronome-com.20150623.gappssmtp.com header.i=@netronome-com.20150623.gappssmtp.com header.b="GjZbxmMI"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43kwm15BKnz9s9h for ; Wed, 23 Jan 2019 17:45:53 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726783AbfAWGpx (ORCPT ); Wed, 23 Jan 2019 01:45:53 -0500 Received: from mail-qt1-f194.google.com ([209.85.160.194]:39285 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726296AbfAWGpu (ORCPT ); Wed, 23 Jan 2019 01:45:50 -0500 Received: by mail-qt1-f194.google.com with SMTP id u47so1214431qtj.6 for ; Tue, 22 Jan 2019 22:45:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netronome-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7R2L/KWHLc4strshAoHLxhN0V8+OvyoyKWc7/8jxgGI=; b=GjZbxmMIWYghWsGWOXVn7SkMGunRJV8TJiaJ0h1DBB3WjtvK3XFhbHOxp/0o+h92Ed 0Z2Rb/qlo4Ka0+lBVOLeRnqYOtfks6XqrtYWQqn+XyeG2FgXjSF7xEQWyFSTeshMq1PT mjHJ8kgEb3h2HisbLXDsPlc9YpzHZFevFKjbqSqRlFDELyC2pgfgAX+BZ70EGseDvRbP 5h39v8EZre9LCjnG3sGd0oAoaTXwo/6onBuceKrQNnN8cvssCcMRbAgUzNDi4HjHRgmL XOU/N/BLj2p6lBvKdOkMC6rpy7ySB6Gzg4JwQL8kFtWD3xeIyiecCWl0YMqrawKoy8/v hLRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7R2L/KWHLc4strshAoHLxhN0V8+OvyoyKWc7/8jxgGI=; b=cObPp+lWX3OjBGsz2F2gROopmXQRlSzKSw8eqV6QM/tVyaJWkIg8YuYQI0kGZaSSeJ MvUcaVnRnGLr6YnhyRJ0aFK0jwbA3sUpqJsJEuX8nT3lgze2TKgwFukVnLW9tOVMSy5I 8sBp9W5ufCI9q1+YQ3vMPsGHE3K27u84EwZirUVdcbmYYQ0fRg4x6Dn+BDMC318NjEWG TPQz7IdUdztiTiyv/foi1qTHVh4GOhL5AX1TSljkC/PJ2h1yVXNlH2o7rXLmdG7uB+WD 4twOyNRxd9HBCT2hw0zi6Fh0iZPAuNFC6RggWq65OQt+R0pKjdcnNVUujGLDbZfPkm+G itYQ== X-Gm-Message-State: AJcUukenpJuZme98Yg9d1MH4EoCP6kMDSCzjzrR9rRTMpwUne9sC/TiU hBLGf72Zm9oQpAN4l8tyMqhhHA== X-Google-Smtp-Source: ALg8bN48zpal6Ky8TcjCMJW86HF9SC7XtycMjJ2/N9znHTA7HKNBxkuY/kuewesBv06DdA2BOLuvcg== X-Received: by 2002:a0c:fa8f:: with SMTP id o15mr894926qvn.135.1548225949115; Tue, 22 Jan 2019 22:45:49 -0800 (PST) Received: from jkicinski-Precision-T1700.netronome.com ([66.60.152.14]) by smtp.gmail.com with ESMTPSA id k6sm80589046qtk.41.2019.01.22.22.45.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Jan 2019 22:45:48 -0800 (PST) From: Jakub Kicinski To: alexei.starovoitov@gmail.com, daniel@iogearbox.net Cc: yhs@fb.com, netdev@vger.kernel.org, oss-drivers@netronome.com, kafai@fb.com, Jakub Kicinski Subject: [PATCH bpf-next v5 04/12] bpf: verifier: remove unconditional branches by 0 Date: Tue, 22 Jan 2019 22:45:21 -0800 Message-Id: <20190123064529.13518-5-jakub.kicinski@netronome.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190123064529.13518-1-jakub.kicinski@netronome.com> References: <20190123064529.13518-1-jakub.kicinski@netronome.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Unconditional branches by 0 instructions are basically noops but they can result from earlier optimizations, e.g. a conditional jumps which would never be taken or a conditional jump around dead code. Remove those branches. v0.2: - s/opt_remove_dead_branches/opt_remove_nops/ (Jiong). Signed-off-by: Jakub Kicinski Reviewed-by: Jiong Wang Acked-by: Yonghong Song --- kernel/bpf/verifier.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 099b2541f87f..f39bca188a5c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -6660,6 +6660,27 @@ static int opt_remove_dead_code(struct bpf_verifier_env *env) return 0; } +static int opt_remove_nops(struct bpf_verifier_env *env) +{ + const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); + struct bpf_insn *insn = env->prog->insnsi; + int insn_cnt = env->prog->len; + int i, err; + + for (i = 0; i < insn_cnt; i++) { + if (memcmp(&insn[i], &ja, sizeof(ja))) + continue; + + err = verifier_remove_insns(env, i, 1); + if (err) + return err; + insn_cnt--; + i--; + } + + return 0; +} + /* convert load instructions that access fields of a context type into a * sequence of instructions that access fields of the underlying structure: * struct __sk_buff -> struct sk_buff @@ -7452,6 +7473,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, opt_hard_wire_dead_code_branches(env); if (ret == 0) ret = opt_remove_dead_code(env); + if (ret == 0) + ret = opt_remove_nops(env); } else { if (ret == 0) sanitize_dead_code(env);