Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp267063ybj; Fri, 8 May 2020 11:18:36 -0700 (PDT) X-Google-Smtp-Source: APiQypJZDDy2u0sI5Cxtv1qQQn2MNTo+wxw2Srrd0cPno/VxhuaXQrl6rwJE1iC3i3FvV+JotWYw X-Received: by 2002:a17:906:4406:: with SMTP id x6mr3146373ejo.160.1588961916498; Fri, 08 May 2020 11:18:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588961916; cv=none; d=google.com; s=arc-20160816; b=UvqroeNm+dWMoMiXXhczo5OgDkfXZ2UPRkYrVC5tSOJozQqqckZvsdnJnPoCSTtfgO ZfATpOJzxPcPmWMx2WZPK4nGiOmmx2vkiw3tqCatr5pSe0UD7qz6fFDZKNBhuOmaFepb QMBWv/D7hyzQm5RCgfkJpYJskbMyGc76HX2JCVaVnOXGF2TQA/CaezwTHinJvR3sBI/S cWp5BpdcZo66cQeppG/EI7SP0Ic9hgS6rDx0fqdYjxCFNa7SddoaIzYAv1nWsTLewzA1 1IH/cG22Tw8Fuqr2QXKeJ0gY6piX0xg0CPjPAO1XytMrAosXBFVxv7vIL9JRICiBeBwA 19hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=1RkJFS+t50tivJwSHzxWQpizFYGiyRAPi5B/W8OYM90=; b=VU4cvr2mY2uRUADZ7tzi5Dvtg4yez9uUbk1twQUdH1EeMnqavU+xZqQg43SiIM3NbY Pu0Oo8uRTGrsjwggeToDT55qDu4GhsFdZfx7Pqdg5AYkVtC2JMis3ne+fRjPcxniwH+a bN6dY04E9RaN6Siq1jP1IZr5i7+43C2RmpNJ24Y2EQPoQONwjeIQlGytlK9HNjDdtBly M3UTtgE3VmbBZqE64TlhY6o2YSho+odMcpZdD4i9spYeU+rWDAsDu1/RStHD1LFzQSSS q/BLoTY+KP2Nmdb07ya+FoA6TofNnexu5BSrWtrKiuEiHBJZzM6yhCdGvsVStxEeaa6E N0pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cs.washington.edu header.s=goo201206 header.b=CCWu0Lup; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=cs.washington.edu Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id aq11si1356003ejc.307.2020.05.08.11.18.12; Fri, 08 May 2020 11:18:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@cs.washington.edu header.s=goo201206 header.b=CCWu0Lup; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=cs.washington.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727911AbgEHSQM (ORCPT + 99 others); Fri, 8 May 2020 14:16:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727878AbgEHSQG (ORCPT ); Fri, 8 May 2020 14:16:06 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46941C05BD43 for ; Fri, 8 May 2020 11:16:06 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id t11so1232544pgg.2 for ; Fri, 08 May 2020 11:16:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1RkJFS+t50tivJwSHzxWQpizFYGiyRAPi5B/W8OYM90=; b=CCWu0LupJQSLY09+5kTFGNYiHYCgaMr3aDRuRcSjVMD7wkRduR4zrHrWLKa53mVjjP 9DMnW8euiPBliMtpPia67Qi698Z94wMFIWcXHzXx/3KnrghW58Dl0By0ArPQMx0jRC4E xAxlH1UM5bTdYo8pmzNqUBGKcKRxGOFWRpDAk= 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=1RkJFS+t50tivJwSHzxWQpizFYGiyRAPi5B/W8OYM90=; b=m+UR9fl75DyjI951E+ckTvVTYJz0Fai3awkJx3FJ7kLSEBO9tOsv63MRXo4cxsab4E psvupwoCfXtyIr29TXKoPrB86/4fMpKAwedji0+hBNYvdsYji7kS0IzEPUbrUMoKgu7d NFl6grui2LD8G7m+RgTL4YGHiSMMOvZ4lSXKnVCf8S8sbaCIyv3MNZpZJpgO1mFF9/ah X18or31R2wisue17xZ6DUX62z6oogNkGjI6T7cb/lACpUbLUMXgchHc2Mf6loxbkE+Pk PL8iOhE5yeyDc6s5qxzoQcfPBCjvjAQ5VcHMarNFxkGbvKioqLk66U3gGZ94qIm2mcrS C0YQ== X-Gm-Message-State: AGi0PubctCHCUpLz1lnp5yirS75SeOKR8vQ8YkhcGxR93VTxL+SOIGsc 2Sl0jwEsSRW2USxFxdks7vvb0g== X-Received: by 2002:aa7:951b:: with SMTP id b27mr4228455pfp.2.1588961765560; Fri, 08 May 2020 11:16:05 -0700 (PDT) Received: from localhost.localdomain (c-73-53-94-119.hsd1.wa.comcast.net. [73.53.94.119]) by smtp.gmail.com with ESMTPSA id e11sm2349463pfl.85.2020.05.08.11.16.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 May 2020 11:16:05 -0700 (PDT) From: Luke Nelson X-Google-Original-From: Luke Nelson To: bpf@vger.kernel.org Cc: Luke Nelson , Xi Wang , Catalin Marinas , Will Deacon , Daniel Borkmann , Alexei Starovoitov , Zi Shen Lim , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , Mark Rutland , Alexios Zavras , Greg Kroah-Hartman , Allison Randal , Thomas Gleixner , Christoffer Dall , Marc Zyngier , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, clang-built-linux@googlegroups.com Subject: [PATCH bpf-next v2 3/3] bpf, arm64: Optimize ADD,SUB,JMP BPF_K using arm64 add/sub immediates Date: Fri, 8 May 2020 11:15:46 -0700 Message-Id: <20200508181547.24783-4-luke.r.nels@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200508181547.24783-1-luke.r.nels@gmail.com> References: <20200508181547.24783-1-luke.r.nels@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The current code for BPF_{ADD,SUB} BPF_K loads the BPF immediate to a temporary register before performing the addition/subtraction. Similarly, BPF_JMP BPF_K cases load the immediate to a temporary register before comparison. This patch introduces optimizations that use arm64 immediate add, sub, cmn, or cmp instructions when the BPF immediate fits. If the immediate does not fit, it falls back to using a temporary register. Example of generated code for BPF_ALU64_IMM(BPF_ADD, R0, 2): without optimization: 24: mov x10, #0x2 28: add x7, x7, x10 with optimization: 24: add x7, x7, #0x2 The code could use A64_{ADD,SUB}_I directly and check if it returns AARCH64_BREAK_FAULT, similar to how logical immediates are handled. However, aarch64_insn_gen_add_sub_imm from insn.c prints error messages when the immediate does not fit, and it's simpler to check if the immediate fits ahead of time. Co-developed-by: Xi Wang Signed-off-by: Xi Wang Signed-off-by: Luke Nelson Acked-by: Daniel Borkmann --- arch/arm64/net/bpf_jit.h | 8 ++++++++ arch/arm64/net/bpf_jit_comp.c | 36 +++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/arch/arm64/net/bpf_jit.h b/arch/arm64/net/bpf_jit.h index f36a779949e6..923ae7ff68c8 100644 --- a/arch/arm64/net/bpf_jit.h +++ b/arch/arm64/net/bpf_jit.h @@ -100,6 +100,14 @@ /* Rd = Rn OP imm12 */ #define A64_ADD_I(sf, Rd, Rn, imm12) A64_ADDSUB_IMM(sf, Rd, Rn, imm12, ADD) #define A64_SUB_I(sf, Rd, Rn, imm12) A64_ADDSUB_IMM(sf, Rd, Rn, imm12, SUB) +#define A64_ADDS_I(sf, Rd, Rn, imm12) \ + A64_ADDSUB_IMM(sf, Rd, Rn, imm12, ADD_SETFLAGS) +#define A64_SUBS_I(sf, Rd, Rn, imm12) \ + A64_ADDSUB_IMM(sf, Rd, Rn, imm12, SUB_SETFLAGS) +/* Rn + imm12; set condition flags */ +#define A64_CMN_I(sf, Rn, imm12) A64_ADDS_I(sf, A64_ZR, Rn, imm12) +/* Rn - imm12; set condition flags */ +#define A64_CMP_I(sf, Rn, imm12) A64_SUBS_I(sf, A64_ZR, Rn, imm12) /* Rd = Rn */ #define A64_MOV(sf, Rd, Rn) A64_ADD_I(sf, Rd, Rn, 0) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 083e5d8a5e2c..561a2fea9cdd 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -167,6 +167,12 @@ static inline int epilogue_offset(const struct jit_ctx *ctx) return to - from; } +static bool is_addsub_imm(u32 imm) +{ + /* Either imm12 or shifted imm12. */ + return !(imm & ~0xfff) || !(imm & ~0xfff000); +} + /* Stack must be multiples of 16B */ #define STACK_ALIGN(sz) (((sz) + 15) & ~15) @@ -479,13 +485,25 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, /* dst = dst OP imm */ case BPF_ALU | BPF_ADD | BPF_K: case BPF_ALU64 | BPF_ADD | BPF_K: - emit_a64_mov_i(is64, tmp, imm, ctx); - emit(A64_ADD(is64, dst, dst, tmp), ctx); + if (is_addsub_imm(imm)) { + emit(A64_ADD_I(is64, dst, dst, imm), ctx); + } else if (is_addsub_imm(-imm)) { + emit(A64_SUB_I(is64, dst, dst, -imm), ctx); + } else { + emit_a64_mov_i(is64, tmp, imm, ctx); + emit(A64_ADD(is64, dst, dst, tmp), ctx); + } break; case BPF_ALU | BPF_SUB | BPF_K: case BPF_ALU64 | BPF_SUB | BPF_K: - emit_a64_mov_i(is64, tmp, imm, ctx); - emit(A64_SUB(is64, dst, dst, tmp), ctx); + if (is_addsub_imm(imm)) { + emit(A64_SUB_I(is64, dst, dst, imm), ctx); + } else if (is_addsub_imm(-imm)) { + emit(A64_ADD_I(is64, dst, dst, -imm), ctx); + } else { + emit_a64_mov_i(is64, tmp, imm, ctx); + emit(A64_SUB(is64, dst, dst, tmp), ctx); + } break; case BPF_ALU | BPF_AND | BPF_K: case BPF_ALU64 | BPF_AND | BPF_K: @@ -639,8 +657,14 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, case BPF_JMP32 | BPF_JSLT | BPF_K: case BPF_JMP32 | BPF_JSGE | BPF_K: case BPF_JMP32 | BPF_JSLE | BPF_K: - emit_a64_mov_i(is64, tmp, imm, ctx); - emit(A64_CMP(is64, dst, tmp), ctx); + if (is_addsub_imm(imm)) { + emit(A64_CMP_I(is64, dst, imm), ctx); + } else if (is_addsub_imm(-imm)) { + emit(A64_CMN_I(is64, dst, -imm), ctx); + } else { + emit_a64_mov_i(is64, tmp, imm, ctx); + emit(A64_CMP(is64, dst, tmp), ctx); + } goto emit_cond_jmp; case BPF_JMP | BPF_JSET | BPF_K: case BPF_JMP32 | BPF_JSET | BPF_K: -- 2.17.1