Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp599977pxu; Thu, 3 Dec 2020 08:09:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJzinh2BOeqrlorDxGvgFN9kCENev/z0BNSaEAxoRqs/yVV0/UW6QCjTj/LBnggQ4/XA/Jjs X-Received: by 2002:a17:906:22c7:: with SMTP id q7mr3161974eja.486.1607011774811; Thu, 03 Dec 2020 08:09:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607011774; cv=none; d=google.com; s=arc-20160816; b=xvUW3R0L9Sri0M8wOc7R3CwfObXlsJ4zM+DO9PhMZEuX4Qz0sCsgaBvFifw+1asRvo zTKIlxfjgo+8ACD/pqz026V97viApYS2e5I+/mPXLp8pvrVnuuxUUsHtF/6tC/z/cVIv gyP6wytDeqb51hdKQ2Izih4HM4In840f7tfn4ObbeccsS2IyUtce730i1e1Zyq8RdU0s 3B92AjG4HOKs7yfZ7DMa+gR4v8lY2dochzIgtV2D9N1MYJttyXLs7DAT2J2WlZqewELm HO1+LoNGhJSIi1TToKFqpKxo+tW60RFyAe1RHY8sI35kAWUKPuLFXnu5iI4kG7OSiJo+ o3xQ== 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=Zq0SqodNfGWo0FrhKhUzqW6LznJPM9VRVrmmLrNNcbU=; b=T/02Zfcq2AzsS2I0wb1KWBHt2873ojPD6iAM1SEwiVWmZnudbCr4KUMHicUnJdlYTK DIroafAvh1A8fcX0D1SyMLGW/tTvWfxxsBWPoGV07q1yujRac1xBM+L22xLeSrmxukSV kr8R9/oJdFykVoByp1i6XPDYfBa+/bacAcSg7DCOumHzgRn1dNcsJD9Wavv/ZSZ5J8C0 qnp0g48it82RNaIGRICxXY36sZUheWs7rzm/gR2H8Qg6jW7d6FznmLzakvfHnVOJkabr pJmzQiEIBM8JeQ1JeVA1COee/ORHwvpSS0SNN+BNUDOKPdb5ULG4rElGpPW4gH/yroAb nc4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=TzpdEgvI; 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 e22si1155212edv.437.2020.12.03.08.09.10; Thu, 03 Dec 2020 08:09:34 -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=TzpdEgvI; 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 S1731172AbgLCQEo (ORCPT + 99 others); Thu, 3 Dec 2020 11:04:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726267AbgLCQEn (ORCPT ); Thu, 3 Dec 2020 11:04:43 -0500 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42ED0C094241 for ; Thu, 3 Dec 2020 08:03:29 -0800 (PST) Received: by mail-qt1-x84a.google.com with SMTP id r29so1874130qtu.21 for ; Thu, 03 Dec 2020 08:03:29 -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=Zq0SqodNfGWo0FrhKhUzqW6LznJPM9VRVrmmLrNNcbU=; b=TzpdEgvIUmgM+1bz8MY//NyO1rKyKk13IzzSbcbwV2VqrSdQS/3/qkAHCqh+yuY4/H BcYXGyH49FPaNcNTKp4BPzkuBgh3OzUnZmj0O8bPFCoFEiiq8L2BfXABRm2rsd1kD4kt vbq8FR7qTzYBIYz8iBUAqikcmhG+kWWeSnXtlK+t9TdwFVGtXJNpVRtUUIIFMRSxPvFG +13484oP8c//fOdSuOL88AArhx4eCcXDTFvWnEJxwid+Ni0Iml/emnmT1yP0EvWzLLWE AnCYrAKz1Tc0ivYwKfC4V3j/gPYh4K95FiJEvpMzEBqE4oS3ksGWnFpUa8m+2Uz34Si8 3UpQ== 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=Zq0SqodNfGWo0FrhKhUzqW6LznJPM9VRVrmmLrNNcbU=; b=NlOH3Qh5lBsiyTe/2Gm6zvnU6U7pIO0biK1hYtYFJX6E/AYueC8917P90paiIBiKOp Ai+TASTE8hBd3mTKPIN7bMHGlFpaZfGEM9OiL5u+pCaJLP3qa3VWYeLQR7UwLezoiKtf EgdCBDv0CDFZ8mVxpeWqjDYT/sC1lt8jh2CDVEXLkinoSO3aiZEsyUi2uZmll+B3UNuB b9bOqWKgZSvDWDUTfkwQCM8aSWS7NTxEqSQjVxtm351zt8F1yHVLcHF8LiW41WZMnBXv k5dQ9E0AmGmOi9GToEhar/QLZnaPErToUb8E5dZHa01bcGvBLr897w1+PnXrIIm0vhAr b6EQ== X-Gm-Message-State: AOAM530Iwg6fzq5ugwFNoMc4DiZvSu33rdU3PbUenXiGnRK22IImQNMO TROFq7+HM0rHX0MywcXDI9P9Bcmf0/dVvw== 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:a0c:ec4a:: with SMTP id n10mr3763187qvq.54.1607011408407; Thu, 03 Dec 2020 08:03:28 -0800 (PST) Date: Thu, 3 Dec 2020 16:02:40 +0000 In-Reply-To: <20201203160245.1014867-1-jackmanb@google.com> Message-Id: <20201203160245.1014867-10-jackmanb@google.com> Mime-Version: 1.0 References: <20201203160245.1014867-1-jackmanb@google.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog Subject: [PATCH bpf-next v3 09/14] bpf: Pull out a macro for interpreting atomic ALU operations 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 Since the atomic operations that are added in subsequent commits are all isomorphic with BPF_ADD, pull out a macro to avoid the interpreter becoming dominated by lines of atomic-related code. Note that this sacrificies interpreter performance (combining STX_ATOMIC_W and STX_ATOMIC_DW into single switch case means that we need an extra conditional branch to differentiate them) in favour of compact and (relatively!) simple C code. Change-Id: I8cae5b66e75f34393de6063b91c05a8006fdd9e6 Signed-off-by: Brendan Jackman --- kernel/bpf/core.c | 79 +++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 28f960bc2e30..498d3f067be7 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -1618,55 +1618,52 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack) LDX_PROBE(DW, 8) #undef LDX_PROBE - STX_ATOMIC_W: - switch (IMM) { - case BPF_ADD: - /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */ - atomic_add((u32) SRC, (atomic_t *)(unsigned long) - (DST + insn->off)); - break; - case BPF_ADD | BPF_FETCH: - SRC = (u32) atomic_fetch_add( - (u32) SRC, - (atomic_t *)(unsigned long) (DST + insn->off)); - break; - case BPF_XCHG: - SRC = (u32) atomic_xchg( - (atomic_t *)(unsigned long) (DST + insn->off), - (u32) SRC); - break; - case BPF_CMPXCHG: - BPF_R0 = (u32) atomic_cmpxchg( - (atomic_t *)(unsigned long) (DST + insn->off), - (u32) BPF_R0, (u32) SRC); +#define ATOMIC(BOP, KOP) \ + case BOP: \ + if (BPF_SIZE(insn->code) == BPF_W) \ + atomic_##KOP((u32) SRC, (atomic_t *)(unsigned long) \ + (DST + insn->off)); \ + else \ + atomic64_##KOP((u64) SRC, (atomic64_t *)(unsigned long) \ + (DST + insn->off)); \ + break; \ + case BOP | BPF_FETCH: \ + if (BPF_SIZE(insn->code) == BPF_W) \ + SRC = (u32) atomic_fetch_##KOP( \ + (u32) SRC, \ + (atomic_t *)(unsigned long) (DST + insn->off)); \ + else \ + SRC = (u64) atomic64_fetch_##KOP( \ + (u64) SRC, \ + (atomic64_t *)(s64) (DST + insn->off)); \ break; - default: - goto default_label; - } - CONT; STX_ATOMIC_DW: + STX_ATOMIC_W: switch (IMM) { - case BPF_ADD: - /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */ - atomic64_add((u64) SRC, (atomic64_t *)(unsigned long) - (DST + insn->off)); - break; - case BPF_ADD | BPF_FETCH: - SRC = (u64) atomic64_fetch_add( - (u64) SRC, - (atomic64_t *)(s64) (DST + insn->off)); - break; + ATOMIC(BPF_ADD, add) + case BPF_XCHG: - SRC = (u64) atomic64_xchg( - (atomic64_t *)(u64) (DST + insn->off), - (u64) SRC); + if (BPF_SIZE(insn->code) == BPF_W) + SRC = (u32) atomic_xchg( + (atomic_t *)(unsigned long) (DST + insn->off), + (u32) SRC); + else + SRC = (u64) atomic64_xchg( + (atomic64_t *)(u64) (DST + insn->off), + (u64) SRC); break; case BPF_CMPXCHG: - BPF_R0 = (u64) atomic64_cmpxchg( - (atomic64_t *)(u64) (DST + insn->off), - (u64) BPF_R0, (u64) SRC); + if (BPF_SIZE(insn->code) == BPF_W) + BPF_R0 = (u32) atomic_cmpxchg( + (atomic_t *)(unsigned long) (DST + insn->off), + (u32) BPF_R0, (u32) SRC); + else + BPF_R0 = (u64) atomic64_cmpxchg( + (atomic64_t *)(u64) (DST + insn->off), + (u64) BPF_R0, (u64) SRC); break; + default: goto default_label; } -- 2.29.2.454.gaff20da3a2-goog