Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1567230pxu; Fri, 27 Nov 2020 09:59:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJz/xFENjt2Shqgm2jg8lATIvPVJAWeJ2D2k2u2YSmJQcXvIjYSjbxKkQrCncVhVmzgI6cpB X-Received: by 2002:a17:906:c0d1:: with SMTP id bn17mr9771267ejb.114.1606499983286; Fri, 27 Nov 2020 09:59:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606499983; cv=none; d=google.com; s=arc-20160816; b=sH1zA+G/eTGLw/GVG3WnrDkbtuwAtWxZ9Q7RyFIjbBgJ2kPXexHrEbd9Bo/gplYSbp 55b/nEyz/AlnK0IOZ2idYvQx+cP5WCIadPx1guyrwoLVJZYFs+7L8x5aeG30YVGLuTpS BazGVwq+/HutEFzbLWoHTjqabU0k3FSLJk/LYqs7Ug0TqlL5C/F1RAruTuSAmGjpsZqm OYk+WVXLJZkCqqQoWtTXpT9O/vXWptvbkfYnX3yxhDcytI181bVmAQECUQ9NfBw6PoN/ fEhcGwA7X6aE5Re0f7S/AxvhwSUy7IUugar6yU5bMPdZj/9JysmyreKQ6ULYGFiCErb1 BqUg== 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=pCm5bSXPkdYGa8DRY20xR0eBkPwWiyvd1AYgQ0fdFE4=; b=W9jf0p6uF5lHm3yaNiFWMYc2m/pwTSC2m4XSV/uz7zGgr/0zrFdxnFx+eALsbLcDPj hVguoh7NDsQSlt+M4ntwMqRY4KIS8tv9pvR1ux6UX4UPSsftfStVRl7qcrcS4CBT4ggM 4b/g6v7Vw95bEBKnCosEUmtx7JkU17MtpTjCo8Sv1julrTxIG2TUCdMSxtDOL209QXcp dt+EEBdKz2gkn3c+Uad5WyGTdi+QVKCwp0g18riS+EZobvOuYy/85xtQH117FyzT6S5T Cx+Bemo+xBYSbJlBOqgzyvA2Jo5j6UxfVY8oaFvktg75ousO1eChpiR+q4rwccTYLL+D +Piw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tIaLYwou; 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 cf17si5475612ejb.541.2020.11.27.09.59.21; Fri, 27 Nov 2020 09:59:43 -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=tIaLYwou; 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 S1732700AbgK0R5v (ORCPT + 99 others); Fri, 27 Nov 2020 12:57:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730985AbgK0R5u (ORCPT ); Fri, 27 Nov 2020 12:57:50 -0500 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAFBFC0613D1 for ; Fri, 27 Nov 2020 09:57:50 -0800 (PST) Received: by mail-qt1-x849.google.com with SMTP id r29so3635209qtu.21 for ; Fri, 27 Nov 2020 09:57:50 -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=pCm5bSXPkdYGa8DRY20xR0eBkPwWiyvd1AYgQ0fdFE4=; b=tIaLYwouJp51q4LpdC5w6IxE4wvtbS/vWXbBRw2PKuDiSDRPyxx1bUK25Gbdt41+Fx gkYmyxooYOjjdPNUl16J0NlVinIY0MfAxd+Mut69Qa0jLOfu7ZAZk+BDKjAYyi79C+yg 20Lwa7gg9F3Tkbilhq4e+UBxxMCJFL50IPeczhuyV/0GcQo0xF54K5Dg0x5y5X5ekSKB 665yHxJEDdyTcsDSHpSOnRBg56ZHLKv3cL7qxQuX0hCXlQp/mBpxx06VPdIOJBkXoBBU 7aq2o+ACt8z0wgsQMpQOg/eo84fBNm00gfKu7bHs/e5XBA5+aDh9XtHf3OV2C+X+JiQT Pi+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=pCm5bSXPkdYGa8DRY20xR0eBkPwWiyvd1AYgQ0fdFE4=; b=PbfNHN7cPHtwo3AKZcOTR5LcVRBzWXdh2p8VtXH5EQOTVI8pNkT/ktmF2dhZzvy8S4 bpwtDUmqtrRU4tcKdcp6pIjG1KsF1uqzdUuwCziapMxESho2fhZby8GcxbJJwfpo9lYA hbJV/wfHR0/Pe0tfdqkNP4WgPrqpJ+AAJsYkbQxPN3M10Tk55dXGs+jH/bvA+c8uYv4v r5gq4bV1BJkjbfZjsrA4Qxty3b9LvR9lILGf3Ldmf24tGZgSiQ/W5BaxafngT0NbuXzY wD2Kv9Zic/jwS2YJM3/Pun6pb5B+zU5k6F8L19RsrJgFQoBofgiJW+c4M6rrQuWhIN/o ihKQ== X-Gm-Message-State: AOAM531tS/eLbie7x5njPhquVrfzX5hUJbskYXc+O3icYkkDTYByF81a WBL3njdaOeQwgg2sQAR2EiQCai7l2iD2uw== 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:ad4:4432:: with SMTP id e18mr9496712qvt.57.1606499869651; Fri, 27 Nov 2020 09:57:49 -0800 (PST) Date: Fri, 27 Nov 2020 17:57:26 +0000 In-Reply-To: <20201127175738.1085417-1-jackmanb@google.com> Message-Id: <20201127175738.1085417-2-jackmanb@google.com> Mime-Version: 1.0 References: <20201127175738.1085417-1-jackmanb@google.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog Subject: [PATCH v2 bpf-next 01/13] bpf: x86: Factor out emission of ModR/M for *(reg + off) From: Brendan Jackman To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Yonghong Song , Daniel Borkmann , KP Singh , Florent Revest , linux-kernel@vger.kernel.org, Jann Horn , 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. Signed-off-by: Brendan Jackman --- arch/x86/net/bpf_jit_comp.c | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 796506dcfc42..94b17bd30e00 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 ModR/M byte for addressing *(r1 + off) and r2 */ +static void emit_modrm_dstoff(u8 **pprog, u32 r1, u32 r2, 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, r1, r2), off); + } else { + /* 4-byte signed displacement */ + EMIT1_off32(add_2reg(0x80, r1, r2), 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_modrm_dstoff(&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_modrm_dstoff(&prog, dst_reg, src_reg, off); *pprog = prog; } @@ -1240,11 +1250,7 @@ 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.29.2.454.gaff20da3a2-goog