Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp477288pxb; Thu, 14 Jan 2021 10:21:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJzpnVQqV/PNLpgTUlAlR8UNy5FsTGku7JGehFSANwQ0TabQnLlwZ2oJzIVN5tghhmoV01VU X-Received: by 2002:a05:6402:5193:: with SMTP id q19mr6805263edd.264.1610648505044; Thu, 14 Jan 2021 10:21:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610648505; cv=none; d=google.com; s=arc-20160816; b=Eby+/Yle8/IP6qLktlzPY9rMQUYHURkgbEbTkzEyE0gTbrcwG4s2ZiJQ6TUfciFW9J 3qHUVi1LSlF1rT8aqPLBsyHhgih5GqhDYiGFehAbHuiP5BBr7+ZgeXKKre1a4IHRikHd 80WHQpBWPJ+OwKzjaR2YmsqQn9F1c7CMJzDFFzj8qfvuLpvGE18zDethwYpCqg8c/sGZ pvC+dHJgyOYsP9YyfOqi3nwgo5bBDfLU6JdMGz/lSkDU3meeTcgDgbl/EZlOWWKZ1S9/ Bt/vPQr3VlLFIPwO8QNFQku2MkQLk6I+RLyAvDvD8uIcSzuPjuMMeRiyLkWOmOQQLXvJ 3NwQ== 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=GnGYLZQpXXpyrj8V8AXVE35GZ42ZV37BIth5wG6TAKc=; b=kbPfJG7X1Du+uvWGCsheHOWHtmvbU8N+s4yno3xrfJWrJeqXy7GNq57pNewiVnIkmQ V+4U6eqo2gU9uBkWo+FrNWyUDb5t+UsWy5KoBYSSKDY/TyYo/Enfzu+YYZ3+YNEpwqMN CWFp69+795AJVlo+ajPz7hwslpq/dZZ2aatRlT+8wXHLP1XZnJu+RbLtni7jJEbRlxdF TQmcGu7yMC89fJTTGeIUR0UWyQbb+vJk2GCxxKZRNL8Pz5CPHIoizpsHuD861paoz3Vh oBYA6orm7VEpy5pQ5XSWniaF8R6z67kknvO6aGrnf9i2UW+IUYWxtY9fnVUnnexLzRnf w9DA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=oEFvraAA; 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 m1si2648130eje.652.2021.01.14.10.21.20; Thu, 14 Jan 2021 10:21:45 -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=oEFvraAA; 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 S1729691AbhANSTo (ORCPT + 99 others); Thu, 14 Jan 2021 13:19:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727867AbhANSTm (ORCPT ); Thu, 14 Jan 2021 13:19:42 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6294DC06179C for ; Thu, 14 Jan 2021 10:18:25 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id x20so2190614wmc.0 for ; Thu, 14 Jan 2021 10:18:25 -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=GnGYLZQpXXpyrj8V8AXVE35GZ42ZV37BIth5wG6TAKc=; b=oEFvraAACDuc/sC3rYp4Qpya1srkmm9MPd3Jw/1QVatQwpM6kmtAqVDzDkZPsp9NbI zKE4eBInIo41m/6VCI8A9q2QKj19A9+oh5WWhi24SbKfgG9xO/1rtnjIQR8evUz82HGO LxjGYy/adXmfmehKX9CUBKKhn57pftDgB2OF03qSFHL06KkAyhEmw1mbXnz53sAT4kBv RG+4Nf5IMggCwBkZaUgrfN8FDEKNFU080Wo9sBPQxLyFuh2O9czBfdvkOH+bFaMvQvmn /TROFTg4oo1piDjHQwjkFpcEmNSQftLSskmd7nKRZV3VNR8NKKBMWMFb4ZOIEiGtGUJd YkDA== 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=GnGYLZQpXXpyrj8V8AXVE35GZ42ZV37BIth5wG6TAKc=; b=rqjEMhG8ZAEEwfZOmv0zquIK9/zD+G7hLxvMAFPtN5VwmwbIRtvZzAp69zxC7yE/f5 W++4hxSiSEjcAXNpIgm5x1j+uXW+U5ZzFHAiRpOycQBGzBxyeFFOHJUvaH90TJpaCnRi 1X+xZXx81IUQHgeSd6lcJmAzjvDDwf1vbxamX9toCx5O7+ywDWg9I7N7eLpMAW1Isd3Q HaY2ANg0qUmESETpa/V/hUPBcjH/v9C6cnfWUq7gIbzYFBFNksdiWaBJSNnFatIVKVOl trCV6rvz4mhX+qIMDZFk6YQmwueDYhVbqxqtMkUW+znT0sAV8AZv/KzIGaO9NIbUcn/8 mZQg== X-Gm-Message-State: AOAM5327ysmXu7iGQ5bcqjtnj8/15tgo84OMga/0jH7tjoe/NcAcm0VF kAmof2Fp6U8ePYJ/2hdZRorI+XLEG1dRFQ== Sender: "jackmanb via sendgmr" X-Received: from beeg.c.googlers.com ([fda3:e722:ac3:10:28:9cb1:c0a8:11db]) (user=jackmanb job=sendgmr) by 2002:a1c:2c89:: with SMTP id s131mr557023wms.0.1610648303757; Thu, 14 Jan 2021 10:18:23 -0800 (PST) Date: Thu, 14 Jan 2021 18:17:48 +0000 In-Reply-To: <20210114181751.768687-1-jackmanb@google.com> Message-Id: <20210114181751.768687-9-jackmanb@google.com> Mime-Version: 1.0 References: <20210114181751.768687-1-jackmanb@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH bpf-next v7 08/11] bpf: Pull out a macro for interpreting atomic ALU operations 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 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. Acked-by: Yonghong Song Signed-off-by: Brendan Jackman --- kernel/bpf/core.c | 80 +++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 4df6daba43ef..8669e685825f 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -1618,55 +1618,53 @@ 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_ALU_OP(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 *)(unsigned long) (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 *)(unsigned long) (DST + insn->off)); - break; + ATOMIC_ALU_OP(BPF_ADD, add) +#undef ATOMIC_ALU_OP + case BPF_XCHG: - SRC = (u64) atomic64_xchg( - (atomic64_t *)(unsigned long) (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 *)(unsigned long) (DST + insn->off), + (u64) SRC); break; case BPF_CMPXCHG: - BPF_R0 = (u64) atomic64_cmpxchg( - (atomic64_t *)(unsigned long) (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 *)(unsigned long) (DST + insn->off), + (u64) BPF_R0, (u64) SRC); break; + default: goto default_label; } -- 2.30.0.284.gd98b1dd5eaa7-goog