Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1568544pxu; Fri, 27 Nov 2020 10:01:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJyUttBg2OhakbBXY2ml8gDR+7LojLpFGZQc5veYQyPD5VWYhBxulJlLaFKOUxNGL5cYptU1 X-Received: by 2002:a2e:9510:: with SMTP id f16mr4386368ljh.408.1606500071087; Fri, 27 Nov 2020 10:01:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606500071; cv=none; d=google.com; s=arc-20160816; b=mcIEHSXgWGg25VWTXU6QHrpSn0QGs497M198m7/eNcGdhNGWrDmR8qpRpdphRhAt5o sarHK2SNRQfMn7j9Fygm96fbp0s4nvmoO7b1uubac2HZRkXw7GgbuOyzp3Uv+Nsyv3N/ YvmKxoVHE4MgXrSmq5PiVrYBLj8n8EABO3RFLjCyfiT51lAxa36Qxth/t9QO/lYI9uKU 2MGrppeTi+nl2KSpqutCDdrfMPv3cHZ8oJ75x4NqeOd9r51aAqvdvbQ5WBpbT6BQ3pNy Y3Tlw4K3zAkCCtzrbMD74cTj8M1Px4rm2ufByc+6CzkdQYJLVB4XcnYOnELwrEY9uOMn kKlw== 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=EiPiPUafMgOaehLSLTGCVFZQBAOetMfRBds+Fc1vJSU=; b=GuHUPIsEeXxC1rg7ngdmTRdYPFSjsBVunUufvpJfPtLXPK12bUoDIuF4qnsYAjJyoK mLAbvJ23BNQJqN1wZjxQO4249JQuF5owX0IBTofeg/2r/ON25Gt+yXdmn0SHXTveIqkm 5srV/LdoGOGXeb/Vc53mQyPxT7+wzsHwwPHpWxIaOg6cKwUKQLBIxbS7Yqc+YxMwKk4o onjGs5vB6tZw0T19Y5VLfC+1efuGcNSgXDOvyfW/WqMhZPU6eUXu/n/dB/Z4LU71dHqe 7rSBdaGx6xDlQol3GYNi7oi+MvZQKTL3sT7Uy64oMzYSzmN94/rRkR5BzGy3FYquk0rG H33w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=akOQ2ZMN; 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 u16si5654363edd.519.2020.11.27.10.00.47; Fri, 27 Nov 2020 10:01:11 -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=akOQ2ZMN; 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 S1732851AbgK0R55 (ORCPT + 99 others); Fri, 27 Nov 2020 12:57:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730985AbgK0R55 (ORCPT ); Fri, 27 Nov 2020 12:57:57 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACDB7C0613D1 for ; Fri, 27 Nov 2020 09:57:55 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id a22so3169929wmj.8 for ; Fri, 27 Nov 2020 09:57:55 -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=EiPiPUafMgOaehLSLTGCVFZQBAOetMfRBds+Fc1vJSU=; b=akOQ2ZMN9W6euIG+IX9HlmE81+hrKhPw5OCfnLqK8PqcBSdfEcIpZ2/w26yPMURt/f kHYXpDQ4Emx4vBD2UOdj56/tyhuo8ib5Ic8TejR8yEu5Fej6yP8kKUo9rCe7pwJ5e2lr 3/T976Jru34VCKkV9ncI66Z18jKrp3SWv5F/UtLCsCscH+ublDACEOClqwiFCqblEUlJ J/vD/zE94YloeZrYp/ovr0dL5jXyfmgtgNILR7rU0DHO/p0kE2yYxRGyAqsJ4LhX4xvB f5HibXuUlctdvesV1sf4ya6juEFTKnjKr2iaxR+mxRzDdLfCMJv6P31InidCMJTE0uwl ffZQ== 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=EiPiPUafMgOaehLSLTGCVFZQBAOetMfRBds+Fc1vJSU=; b=HHYDlIi0LeLzRVOVZXVujD+9ShzVpy5QIEApJO0/OprbeGvP5sOuIxKsiQynGTqZnI uddMRIayit1WvNvZ6WkEvp8ybv5Euz63DqFzOacG5u/xgWX7UFrLZvU+0YTFIC19AQSs bVlzwK/rmvWLaVKUGp82V4kAQyfX6NrKNfnDE0sQd77UB8nhGiHWke6SOCHenbcEf8XZ iBvvbJtt7BH9DsOT7mQO9jd2EKBZ0/FwbSFj9M2k7gxJKYIFSYolPj9/AVURQ+9iTPbJ 5iNauhp9oce+zwVTLRnaousQ0gZ8pj5AYkHlhgHxZfjVfDsBnSnBTEBWz0BbdtV7DxE2 Ybmw== X-Gm-Message-State: AOAM533Z3gxD9SVT0mp9Az9MR8mAn2msZ/rU1kBUFXPiocFKR1gqNFpF fNaCGL6kYhNwG285+0S88fLWhGVqCMx0Iw== 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:e7d0:: with SMTP id e16mr12210610wrn.114.1606499874225; Fri, 27 Nov 2020 09:57:54 -0800 (PST) Date: Fri, 27 Nov 2020 17:57:28 +0000 In-Reply-To: <20201127175738.1085417-1-jackmanb@google.com> Message-Id: <20201127175738.1085417-4-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 03/13] bpf: x86: Factor out function to emit NEG 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 There's currently only one usage of this but implementation of atomic_sub add another. Signed-off-by: Brendan Jackman --- arch/x86/net/bpf_jit_comp.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index a839c1a54276..49dea0c1a130 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -783,6 +783,22 @@ static void emit_stx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off) *pprog = prog; } + +static void emit_neg(u8 **pprog, u32 reg, bool is64) +{ + u8 *prog = *pprog; + int cnt = 0; + + /* Emit REX byte if necessary */ + if (is64) + EMIT1(add_1mod(0x48, reg)); + else if (is_ereg(reg)) + EMIT1(add_1mod(0x40, reg)); + + EMIT2(0xF7, add_1reg(0xD8, reg)); /* x86 NEG */ + *pprog = prog; +} + static bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs, int trapnr, unsigned long error_code, unsigned long fault_addr) @@ -884,11 +900,8 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, /* neg dst */ case BPF_ALU | BPF_NEG: case BPF_ALU64 | BPF_NEG: - if (BPF_CLASS(insn->code) == BPF_ALU64) - EMIT1(add_1mod(0x48, dst_reg)); - else if (is_ereg(dst_reg)) - EMIT1(add_1mod(0x40, dst_reg)); - EMIT2(0xF7, add_1reg(0xD8, dst_reg)); + emit_neg(&prog, dst_reg, + BPF_CLASS(insn->code) == BPF_ALU64); break; case BPF_ALU | BPF_ADD | BPF_K: -- 2.29.2.454.gaff20da3a2-goog