From patchwork Fri May 3 10:42:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiong Wang X-Patchwork-Id: 1094783 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.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=bpf-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="v4ZxxQ1K"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44wTJR6yPTz9s6w for ; Fri, 3 May 2019 20:43:51 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727483AbfECKnv (ORCPT ); Fri, 3 May 2019 06:43:51 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:35988 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727261AbfECKnt (ORCPT ); Fri, 3 May 2019 06:43:49 -0400 Received: by mail-wr1-f68.google.com with SMTP id o4so7274519wra.3 for ; Fri, 03 May 2019 03:43:48 -0700 (PDT) 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; bh=K4gLqJ4SgDOsgXMmICWdVA0zCxYikPtUo1d3XLqhgFA=; b=v4ZxxQ1KTyQ5O3GeNw5PvYYAdDrhFj+uHrpzgQVuLgBCOWg8q4gl1TQAPHFVHG8G0M TEKqIFHWoSQPaRAhxznmu7UERdvAqkSOVIpwS83PoA2vHPp3SsDFBgZJRCYdwHSUbpS0 c2FaEXJdqSMJxJk55GacCwDsKNicaaN9KTs/SktNyMOP4s7IPRBoOdEKCJfTbbiX7c3P bAVEWJU7Ozt2t+tRSpLU0LjDSWyr5GTGu5HJPn2zPb/ac21AR5SJpAZktY659TS8Fl3q snq8Dh7vRZbcw8VWDRNvClUyOG67nOXuUTAt8W8j57fXrW7nzfz1+r3TQQySTwFpri5z 5hyw== 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; bh=K4gLqJ4SgDOsgXMmICWdVA0zCxYikPtUo1d3XLqhgFA=; b=hC8M7rj117DT0j6V/0njYXWZ5lVsWgXAqmm+Ckr+e7KgsYVpTVBGkcvGBGvsHRsKQA wY+gtHAoH1Trn9jcPQhiBs61+P6+C2JaAJ7kjKSiqqPn/0X8nWdlegoA/Amsqx9NXIgQ YYRSNNZb81rvqH5Klp3B6/xXptST+W6ub/0nhodTgVPWCycP/xHykS4Ocdy6mTDHU57c BuEW/KFaxf4/DJZZJVvw1ESau19pLcQT5J8wySZ/hkTVGT6e593JfDW0zh53SPYQSJtj IJdi+mo2SXmj9CPUvbm/mzXIwkBjfrkhnRzpWDpWDjpOG8j+7ihzrlHfVCBjBjy/F+b3 xTNw== X-Gm-Message-State: APjAAAWVgLUjr3/kHxITtHvjdDLPOe4zKc6ioxLh/TIUM7YvHXLXqq/t +pwV4Y4XlQBE9vu0kOg0OqtaYV7Mu/4= X-Google-Smtp-Source: APXvYqxmES30jzauIQvZznWOLf6CJ5RhObBl0zMx451X3bpaaoUw5IVsoYJ1tggNqzvt+fwqfUwyeQ== X-Received: by 2002:a5d:4942:: with SMTP id r2mr6157661wrs.159.1556880227408; Fri, 03 May 2019 03:43:47 -0700 (PDT) Received: from cbtest28.netronome.com ([217.38.71.146]) by smtp.gmail.com with ESMTPSA id r29sm1716999wra.56.2019.05.03.03.43.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 03 May 2019 03:43:46 -0700 (PDT) From: Jiong Wang To: alexei.starovoitov@gmail.com, daniel@iogearbox.net Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, oss-drivers@netronome.com, Jiong Wang Subject: [PATCH v6 bpf-next 04/17] bpf: introduce new alu insn BPF_ZEXT for explicit zero extension Date: Fri, 3 May 2019 11:42:31 +0100 Message-Id: <1556880164-10689-5-git-send-email-jiong.wang@netronome.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1556880164-10689-1-git-send-email-jiong.wang@netronome.com> References: <1556880164-10689-1-git-send-email-jiong.wang@netronome.com> Sender: bpf-owner@vger.kernel.org Precedence: bulk List-Id: netdev.vger.kernel.org This patch introduce new alu32 insn BPF_ZEXT, and allocate the unused opcode 0xe0 to it. Compared with the other alu32 insns, zero extension on low 32-bit is the only semantics for this instruction. It also allows various JIT back-ends to do optimal zero extension code-gen. BPF_ZEXT is supposed to be encoded with BPF_ALU only, and is supposed to be generated by the latter 32-bit optimization code inside verifier for those arches that do not support hardware implicit zero extension only. It is not supposed to be used in user's program directly at the moment. Therefore, no need to recognize it inside generic verification code. It just need to be supported for execution on interpreter or related JIT back-ends. Signed-off-by: Jiong Wang --- Documentation/networking/filter.txt | 10 ++++++++++ include/uapi/linux/bpf.h | 3 +++ kernel/bpf/core.c | 4 ++++ tools/include/uapi/linux/bpf.h | 3 +++ 4 files changed, 20 insertions(+) diff --git a/Documentation/networking/filter.txt b/Documentation/networking/filter.txt index 319e5e0..1cb3e42 100644 --- a/Documentation/networking/filter.txt +++ b/Documentation/networking/filter.txt @@ -903,6 +903,16 @@ If BPF_CLASS(code) == BPF_ALU or BPF_ALU64 [ in eBPF ], BPF_OP(code) is one of: BPF_MOV 0xb0 /* eBPF only: mov reg to reg */ BPF_ARSH 0xc0 /* eBPF only: sign extending shift right */ BPF_END 0xd0 /* eBPF only: endianness conversion */ + BPF_ZEXT 0xe0 /* eBPF BPF_ALU only: zero-extends low 32-bit */ + +Compared with BPF_ALU | BPF_MOV which zero-extends low 32-bit implicitly, +BPF_ALU | BPF_ZEXT zero-extends low 32-bit explicitly. Such zero extension is +not the main semantics for the prior, but is for the latter. Therefore, JIT +optimizer could optimize out the zero extension for the prior when it is +concluded safe to do so, but should never do such optimization for the latter. +LLVM compiler won't generate BPF_ZEXT, and hand written assembly is not supposed +to use it. Verifier 32-bit optimization pass, which removes zero extension +semantics from the other BPF_ALU instructions, is the only place generates it. If BPF_CLASS(code) == BPF_JMP or BPF_JMP32 [ in eBPF ], BPF_OP(code) is one of: diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 72336ba..22ccdf4 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -32,6 +32,9 @@ #define BPF_FROM_LE BPF_TO_LE #define BPF_FROM_BE BPF_TO_BE +/* zero extend low 32-bit */ +#define BPF_ZEXT 0xe0 + /* jmp encodings */ #define BPF_JNE 0x50 /* jump != */ #define BPF_JLT 0xa0 /* LT is unsigned, '<' */ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 2792eda..ee8703d 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -1152,6 +1152,7 @@ EXPORT_SYMBOL_GPL(__bpf_call_base); INSN_2(ALU, NEG), \ INSN_3(ALU, END, TO_BE), \ INSN_3(ALU, END, TO_LE), \ + INSN_2(ALU, ZEXT), \ /* Immediate based. */ \ INSN_3(ALU, ADD, K), \ INSN_3(ALU, SUB, K), \ @@ -1352,6 +1353,9 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack) ALU64_NEG: DST = -DST; CONT; + ALU_ZEXT: + DST = (u32) DST; + CONT; ALU_MOV_X: DST = (u32) SRC; CONT; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 72336ba..22ccdf4 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -32,6 +32,9 @@ #define BPF_FROM_LE BPF_TO_LE #define BPF_FROM_BE BPF_TO_BE +/* zero extend low 32-bit */ +#define BPF_ZEXT 0xe0 + /* jmp encodings */ #define BPF_JNE 0x50 /* jump != */ #define BPF_JLT 0xa0 /* LT is unsigned, '<' */