Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp3160761pxb; Tue, 12 Jan 2021 07:46:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJzzaPCkaaeZzR8vN8n6WGgioIFjjkrecMN40NGJBLkXnhUCUb1aSZ5Dy8e8qyaaQIqqchEM X-Received: by 2002:aa7:d7d8:: with SMTP id e24mr3829539eds.135.1610466377205; Tue, 12 Jan 2021 07:46:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610466377; cv=none; d=google.com; s=arc-20160816; b=LRCCXoM9e647iEYxVI+nusc4lFIYWQN2JyvjnfPSd547GjGDv8+z6VYSeodd/UPPqW SPWtt3zwIN96VMWOr0HQOit6HKTnLm64nt57DeEBI+vfaBwItkbHeSUUrp6ufBjLuA/r pJAj9oqVXIq2QVqyUk38RgeXLWnXMIrtx7vakaPIlI1tH/EYG4FyHC+OZ/61c062vpbR gikAUeZwX22ZQ5SuDgpBeZZMXhb1B5XR8uNAnSzP2RYuM8sGOzeq74aZ8yq3gecjeMTd osO/P+FjsuKxxT44e+CIzFfpFKsBfTukvkj5GuBzujVnzElHqcTTNjxGO+4lE9z5G51K RIMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=4StJvEPcqpOoMaFQqXp2l4E4gqPLXrwP2qT6WBxKOzM=; b=Ob9BeE4OQodqlOM2IcbD3hgCaxQ/Wh19iYJXuc7BNkU5H4D9eHEhw/BXZ4KdpPKKhe Mjmdre2zyi+i4Zl1SdmcJeVwN2ew4WvzwfgY072m4tetcDmS9uNlwMADXCWu5fjZWnaT I1lcJrQJCWP0mltyEHWLRKiQ0IfW5xBVLCqi3hWGinUUdu9XyWlpAYD/ck0R+o3Qv8KW N3PnEPBNyiR4hEtnHh/uBOlWNO45Sue7sG/l7lwflre9+QhecFXMUxRJiabaUaoNn529 7Uly5+j6b/NUe+wsTc311GiM1qlyO+kM0trMmnIoOuzl1hME4nAsoQMZFFGG+VUWEHnI UDgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="Alxp/7Mc"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d2si1457883edx.348.2021.01.12.07.45.52; Tue, 12 Jan 2021 07:46:17 -0800 (PST) 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=@google.com header.s=20161025 header.b="Alxp/7Mc"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2403790AbhALPnY (ORCPT + 99 others); Tue, 12 Jan 2021 10:43:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391924AbhALPnX (ORCPT ); Tue, 12 Jan 2021 10:43:23 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 911A5C061794 for ; Tue, 12 Jan 2021 07:42:42 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id v7so1325785wra.3 for ; Tue, 12 Jan 2021 07:42:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=4StJvEPcqpOoMaFQqXp2l4E4gqPLXrwP2qT6WBxKOzM=; b=Alxp/7McXhYTRj4ejJtb24GbL7XVg9SRHlt2G3JYcMOF79DupYcmHMkpMBV3rA26lL /t5W3RV9xFJTYTyzog+0/MA14nHuB+2z8uSslCINuDe4F9Z9NDg4EVm+QjsPIAHL+awx cUJBZs88nezRLWlkujK8fNlx1M5fdL+P/0pU4bJujWoHXzl5x0ZEctucjpdRaytc/ULD V8VivzQI/ByniVMebsIDDdapi5QgQx4tAXA2849ljJ4+913FTp0ftUwgjbZPjdDd74Rt ru5FJctTCCOPTy5xiYvXft5ecFFIUouOM2CVWeAeL+erG+cLh62c++oeysHylF9Wj7z3 VQ/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=4StJvEPcqpOoMaFQqXp2l4E4gqPLXrwP2qT6WBxKOzM=; b=RR9vVsFCrUmM+oFlaH6HU6Ar/pyFYLBO/6Ea+qoiXnh/VCfc0uGf3ynq9H0GWCLeJ2 w5iVKLMF17ciutD1PeG8DkkwxjQscvH8RmlQN+C73TQ23FyXK2wNncHDeKhnmwMWA3+g 95quoSwSs6GqIyA9EnkHCuZTH+lXiXSa/OMHY29U65X0JCPPdnQWFxfxp8lfCKiU4nru O4R6Y0iVkXyv8TdZmAOyIbI8Qw+f6giNoNltQgh6I+WAZr99uUbJm7Q7c85dPPnD6/so 8WaBFiZ/RMXenGQBcO9lq4O8tz3imd3SXQk4AJuOXnOC+PuO14cMixCU0k4la3zUBVOs 597g== X-Gm-Message-State: AOAM5304dU97ViDcIYCGVaVTPoo/FpsRbOSE4+1e70Q1kcWMWgV37Oim GgItwpFEdNzYpZL1uPN/s2ImqBgqWKzLOA== Sender: "jackmanb via sendgmr" X-Received: from beeg.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:11db]) (user=jackmanb job=sendgmr) by 2002:adf:8290:: with SMTP id 16mr4792288wrc.27.1610466161171; Tue, 12 Jan 2021 07:42:41 -0800 (PST) Date: Tue, 12 Jan 2021 15:42:25 +0000 In-Reply-To: <20210112154235.2192781-1-jackmanb@google.com> Message-Id: <20210112154235.2192781-2-jackmanb@google.com> Mime-Version: 1.0 References: <20210112154235.2192781-1-jackmanb@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH bpf-next v6 01/11] bpf: x86: Factor out emission of ModR/M for *(reg + off) From: Brendan Jackman To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , KP Singh , Florent Revest , linux-kernel@vger.kernel.org, "=?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?=" , John Fastabend , Yonghong Song , Brendan Jackman Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The case for JITing atomics is about to get more complicated. Let's factor out some common code to make the review and result more readable. NB the atomics code doesn't yet use the new helper - a subsequent patch will add its use as a side-effect of other changes. Acked-by: John Fastabend Signed-off-by: Brendan Jackman --- arch/x86/net/bpf_jit_comp.c | 43 +++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 796506dcfc42..30526776fa78 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -681,6 +681,27 @@ static void emit_mov_reg(u8 **pprog, bool is64, u32 dst_reg, u32 src_reg) *pprog = prog; } +/* Emit the suffix (ModR/M etc) for addressing *(ptr_reg + off) and val_reg */ +static void emit_insn_suffix(u8 **pprog, u32 ptr_reg, u32 val_reg, int off) +{ + u8 *prog = *pprog; + int cnt = 0; + + if (is_imm8(off)) { + /* 1-byte signed displacement. + * + * If off == 0 we could skip this and save one extra byte, but + * special case of x86 R13 which always needs an offset is not + * worth the hassle + */ + EMIT2(add_2reg(0x40, ptr_reg, val_reg), off); + } else { + /* 4-byte signed displacement */ + EMIT1_off32(add_2reg(0x80, ptr_reg, val_reg), off); + } + *pprog = prog; +} + /* LDX: dst_reg = *(u8*)(src_reg + off) */ static void emit_ldx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off) { @@ -708,15 +729,7 @@ static void emit_ldx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off) EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B); break; } - /* - * If insn->off == 0 we can save one extra byte, but - * special case of x86 R13 which always needs an offset - * is not worth the hassle - */ - if (is_imm8(off)) - EMIT2(add_2reg(0x40, src_reg, dst_reg), off); - else - EMIT1_off32(add_2reg(0x80, src_reg, dst_reg), off); + emit_insn_suffix(&prog, src_reg, dst_reg, off); *pprog = prog; } @@ -751,10 +764,7 @@ static void emit_stx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off) EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89); break; } - if (is_imm8(off)) - EMIT2(add_2reg(0x40, dst_reg, src_reg), off); - else - EMIT1_off32(add_2reg(0x80, dst_reg, src_reg), off); + emit_insn_suffix(&prog, dst_reg, src_reg, off); *pprog = prog; } @@ -1240,11 +1250,8 @@ st: if (is_imm8(insn->off)) goto xadd; case BPF_STX | BPF_XADD | BPF_DW: EMIT3(0xF0, add_2mod(0x48, dst_reg, src_reg), 0x01); -xadd: if (is_imm8(insn->off)) - EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off); - else - EMIT1_off32(add_2reg(0x80, dst_reg, src_reg), - insn->off); +xadd: + emit_modrm_dstoff(&prog, dst_reg, src_reg, insn->off); break; /* call */ -- 2.30.0.284.gd98b1dd5eaa7-goog